# 348 - Design Tic-Tac-Toe

## 解法一 - player1 + 1，player2 - 1，記錄每個 row, col diag 的正負值

``````class TicTacToe {
public:
/** Initialize your data structure here. */
TicTacToe(int n): rows(n, 0), cols(n, 0) {
size = 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. */
int move(int row, int col, int player) {
int toAdd = player == 1 ? 1 : -1;

if(row - col == 0) { diagonal += toAdd; }
if(row + col == size-1) { antidiagonal += toAdd; }

if(abs(rows[row]) == size ||
abs(cols[col]) == size ||
abs(diagonal) == size  ||
abs(antidiagonal) == size) {
return player;
}

return 0;
}

private:
vector<int> rows, cols;
int diagonal, antidiagonal, size;
};

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