> For the complete documentation index, see [llms.txt](https://hao-fu-1.gitbook.io/oj/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hao-fu-1.gitbook.io/oj/string/348.-design-tic-tac-toe.md).

# 348. Design Tic-Tac-Toe

N子棋给定每一步由谁走在哪，要求实时判断出胜负。和Valid Tic-Tac-Toe类似，用rows\[]，cols\[]，diag和rdiag实时记录对应的行，列，对角线当前的值，当变为N时说明取胜。

```
class TicTacToe {
public:
    vector<int> rows, cols;
    int diag, rdiag, N;
    /** Initialize your data structure here. */
    TicTacToe(int n): rows(n), cols(n), diag(0), rdiag(0), N(n) {}

    int move(int row, int col, int player) {
        int add = player == 1 ? 1 : -1;
        rows[row] += add;
        cols[col] += add;
        diag += (row == col ? add : 0);
        rdiag += (row == N - col - 1 ? add : 0);
        return (abs(rows[row]) == N || abs(cols[col]) == N || abs(diag) == N || abs(rdiag) == N) ? player : 0;
    }
};

```
