Problem solution · Java

Design Snake Game

Design Snake Game: a Java solution using sliding window or two pointers. Learn the idea, check the complexity, and read the full code, with credit to walkccc LeetCode Solutions.

Technique
Sliding window or two pointers
Source
walkccc LeetCode Solutions
Length
76 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Sliding window or two pointers

For Design Snake Game, the implementation maintains a moving interval and updates only the information that enters or leaves the window.

  1. Choose the invariant that makes a window valid or useful.
  2. Advance the right boundary and add the new element.
  3. Move the left boundary only as needed while maintaining the invariant and updating the answer.

Code notes

  • 76 lines of Java from the credited upstream file 353.java.
  • The implementation visibly relies on sequence storage, hash lookup, ordered lookup, work queue.
  • No explicit loop blocks detected.

Complexity

Confirm that neither pointer moves backwards; if so, the scan is usually linear apart from the window’s data-structure operations.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from walkccc LeetCode Solutions by P.-Y. Chen (walkccc) and is used under the MIT licence.

Full codeDesign Snake Game · JavaJava
Use this to learn the idea, then write your own version.
class SnakeGame {  /**   * 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.width = width;    this.height = height;    this.food = food;    lookup.add(getId(0, 0));    body.offerLast(getId(0, 0));  }   /**   * 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) {    // the old head's position    int i = body.peekFirst() / width;    int j = body.peekFirst() % width;     // Update the head's position and check if it's out-of-bounds.    if (direction.equals("U") && --i < 0)      return -1;    if (direction.equals("L") && --j < 0)      return -1;    if (direction.equals("R") && ++j == width)      return -1;    if (direction.equals("D") && ++i == height)      return -1;     final int newHead = getId(i, j);     // 1. Eat food and increase the size by 1.    if (k < food.length && i == food[k][0] && j == food[k][1]) {      lookup.add(newHead);      body.offerFirst(newHead);      ++k;      return ++score;    }     // 2. new head != old tail and eat body!    if (newHead != body.peekLast() && lookup.contains(newHead))      return -1;     // 3. normal case    // Remove the old tail first, then add new head because new head may be in    // old tail's position.    lookup.remove(body.peekLast());    lookup.add(newHead);    body.pollLast();    body.offerFirst(newHead);    return score;  }   private int width;  private int height;  private int score = 0;  private int k = 0; // food's index  private int[][] food;  private Set<Integer> lookup = new HashSet<>();  private Deque<Integer> body = new ArrayDeque<>(); // snake's body   private int getId(int i, int j) {    return i * width + j;  }} 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗