machine coding

Snake Game

The Nokia classic as an engine: a deque for the body, a set for “did I bite myself?”.

Easy90 minutesJava · class SnakeGame

the brief

What you are building

Remember Snake on the old Nokia phones? You will build its game engine — no graphics, just the rules.

The board is a grid. The snake starts in the top-left corner, one cell long. Food appears one piece at a time.

Each call moves the snake one cell. Eat food and you grow. Hit a wall or your own body, and the game is over.

must have

Requirements

  1. The board has height rows and width columns. The snake starts at row 0, column 0, with length 1. The score starts at 0.
  2. food lists the pieces in order. Only the next piece is on the board; after it is eaten, the following one appears.
  3. move takes U, D, L or R and moves the head one cell.
  4. If the new head is off the board, the game is over.
  5. If the new head is on the current food, the snake grows: the tail stays where it is, and the score goes up by 1. Otherwise the tail moves forward first (it leaves its cell).
  6. Then, if the new head is on the snake's body, the game is over. (So moving into the cell the tail just left is fine.)
  7. move returns the score, or -1 if this move ends the game. Every move after that also returns -1.

the contract

The Java API: class SnakeGame

This is close to LeetCode 353 (Design Snake Game, Premium); the move order above is the exact rule our judge uses.

public SnakeGame(int width, int height, int[][] food)
food[i] = {row, col}.
public int move(String direction)
Returns the score after the move, or -1 once the game is over.
  • 1 ≤ width, height ≤ 10,000 (so row * width + col fits in an int)
  • 0 ≤ food.length ≤ 5,000
  • up to 10,000 moves

example session

Calls in, answers out

A board 4 wide and 3 high. Food at (0,2), then (1,2), then (1,1).

callreturnswhy
new SnakeGame(4, 3, new int[][] {{0, 2}, {1, 2}, {1, 1}})—
move("R")0head at (0,1); no food here, so the tail follows
move("R")1eats at (0,2); length 2
move("D")2eats at (1,2); length 3
move("L")3eats at (1,1); length 4
move("U")3into (0,1), the cell the tail just left: allowed
move("D")-1back into (1,1), which is still body: it bit itself
move("L")-1the game is already over

time plan

Your 90 minutes

  1. 10 minclarify — Where does the snake start? Can it move into its tail's cell? What happens after game over? Write the move order down.
  2. 10 mindesign — The body changes at both ends: a deque. Is this cell body? must be fast: a set. Decide how to store a cell.
  3. 35 mincore code — move in four steps: new head, wall check, eat-or-move-tail, body check. Run the example.
  4. 25 minedge cases — 1×1 board, every wall, turning back on a length-2 and a length-3 snake, food under the tail, moves after game over.
  5. 10 minpolish — Name the steps. Be ready to add obstacles, or several snakes.

Practise under real pressure: a 90-minute clock with the phases above. It keeps running if you reload the page.

your turn

Build it here

Write the whole class in the editor. Run replays the example session; Submit adds the hidden tests (edge cases and large random sessions). Stuck for 20 minutes? Open one hint at a time.

Build SnakeGame in the browser

Design: implement every operation. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    The reference, the data-structure reasoning and the rubric are hidden so you can build it first. Even a half-working attempt teaches you more than reading a perfect answer.

    Opening all three hints also shows it.

    revise

    Chapters that teach these structures