7
7
8
8
/**
9
9
* Design a Snake game that is played on a device with screen size = width x height. Play the game online if you are not familiar with the game.
10
-
11
- The snake is initially positioned at the top left corner (0,0) with length = 1 unit.
12
-
13
- You are given a list of food's positions in row-column order. When a snake eats the food, its length and the game's score both increase by 1.
14
-
15
- Each food appears one by one on the screen. For example, the second food will not appear until the first food was eaten by the snake.
16
-
17
- When a food does appear on the screen, it is guaranteed that it will not appear on a block occupied by the snake.
18
-
19
- Example:
20
- Given width = 3, height = 2, and food = [[1,2],[0,1]].
21
-
22
- Snake snake = new Snake(width, height, food);
23
-
24
- Initially the snake appears at position (0,0) and the food at (1,2).
25
-
26
- |S| | |
27
- | | |F|
28
-
29
- snake.move("R"); -> Returns 0
30
-
31
- | |S| |
32
- | | |F|
33
-
34
- snake.move("D"); -> Returns 0
35
-
36
- | | | |
37
- | |S|F|
38
-
39
- snake.move("R"); -> Returns 1 (Snake eats the first food and right after that, the second food appears at (0,1) )
40
-
41
- | |F| |
42
- | |S|S|
43
-
44
- snake.move("U"); -> Returns 1
45
-
46
- | |F|S|
47
- | | |S|
48
-
49
- snake.move("L"); -> Returns 2 (Snake eats the second food)
50
-
51
- | |S|S|
52
- | | |S|
53
-
54
- snake.move("U"); -> Returns -1 (Game over because snake collides with border)
10
+ * <p>
11
+ * The snake is initially positioned at the top left corner (0,0) with length = 1 unit.
12
+ * <p>
13
+ * You are given a list of food's positions in row-column order. When a snake eats the food, its length and the game's score both increase by 1.
14
+ * <p>
15
+ * Each food appears one by one on the screen. For example, the second food will not appear until the first food was eaten by the snake.
16
+ * <p>
17
+ * When a food does appear on the screen, it is guaranteed that it will not appear on a block occupied by the snake.
18
+ * <p>
19
+ * Example:
20
+ * Given width = 3, height = 2, and food = [[1,2],[0,1]].
21
+ * <p>
22
+ * Snake snake = new Snake(width, height, food);
23
+ * <p>
24
+ * Initially the snake appears at position (0,0) and the food at (1,2).
25
+ * <p>
26
+ * |S| | |
27
+ * | | |F|
28
+ * <p>
29
+ * snake.move("R"); -> Returns 0
30
+ * <p>
31
+ * | |S| |
32
+ * | | |F|
33
+ * <p>
34
+ * snake.move("D"); -> Returns 0
35
+ * <p>
36
+ * | | | |
37
+ * | |S|F|
38
+ * <p>
39
+ * snake.move("R"); -> Returns 1 (Snake eats the first food and right after that, the second food appears at (0,1) )
40
+ * <p>
41
+ * | |F| |
42
+ * | |S|S|
43
+ * <p>
44
+ * snake.move("U"); -> Returns 1
45
+ * <p>
46
+ * | |F|S|
47
+ * | | |S|
48
+ * <p>
49
+ * snake.move("L"); -> Returns 2 (Snake eats the second food)
50
+ * <p>
51
+ * | |S|S|
52
+ * | | |S|
53
+ * <p>
54
+ * snake.move("U"); -> Returns -1 (Game over because snake collides with border)
55
55
*/
56
56
public class _353 {
57
57
public class SnakeGame {
@@ -63,11 +63,14 @@ public class SnakeGame {
63
63
int width ;
64
64
int height ;
65
65
66
- /** Initialize your data structure here.
67
- @param width - screen width
68
- @param height - screen height
69
- @param food - A list of food positions
70
- E.g food = [[1,1], [1,0]] means the first food is positioned at [1,1], the second is at [1,0]. */
66
+ /**
67
+ * Initialize your data structure here.
68
+ *
69
+ * @param width - screen width
70
+ * @param height - screen height
71
+ * @param food - A list of food positions
72
+ * E.g food = [[1,1], [1,0]] means the first food is positioned at [1,1], the second is at [1,0].
73
+ */
71
74
public SnakeGame (int width , int height , int [][] food ) {
72
75
this .set = new HashSet ();
73
76
set .add (0 );//initially at [0][0]
@@ -78,17 +81,20 @@ public SnakeGame(int width, int height, int[][] food) {
78
81
this .height = height ;
79
82
}
80
83
81
- /** Moves the snake.
82
- @param direction - 'U' = Up, 'L' = Left, 'R' = Right, 'D' = Down
83
- @return The game's score after the move. Return -1 if game over.
84
- Game over when snake crosses the screen boundary or bites its body. */
84
+ /**
85
+ * Moves the snake.
86
+ *
87
+ * @param direction - 'U' = Up, 'L' = Left, 'R' = Right, 'D' = Down
88
+ * @return The game's score after the move. Return -1 if game over.
89
+ * Game over when snake crosses the screen boundary or bites its body.
90
+ */
85
91
public int move (String direction ) {
86
- if (score == -1 ) return -1 ;
92
+ if (score == -1 ) return -1 ;
87
93
88
94
//compute head
89
95
int rowHead = body .peekFirst () / width ;
90
96
int colHead = body .peekFirst () % width ;
91
- switch (direction ){
97
+ switch (direction ) {
92
98
case "U" :
93
99
rowHead --;
94
100
break ;
@@ -101,11 +107,11 @@ public int move(String direction) {
101
107
default :
102
108
colHead ++;
103
109
}
104
- int newHead = rowHead * width + colHead ;
110
+ int newHead = rowHead * width + colHead ;
105
111
106
112
set .remove (body .peekLast ());//we'll remove the tail from set for now to see if it hits its tail
107
113
//if it hits the boundary
108
- if (set .contains (newHead ) || rowHead < 0 || colHead < 0 || rowHead >= height || colHead >= width ){
114
+ if (set .contains (newHead ) || rowHead < 0 || colHead < 0 || rowHead >= height || colHead >= width ) {
109
115
return score = -1 ;
110
116
}
111
117
@@ -114,7 +120,7 @@ public int move(String direction) {
114
120
body .offerFirst (newHead );
115
121
116
122
//normal eat case: keep tail, add head
117
- if (foodIndex < food .length && rowHead == food [foodIndex ][0 ] && colHead == food [foodIndex ][1 ]){
123
+ if (foodIndex < food .length && rowHead == food [foodIndex ][0 ] && colHead == food [foodIndex ][1 ]) {
118
124
set .add (body .peekLast ());//old tail does not change, so add it back to set since we removed it earlier
119
125
foodIndex ++;
120
126
return ++score ;
0 commit comments