-
Notifications
You must be signed in to change notification settings - Fork 1
/
LeetCode-353-Design-Snake-Game.java
72 lines (66 loc) · 2.14 KB
/
LeetCode-353-Design-Snake-Game.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
https://leetcode.com/problems/design-snake-game/discuss/82668/Java-Deque-and-HashSet-design-with-detailed-comments
*/
class SnakeGame {
Queue<Integer> queue;
int width;
int height;
int row, col; // the row and col of head position
int[][] foods;
int foodIndex;
/** Initialize your data structure here.
@param width - screen width
@param height - screen height
@param food - A list of food positions
E.g food = [[1,1], [1,0]] means the first food is positioned at [1,1], the second is at [1,0]. */
public SnakeGame(int width, int height, int[][] food) {
queue = new LinkedList<>();
this.width = width;
this.height = height;
this.row = 0;
this.col = 0;
this.foods = food;
this.foodIndex = 0;
}
/** Moves the snake.
@param direction - 'U' = Up, 'L' = Left, 'R' = Right, 'D' = Down
@return The game's score after the move. Return -1 if game over.
Game over when snake crosses the screen boundary or bites its body. */
public int move(String direction) {
switch(direction) {
case "U":
row--;
break;
case "D":
row++;
break;
case "L":
col--;
break;
case "R":
col++;
break;
default:
break;
}
int head = row * width + col;
if (queue.contains(head)) {
return -1;
}
if (row >= 0 && row < height && col >= 0 && col < width) {
queue.offer(head);
if (foodIndex < foods.length && row == foods[foodIndex][0] && col == foods[foodIndex][1]) {
foodIndex++;
} else {
queue.poll();
}
return foodIndex;
}
return -1;
}
}
/**
* Your SnakeGame object will be instantiated and called as such:
* SnakeGame obj = new SnakeGame(width, height, food);
* int param_1 = obj.move(direction);
*/