Fucking long java answer


  • 0
    V

    class SnakeGame {

    /** 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]. */
    class PositionNode {
        int x;
        int y;
        PositionNode pre = null;
        PositionNode next = null;
    }
    
    PositionNode foodHead;
    int score = 0;
    Deque<PositionNode> dq;
    HashMap<String, PositionNode> directMap;
    HashSet<Integer> snakePosSet;
    int foodPosition = -1;
    int width;
    int height;
    
    
    public SnakeGame(int width, int height, int[][] food) {
        this.width = width;
        this.height = height;
        foodHead = new PositionNode();
        snakePosSet = new HashSet<Integer>();
        initializeFoodNodeList(food);
        initializeDirectMap();
        dq = new LinkedList<PositionNode>();
        PositionNode initialNode = new PositionNode();
        initialNode.x = 0;
        initialNode.y = 0;
        dq.offer(initialNode);
        snakePosSet.add(0);
        if (food.length != 0 && food[0].length != 0) {
              foodPosition = food[0][0]*width + food[0][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) {
        PositionNode directNode = directMap.get(direction);
        PositionNode lastNode = dq.peekLast();
        PositionNode node = new PositionNode();
        node.x = lastNode.x + directNode.x;
        node.y = lastNode.y + directNode.y;
        
        if (node.x >= width || node.x < 0 || node.y >= height || node.y < 0) {
            return -1;
        }
        
        int pos = node.y*width + node.x;
        
        if (snakePosSet.contains(pos)) {
            if (dq.peekFirst().x != node.x || dq.peekFirst().y != node.y) {
                return -1;
            } 
            
            moveSnake(node);
        } else if (pos == foodPosition) {
            score++;
            dq.offerLast(node);
            snakePosSet.add(convertNodeToPos(node));
            putNewFood();
        } else {
            moveSnake(node);
        }
        
        return score;
    }
    
    private void moveSnake(PositionNode node) {
        PositionNode firstNode = dq.pollFirst();
        snakePosSet.remove(convertNodeToPos(firstNode));
        dq.offerLast(node);
        snakePosSet.add(convertNodeToPos(node));
    }
    
    private void initializeFoodNodeList(int[][] food) {
        PositionNode pre = foodHead;
        for (int i = 1; i < food.length; i++) {
            PositionNode node = new PositionNode();
            node.y = food[i][0];
            node.x = food[i][1];
            pre.next = node;
            node.pre = pre;
            pre = node;
        }
    }
    
    private void initializeDirectMap() {
        directMap = new HashMap<String, PositionNode>();
        
        PositionNode upNode = new PositionNode();
        upNode.y = -1;
        upNode.x = 0;
        directMap.put("U", upNode);
        
        PositionNode downNode = new PositionNode();
        downNode.y = 1;
        downNode.x = 0;
        directMap.put("D", downNode);
        
        PositionNode leftNode = new PositionNode();
        leftNode.y = 0;
        leftNode.x = -1;
        directMap.put("L", leftNode);
        
        PositionNode rightNode = new PositionNode();
        rightNode.y = 0;
        rightNode.x = 1;
        directMap.put("R", rightNode);
    }
    
    private void putNewFood() {
        PositionNode pre = foodHead;
        while (pre.next != null) {
            PositionNode node = pre.next;
            if (!snakePosSet.contains(convertNodeToPos(node))) {
                foodPosition = convertNodeToPos(node);
                node.pre.next = node.next;
                if (node.next != null) {
                    node.next.pre = node.pre; 
                } 
                
                break;
            }
            
            pre = node;
        }
    }
    
    private int convertNodeToPos(PositionNode node) {
        return node.y * width + node.x;
    }
    

    }


Log in to reply
 

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