Design Tic-Tac-Toe
class TicTacToe {
int[] r;
int[] c;
int diagonal;
int antidiagonal;
/** Initialize your data structure here. */
public TicTacToe(int n) {
r = new int[n];
c = new int[n];
diagonal = 0;
antidiagonal = 0;
}
/** 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 movedPlayer= (player ==1)?1:-1;
r[row]+=movedPlayer;
c[col]+=movedPlayer;
if(row == col){
diagonal+=movedPlayer;
}
if(row == r.length-1-col){
antidiagonal+=movedPlayer;
}
if(Math.abs(r[row])==r.length||Math.abs(c[col])==c.length||Math.abs(diagonal)==c.length||Math.abs(antidiagonal) == c.length) {
return player;
}
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);
*/