Design Snake Game case not pass


  • 0
    T
    public class SnakeGame {
        Queue<Point> foods;
        int score = 0;
        List<Point> snake;
        int width, height;
        int row = 0;
        int col = 0;
        /** 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;
            foods = new LinkedList<Point>();
            snake = new LinkedList<Point>();
            Point head = new Point(0, 0);
            snake.add(head);
            for(int i = 0; i < food.length; ++ i){
                foods.offer(new Point(food[i][0], food[i][1]) );
            }
            
        }
        
        /** 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) {
          
            switch(direction){
                case "U":
                    if(row == 0) return -1; // Out of bound
                    -- row;
                    
                    break;
                case "L":
                    if(col == 0) return -1; // Out of bound
                    --col;
                    break;
                case "R":
                    if(col == width - 1) return -1; // Out of bound
                    ++ col;
                    break;
                case "D":
                    if(row == height - 1) return -1; // Out of bound
                    ++ row;
                    break;
                default :
                    return -1;
            }
            
            if(foods.isEmpty()){
                
                Point head = new Point(row, col);
                if(snake.contains(head)) return -1;
                snake.add(head);
                System.out.println("no food");
                snake.remove(0);
                return score;
            }
            
            Point food = foods.peek();
            //System.out.println(food.x+" "+food.y+" "+ row+" "+ col +" " + snake.size() );
            if(food.x == row && food.y == col ){
                score ++;
                foods.poll();
                //System.out.println("ate");
            }else{
             /*   Point tail = snake.poll();
                System.out.println("Throw" + tail.x + " " + tail.y);*/
                snake.remove(0);
            }
            
            Point head = new Point(row, col);
            //System.out.println("Head is " + head.x + " " + head.y);
            for(Point p : snake){
                //if(p.x == row && p.y == col) return -1;
                System.out.print(p.x + " " + p.y + "***");
            }
            if(snake.contains(head) ) {
                System.out.println("Bite itself");
                return -1;
            }
            System.out.println("\nMy head is " + head.x + " " + head.y);
            snake.add(head);
            
            
            return score;
        }
    }
    
    /**
     * Your SnakeGame object will be instantiated and called as such:
     * SnakeGame obj = new SnakeGame(width, height, food);
     * int param_1 = obj.move(direction);
     */
    

Log in to reply
 

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