> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Design Tic-Tac-Toe Python Solution with Tests

> Tested Python solution for LeetCode 348 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 348, [Medium](/catalog/medium). Topics: [Design](/catalog/topics/design), [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Matrix](/catalog/topics/matrix), [Simulation](/catalog/topics/simulation). [View on LeetCode](https://leetcode.com/problems/design-tic-tac-toe/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 348   # by problem number
lcpy gen -s design_tic_tac_toe   # by problem name
```

## Problem

Assume the following rules are for the tic-tac-toe game on an `n x n` board between two players:

1. A move is guaranteed to be valid and is placed on an empty block.
2. Once a winning condition is reached, no more moves are allowed.
3. A player who succeeds in placing `n` of their marks in a horizontal, vertical, or diagonal row wins the game.

Implement the `TicTacToe` class:

* `TicTacToe(int n)` Initializes the object the size of the board `n`.
* `int move(int row, int col, int player)` Indicates that the player with id `player` plays at the cell `(row, col)` of the board. The move is guaranteed to be a valid move, and the two players alternate in making moves. Return
  * `0` if there is **no winner** after the move,
  * `1` if **player 1** is the winner after the move, or
  * `2` if **player 2** is the winner after the move.

### Examples

```
Input
["TicTacToe", "move", "move", "move", "move", "move", "move", "move"]
[[3], [0, 0, 1], [0, 2, 2], [2, 2, 1], [1, 1, 2], [2, 0, 1], [1, 0, 2], [2, 1, 1]]
Output
[null, 0, 0, 0, 0, 0, 0, 1]

Explanation
TicTacToe ticTacToe = new TicTacToe(3);
Assume that player 1 is "X" and player 2 is "O" in the board.
ticTacToe.move(0, 0, 1); // return 0 (no one wins)
|X| | |
| | | |    // Player 1 makes a move at (0, 0).
| | | |

ticTacToe.move(0, 2, 2); // return 0 (no one wins)
|X| |O|
| | | |    // Player 2 makes a move at (0, 2).
| | | |

ticTacToe.move(2, 2, 1); // return 0 (no one wins)
|X| |O|
| | | |    // Player 1 makes a move at (2, 2).
| | |X|

ticTacToe.move(1, 1, 2); // return 0 (no one wins)
|X| |O|
| |O| |    // Player 2 makes a move at (1, 1).
| | |X|

ticTacToe.move(2, 0, 1); // return 0 (no one wins)
|X| |O|
| |O| |    // Player 1 makes a move at (2, 0).
|X| |X|

ticTacToe.move(1, 0, 2); // return 0 (no one wins)
|X| |O|
|O|O| |    // Player 2 makes a move at (1, 0).
|X| |X|

ticTacToe.move(2, 1, 1); // return 1 (player 1 wins)
|X| |O|
|O|O| |    // Player 1 makes a move at (2, 1).
|X|X|X|
```

### Constraints

* `2 <= n <= 100`
* `player` is `1` or `2`.
* `0 <= row, col < n`
* `(row, col)` are **unique** for each different call to `move`.
* At most `n^2` calls will be made to `move`.

**Follow-up:** Could you do better than `O(n^2)` per `move()` operation?

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/design_tic_tac_toe/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/design_tic_tac_toe/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class TicTacToe:
    # Time: O(n) setup — per-player row, column, and diagonal counters
    # Space: O(n) — 2n rows + 2n cols + 2 diagonals per player index
    def __init__(self, n: int) -> None:
        self.n = n
        self.rows = [[0] * n for _ in range(3)]
        self.cols = [[0] * n for _ in range(3)]
        self.diag = [0] * 3
        self.anti_diag = [0] * 3

    # Time: O(1) — increment four counters, compare against n
    # Space: O(1)
    def move(self, row: int, col: int, player: int) -> int:
        self.rows[player][row] += 1
        self.cols[player][col] += 1
        if row == col:
            self.diag[player] += 1
        if row + col == self.n - 1:
            self.anti_diag[player] += 1
        if (
            self.rows[player][row] == self.n
            or self.cols[player][col] == self.n
            or self.diag[player] == self.n
            or self.anti_diag[player] == self.n
        ):
            return player
        return 0
```

## Complexity

| Time | Space |
| - | - |
| O(n) setup — per-player row, column, and diagonal counters | O(n) — 2n rows + 2n cols + 2 diagonals per player index |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.