Timi Areola's Design for Tic-Tac-Toe


  • 0
    T

    class TicTacToe {

    private int[] rows;
    private int grid_count;
    private int[] cols;
    private int diagonal;
    private int anti_diagonal;

    public TicTacToe(int n) {
       rows = new int[n];
        cols = new int[n];
        grid_count = n;
    }
    
    
    public int move(int row, int col, int player) 
    {
        int play_count = player == 1 ? 1 : -1;
        
        rows[row] += play_count;
        cols[col] += play_count;
        
        if(row == col)
        {
            diagonal += play_count;
        }
        if(col == (cols.length - row - 1))
        {
            anti_diagonal += play_count;
        }
        
        if(Math.abs(rows[row]) == grid_count || Math.abs(cols[col]) == grid_count || Math.abs(diagonal) == grid_count ||                        Math.abs(anti_diagonal) == grid_count)
        { 
            return player;
        }
        return 0;
    }
    

    }


Log in to reply
 

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