What's wrong with my code ? 534/539 test cases passed. Thanks


  • 0
    F
    public class SnakeGame {
        Snake snake;
        int width, height;
        int[][] food;
        int curFood;
        
        private int[][] dirs = {{1,0}, {-1, 0}, {0, 1}, {0, -1}};
        
        private int[] parseDir(String direction){
            if(direction.equals("U")) return dirs[1];
            if(direction.equals("D")) return dirs[0];
            if(direction.equals("R")) return dirs[2];
            if(direction.equals("L")) return dirs[3];
            return new int[]{0, 0};
        }
        
        private int score(){
            return snake.getLength() - 1;
        }
        
        private boolean eatFood(Location toEat){
            if(curFood < food.length){
                int[] curFoodLoc = food[curFood];
                if(toEat.equals(curFoodLoc)){
                    curFood++;
                    return true;
                }
            }
            return false;
        }
        /** 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;
            curFood = 0;
            snake = new Snake();
        }
        
        /** 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[] dir = parseDir(direction);
            
            Location next = nextLocation(snake.curHead(), dir);
            
            if(next.isOut(width, height)) return -1;
            
            
            if(eatFood(next)){
                snake.addHead(next);
            }
            else{
                snake.move(next); 
            }
            return score();
        }
        
        public Location nextLocation(Location cur, int[] dir){
            return new Location(cur.x + dir[0], cur.y + dir[1]);
        }
        
        class Location{
            int x, y;
            Location(int x, int y){
                this.x = x;
                this.y = y;
            }
            public boolean isOut(int width, int height){
                return !(x >= 0 && y >= 0 && x < height && y < width);
            }
            
            public boolean equals(int[] loc){
                return loc[0] == this.x && loc[1] == this.y;
            }
        }
        
        class Snake{
            Deque<Location> body;
            
            Snake(){
                body = new LinkedList<>();
                body.offerFirst(new Location(0, 0));
            }
            
            public void move(Location nextLoc){
                body.pollLast();
                body.offerFirst(nextLoc);
            }
            
            public int getLength(){
                return body.size();
            }
            
            public Location curHead(){
                return body.peekFirst();
            }
            
            public void addHead(Location newHead){
                body.offerFirst(newHead);
            }
        }
    }
    

Log in to reply
 

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