Test case have the Snake "turn around", is it correct?


  • 3
    L

    Is this a valid test case?

    ["SnakeGame","move","move","move"]
    [[3,3,[[2,0],[0,0]]],["D"],["D"],["U"]]

      public class SnakeGame {
            LinkedHashSet<Integer> snake = new LinkedHashSet<Integer>();
            Integer head = null;
            int width;
            int height;
            int[][] food;
            int foodCursor;
        
            /** 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;
                this.foodCursor = 0;
                head = 0;
                snake.add(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) {
                int next = -1;
                
                if(direction.equals("L")){
                    if(head % width == 0){
                        return -1;
                    }else{
                        next = head - 1;
                    }
                }else if(direction.equals("R")){
                    if(head % width == width - 1){
                        return -1;
                    }else{
                        next = head + 1;
                    }
                }else if(direction.equals("U")){
                    if(head / width == 0){
                        return -1;
                    }else{
                        next = head - width;
                    }
                }else if(direction.equals("D")){
                    if(head / width == height - 1){
                        return -1;
                    }else{
                        next = head + width;
                    }
                }
                
                if(foodCursor < food.length && food[foodCursor][0] * width + food[foodCursor][1] == next){
                    if(snake.contains(next)){
                        return -1;
                    }
                    
                    snake.add(next);
                    head = next;
                    foodCursor++;
                }else{
                    snake.remove(snake.iterator().next());
                    
                    if(snake.contains(next)){
                        return -1;
                    }
                    snake.add(next);
                    head = next;
                }
                
                return snake.size() - 1;
            }
        }

  • 0
    H

    I share the same concern! This particular test case is arguable since it allows the snake to bite its own body.

    There are two ways to implement the snake body growth when eating food:

    1. Remove snake tail first before checking body bite, and next if we eat food, add back the tail. (it'd pass the test case);
    2. Keep the snake tail before checking body bite. Next if no food to eat, remove the tail. (it'd fail the test case).

    Actually, I think the code with Option 2) makes more sense, because when the snake eats a food, it grows immediately with no need to shrink tail before growing. So I'd consider fix this test case.


  • 1
    W

    The same! Test case is wrong. In real game, snake can't go in the opposite direction.


  • 1
    J

    I think this test case does not make sense because the snake cannot turn around immediately. In the js game, the turn around option is disabled.


  • 0
    J

    I agree with you. Nice analysis!


  • 1

    We have just fixed the affected test cases. There should no longer be "turn around" cases for the Snake anymore. Could you please check? Thanks.


  • 0
    L

    this case:
    ["SnakeGame","move","move","move","move","move","move","move","move","move","move","move","move"]
    [[3,3,[[2,0],[0,0],[0,2],[2,2]]],["D"],["D"],["R"],["U"],["U"],["L"],["D"],["R"],["R"],["U"],["L"],["D"]]

    I guess the question is when the snake moves, whether head changes first or tail changes first. In my previous experience of snake, it is not allowed to go to the cell the tail was in.


  • 0

    I think this is a valid test. You can imagine that the Snake is almost touching its tail. If you consider the snake is moving in a continuous motion, while advancing to the next block the Snake is chasing its tail but will never touch it.


  • 0
    W

    @1337c0d3r can you check this post thanks. link text I think the value of food after all of them have been eaten is confusing.


  • 1
    T

    @wuzixigua
    Strictly speaking, the snake can't go in the opposite direction if its length is greater than 1.


  • 0
    J

    This test case still have lots of 'turn arounds'

    for example:
    ,["R"],["U"],["D"],["R"], in this case, D should be ignored and just return current value.

    ["SnakeGame","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move","move"]
    [[100,30,[[11,0],[58,7]]],["D"],["R"],["R"],["D"],["D"],["U"],["D"],["R"],["L"],["R"],["U"],["D"],["D"],["R"],["R"],["U"],["D"],["R"],["D"],["D"],["D"],["D"],["U"],["D"],["L"],["D"],["U"],["D"],["U"],["D"],["R"],["L"],["L"],["R"],["D"],["L"],["U"],["L"],["L"],["L"],["R"],["R"],["U"],["R"],["L"],["D"],["R"],["L"],["U"],["U"],["D"],["D"],["D"],["L"],["L"],["D"],["L"],["D"],["R"],["U"],["L"],["U"],["R"],["R"],["U"],["L"],["D"],["L"],["D"],["D"]]


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.