Java two queues, one for food one for snack + positions to store snack body position


  • 0
    A
    class SnakeGame {
    
        class Coordinate{
            int x;
            int y;
            public Coordinate(int x, int y){
                this.x = x;
                this.y = y;
            }
        }
        
        boolean[][] occupied;
        Queue<Coordinate> foodQ = new LinkedList<Coordinate>();
        Queue<Coordinate> snack = new LinkedList<Coordinate>();
    
        Coordinate targetFood;
        Coordinate head;
        
        int score = 0;
        int width;
        int height;
        
        /** 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;
            for(int i = 0 ; i < food.length; i++) {
                foodQ.add(new Coordinate(food[i][0], food[i][1]));
            }
            occupied = new boolean[height][width];
            occupied[0][0] = true;
            targetFood = foodQ.poll();
            head = new Coordinate(0,0);
            snack.add(head);
        }
        
        /** 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 newX = head.x;
            int newY = head.y;
            switch(direction) {
                case "U":
                    newX--;     break;
                case "L":
                    newY--;     break;
                case "R":
                    newY++;     break;
                case "D":
                    newX++;     break;
            }
            if(isNotValid(newX, newY))      return -1;
            
            Coordinate newBody = new Coordinate(newX, newY);
            head = newBody;
            snack.add(newBody);
            
            if(targetFood != null && newX == targetFood.x && newY == targetFood.y){
                score++;
                targetFood = foodQ.poll();
            } else {
                Coordinate tail = snack.poll();
                occupied[tail.x][tail.y] = false;
            }
            occupied[newX][newY] = true;
            return score;
        }
        
        private boolean isNotValid(int x, int y){
            if(x < 0 || x == height || y < 0 || y == width)     return true;
            if(occupied[x][y]){
                if (x != snack.peek().x || y != snack.peek().y){
                    return true;
                } else {
                    if (targetFood == null)     return false;
                    if (x == targetFood.x && y == targetFood.y){
                        return true;
                    }
                }
            }     
            return false;
        }
    }
    

Log in to reply
 

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