SDE Path

Tic-Tac-Toe Engine

Easy

Tic-Tac-Toe Engine

An n×n tic-tac-toe engine with O(1) win detection per move — small game, real design conversation.

Requirements

  • Win = any full row, full column, main diagonal, or anti-diagonal owned by one player.
  • Moves are guaranteed valid (empty cell, in bounds, players may move in any order) and stop after a win — focus on the detection design, not input validation.
  • Target O(1) per move, not an O(n²) board scan.

API to implement

Implement the class TicTacToe. The I/O driver is already written in the starter code — it parses the command script below and calls your methods. You only implement the class.

| Command | Returns | Behavior | |---|---|---| | move <row> <col> <player> | int | Player player (1 or 2) marks cell (row, col) — 0-indexed, guaranteed empty and in-bounds. Return player if this move wins (n in a row/column/diagonal), else 0. No moves are sent after a win. |

Input format

The first line contains Q, the number of operations. The second line contains the constructor argument: n — board dimension (n×n, n ≥ 1). Each of the next Q lines is one command as listed above. String arguments contain no spaces.

Output format

For each command with a non-void return, print its result on its own line, in order. The driver does this for you — your methods just return values.

This is a design problem: the hidden tests exercise edge cases and interaction sequences, not performance tricks. Model the state cleanly and the tests will pass.

Sample cases

Example 1
Input
5
3
move 0 0 1
move 0 2 2
move 2 2 1
move 1 1 2
move 1 1 1
Expected output
0
0
0
0
1
Example 2
Input
5
3
move 0 0 1
move 1 0 2
move 0 1 1
move 1 1 2
move 0 2 1
Expected output
0
0
0
0
1