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)

 1 public class SnakeGame {
 2     Map<String, int[]> map;
 3     int idx = 0;
 4     int[][] food;
 5     LinkedList<int[]> snake;
 6     int width;
 7     int height;
 8     Set<String> position = new HashSet<>();
 9 
10     public SnakeGame(int width, int height, int[][] food) {
11         this.food = food;
12         this.width = width;
13         this.height = height;
14         map = createMap();
15         snake = new LinkedList<>();
16         snake.add(new int[] { 0, 0 });
17         position.add(0 + "_" + 0);
18     }
19 
20     /**
21      * Moves the snake.
22      * 
23      * @param direction - 'U' = Up, 'L' = Left, 'R' = Right, 'D' = Down
24      * @return The game's score after the move. Return -1 if game over. Game over
25      *         when snake crosses the screen boundary or bites its body.
26      */
27     public int move(String direction) {
28         int[] dir = map.get(direction);
29         int[] head = snake.get(0);
30         int[] tail = snake.get(snake.size() - 1);
31         position.remove(tail[0] + "_" + tail[1]);
32 
33         int nextR = head[0] + dir[0];
34         int nextC = head[1] + dir[1];
35         String posiKey = nextR + "_" + nextC;
36         if (nextR < 0 || nextC < 0 || nextR >= height || nextC >= width || position.contains(posiKey)) {
37             return -1;
38         }
39 
40         if (idx < food.length && nextR == food[idx][0] && nextC == food[idx][1]) {
41             idx++;
42         } else {
43             snake.removeLast();
44         }
45         snake.addFirst(new int[] { nextR, nextC });
46         position.add(posiKey);
47         return snake.size() - 1;
48     }
49 
50     private Map<String, int[]> createMap() {
51         Map<String, int[]> map = new HashMap<>();
52         map.put("U", new int[] { -1, 0 });
53         map.put("L", new int[] { 0, -1 });
54         map.put("R", new int[] { 0, 1 });
55         map.put("D", new int[] { 1, 0 });
56         return map;
57     }
58 }