Design Tic-Tac-Toe

medium design matrix

Problem

Design an n×n tic-tac-toe board where each move(row, col, player) returns whether that player just won (or 0 for no winner).

Inputmoves = [(0,0,1),(0,2,2),(2,2,1),(1,1,2),(2,0,1),(1,0,2),(2,1,1)]
Output[0,0,0,0,0,0,1]
Player 1 completes the bottom row on move 7.

class TicTacToe:
    def __init__(self, n):
        self.n = n
        self.rows = [[0, 0] for _ in range(n)]
        self.cols = [[0, 0] for _ in range(n)]
        self.diag = [0, 0]
        self.anti = [0, 0]
    def move(self, r, c, p):
        i = p - 1
        self.rows[r][i] += 1
        self.cols[c][i] += 1
        if r == c: self.diag[i] += 1
        if r + c == self.n - 1: self.anti[i] += 1
        if self.n in (self.rows[r][i], self.cols[c][i], self.diag[i], self.anti[i]):
            return p
        return 0
class TicTacToe {
  constructor(n) {
    this.n = n;
    this.rows = Array.from({length: n}, () => [0, 0]);
    this.cols = Array.from({length: n}, () => [0, 0]);
    this.diag = [0, 0]; this.anti = [0, 0];
  }
  move(r, c, p) {
    const i = p - 1;
    this.rows[r][i]++; this.cols[c][i]++;
    if (r === c) this.diag[i]++;
    if (r + c === this.n - 1) this.anti[i]++;
    if ([this.rows[r][i], this.cols[c][i], this.diag[i], this.anti[i]].includes(this.n)) return p;
    return 0;
  }
}
class TicTacToe {
    int n; int[][] rows, cols; int[] diag = new int[2], anti = new int[2];
    public TicTacToe(int n) { this.n = n; rows = new int[n][2]; cols = new int[n][2]; }
    public int move(int r, int c, int p) {
        int i = p - 1;
        rows[r][i]++; cols[c][i]++;
        if (r == c) diag[i]++;
        if (r + c == n - 1) anti[i]++;
        if (rows[r][i] == n || cols[c][i] == n || diag[i] == n || anti[i] == n) return p;
        return 0;
    }
}
class TicTacToe {
    int n; vector> rows, cols; int diag[2] = {0,0}, anti[2] = {0,0};
public:
    TicTacToe(int n) : n(n), rows(n, {0,0}), cols(n, {0,0}) {}
    int move(int r, int c, int p) {
        int i = p - 1;
        rows[r][i]++; cols[c][i]++;
        if (r == c) diag[i]++;
        if (r + c == n - 1) anti[i]++;
        if (rows[r][i] == n || cols[c][i] == n || diag[i] == n || anti[i] == n) return p;
        return 0;
    }
};
Time: O(1) per move Space: O(n)