Design Tic-Tac-Toe

设计连连看游戏. 要求move的时候判断胜负, 并且要求判断O(1).

class TicTacToe {
    int[] rows;
    int[] cols;
    int diag;
    int anti_diag;
    int n;
    /** Initialize your data structure here. */
    public TicTacToe(int n) {
        this.rows = new int[n];
        this.cols = new int[n];
        this.diag = 0;
        this.anti_diag = 0;
        this.n = n;
    }
    
    /** Player {player} makes a move at ({row}, {col}).
        @param row The row of the board.
        @param col The column of the board.
        @param player The player, can be either 1 or 2.
        @return The current winning condition, can be either:
                0: No one wins.
                1: Player 1 wins.
                2: Player 2 wins. */
    public int move(int row, int col, int player) {
        int m = player == 1 ? 1 : -1;
        rows[row] += m;
        cols[col] += m;
        if(row == col) {
            diag += m;
        }
        if(row == n - col - 1) {
            anti_diag += m;
        }
        if(rows[row] == n || cols[col] == n || diag == n || anti_diag == n)
            return 1;
        else if(rows[row] == -n || cols[col] == -n || diag == -n || anti_diag == -n)
            return 2;
        return 0;
    }
    
 }

/**
 * Your TicTacToe object will be instantiated and called as such:
 * TicTacToe obj = new TicTacToe(n);
 * int param_1 = obj.move(row,col,player);
 */