Java Deque , Queue and Set


  • 0
    A
    public class SnakeGame {
    
        private int rows, columns;
        private Queue<int[]> foodPositions;
        private Deque<int[]> snakePositions;
        private Set<Integer> occupied;// will store a mapping of (i,j) : i * columns + j
        private int score;
    
        private Map<Character, int[]> directions = new HashMap<Character, int[]>() {{
            put('U', new int[] {-1, 0});
            put('D', new int[] {1, 0});
            put('L', new int[] {0, -1});
            put('R', new int[] {0, 1});
        }};
    
        /** 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(final int width, final int height, final int[][] food) {
            rows = height;
            columns = width;
            score = 0;
    
            foodPositions = new LinkedList<>();
            for (int[] foodPosition : food) {
                foodPositions.offer(foodPosition);
            }
    
            snakePositions = new LinkedList<>();
            snakePositions.offerLast(new int[]{0, 0});
            occupied = new HashSet<>();
            occupied.add(0);// will store a mapping of (i,j) : i * columns + j
        }
    
        /** 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(final String direction) {
            if (direction == null || direction.isEmpty() || !directions.containsKey(direction.charAt(0))) {
                throw new IllegalArgumentException("Invalid direction");
            }
    
            int[] offset = directions.get(direction.charAt(0));
            int[] snakeHead = snakePositions.peekLast();
    
            int nextRow = snakeHead[0] + offset[0];
            int nextColumn = snakeHead[1] + offset[1];
    
            int[] snakeTail = snakePositions.pollFirst();
            occupied.remove(mapToRowOrderPosition(snakeTail[0], snakeTail[1]));// map (i,j) to i * columns + j
    
            if (nextRow < 0 || nextRow >= rows || nextColumn < 0 || nextColumn >= columns || occupied.contains(mapToRowOrderPosition(nextRow, nextColumn))) {
                return -1;
            }
    
            if (!foodPositions.isEmpty()) {
                int[] foodPosition = foodPositions.peek();
                if (nextRow == foodPosition[0] && nextColumn == foodPosition[1]) {
                    score++;
                    foodPositions.poll();
    
                    // length is increased. So add back snake's tail position at the end again.
                    snakePositions.offerFirst(snakeTail);
                    occupied.add(mapToRowOrderPosition(snakeTail[0], snakeTail[1]));// map (i,j) to i * columns + j
                }
            }
    
            snakePositions.offerLast(new int[]{nextRow, nextColumn});
            occupied.add(mapToRowOrderPosition(nextRow, nextColumn));// map (i,j) to i * columns + j
    
            return score;
        }
    
        private int mapToRowOrderPosition(final int row, final int column) {
            return row * columns + column;
        }
    }
    

Log in to reply
 

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