353. Design Snake Game

Problem

Design the snake game. Move the snake and return the current score or -1 if the snake hits a wall or itself.

Design the classic Snake game with:

  • SnakeGame(width, height, food): Initialize the game.
  • move(direction): Move the snake in the given direction (U, D, L, R). Return the score after the move, or -1 if the game ends.

Rules:

  • Snake starts in the middle
  • Snake moves one cell at a time
  • Eating food increases score by 1 and snake grows
  • Game ends if snake hits the wall or itself
Examples
Input: [["SnakeGame","move","move","move","move","move","move"],[[3,2,[[1,2],[0,1]]],["R"],["D"],["R"],["U"],["L"],["U"]]]
Output: [null,0,0,1,1,2,-1]
Hints
Related Problems

353. Design Snake Game

Design the snake game. Move the snake and return the current score or -1 if the snake hits a wall or itself.