LeetCode-Design Snake Game

LiBlog發表於2016-08-15

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.

The snake is initially positioned at the top left corner (0,0) with length = 1 unit.

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.

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.

When a food does appear on the screen, it is guaranteed that it will not appear on a block occupied by the snake.

Example:

Given width = 3, height = 2, and food = [[1,2],[0,1]].

Snake snake = new Snake(width, height, food);

Initially the snake appears at position (0,0) and the food at (1,2).

|S| | |
| | |F|

snake.move("R"); -> Returns 0

| |S| |
| | |F|

snake.move("D"); -> Returns 0

| | | |
| |S|F|

snake.move("R"); -> Returns 1 (Snake eats the first food and right after that, the second food appears at (0,1) )

| |F| |
| |S|S|

snake.move("U"); -> Returns 1

| |F|S|
| | |S|

snake.move("L"); -> Returns 2 (Snake eats the second food)

| |S|S|
| | |S|

snake.move("U"); -> Returns -1 (Game over because snake collides with border)

Credits:
Special thanks to @elmirap for adding this problem and creating all test cases.

Analysis:

Use a Deque to store snake, fast add head and remove tail.

Use a HashSet to fast query about occupancy of a point.

Solution:

 1 public class SnakeGame {
 2         Set<Integer> snakeSet;
 3         int[][] foods;
 4         int foodInd;
 5         int score;
 6         Deque<Integer> snake;
 7         int w,h;
 8         int[] head;
 9 
10         /** Initialize your data structure here.
11             @param width - screen width
12             @param height - screen height 
13             @param food - A list of food positions
14             E.g food = [[1,1], [1,0]] means the first food is positioned at [1,1], the second is at [1,0]. */
15         public SnakeGame(int width, int height, int[][] food) {
16             foods = food;
17             foodInd = 0;
18             w = width;
19             h = height;
20             
21             snake = new LinkedList<Integer>();
22             snake.addFirst(0);
23             snakeSet = new HashSet<Integer>();
24             snakeSet.add(0);
25             head = new int[]{0,0};
26             
27             score = 0;
28         }
29         
30         /** Moves the snake.
31             @param direction - 'U' = Up, 'L' = Left, 'R' = Right, 'D' = Down 
32             @return The game's score after the move. Return -1 if game over. 
33             Game over when snake crosses the screen boundary or bites its body. */
34         public int move(String direction) {
35             // whether move is valid
36             int[] newHead;
37             switch (direction.charAt(0)){
38                 case 'U' : newHead = new int[]{head[0]-1,head[1]};
39                             break;
40                 case 'L' : newHead = new int[]{head[0],head[1]-1};
41                             break;
42                 case 'R' : newHead = new int[]{head[0],head[1]+1};
43                             break;
44                 case 'D' : newHead = new int[]{head[0]+1,head[1]};
45                             break;
46                 default : return -1;
47             }
48             if (newHead[0]<0 || newHead[0]>=h || newHead[1]<0 || newHead[1]>=w){
49                 return -1;
50             }
51             
52             int newHeadInd = newHead[0]*w+newHead[1];
53             int tailInd = snake.peekLast();
54             if (snakeSet.contains(newHeadInd) && (newHeadInd!=tailInd)){
55                 return -1;
56             }
57             
58             // if eat a food
59             if (foodInd < foods.length && newHead[0] == foods[foodInd][0] && newHead[1] == foods[foodInd][1]){
60                 score++;
61                 foodInd++;
62             } else {
63                 // regular move
64                 snake.removeLast();
65                 snakeSet.remove(tailInd);
66             }
67             snake.addFirst(newHeadInd);
68             snakeSet.add(newHeadInd);
69             head = newHead;
70             
71             return score;
72         }
73 }
74 
75 /**
76  * Your SnakeGame object will be instantiated and called as such:
77  * SnakeGame obj = new SnakeGame(width, height, food);
78  * int param_1 = obj.move(direction);
79  */

 

相關文章