77
88/**
99 * 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)
5555 */
5656public class _353 {
5757 public class SnakeGame {
@@ -63,11 +63,14 @@ public class SnakeGame {
6363 int width ;
6464 int height ;
6565
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+ */
7174 public SnakeGame (int width , int height , int [][] food ) {
7275 this .set = new HashSet ();
7376 set .add (0 );//initially at [0][0]
@@ -78,17 +81,20 @@ public SnakeGame(int width, int height, int[][] food) {
7881 this .height = height ;
7982 }
8083
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+ */
8591 public int move (String direction ) {
86- if (score == -1 ) return -1 ;
92+ if (score == -1 ) return -1 ;
8793
8894 //compute head
8995 int rowHead = body .peekFirst () / width ;
9096 int colHead = body .peekFirst () % width ;
91- switch (direction ){
97+ switch (direction ) {
9298 case "U" :
9399 rowHead --;
94100 break ;
@@ -101,11 +107,11 @@ public int move(String direction) {
101107 default :
102108 colHead ++;
103109 }
104- int newHead = rowHead * width + colHead ;
110+ int newHead = rowHead * width + colHead ;
105111
106112 set .remove (body .peekLast ());//we'll remove the tail from set for now to see if it hits its tail
107113 //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 ) {
109115 return score = -1 ;
110116 }
111117
@@ -114,7 +120,7 @@ public int move(String direction) {
114120 body .offerFirst (newHead );
115121
116122 //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 ]) {
118124 set .add (body .peekLast ());//old tail does not change, so add it back to set since we removed it earlier
119125 foodIndex ++;
120126 return ++score ;
0 commit comments