# Clear Java solution with comments (259ms)

• ``````public class SnakeGame {
int[][] food;
//row*width+column to save column and row position
List<Integer> snake;
int foodPos = 0;
int height, width;
int[][] dirs = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };

/**
* 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) {
this.height = height;
this.width = width;
this.food = food;
snake = new ArrayList<>();
}

/**
* 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) {
int[] d = new int[2];
switch (direction) {
case "U":
d = dirs[1];
break;
case "L":
d = dirs[3];
break;
case "R":
d = dirs[2];
break;
case "D":
d = dirs[0];
}

// hit wall, die
return -1;
}

// hit itself, die(new head is legal to be the tail)
return -1;
}

// if food, eat.
// release new food
foodPos++;
} else {
//no food move