Find Winner on a Tic Tac Toe Game
Real-world version: Who Comes Out Ahead on the Three-by-ThreeThe statement here is our own: the same inputs, outputs and technique.
Two players take turns marking cells of a three-by-three board, the first player writing "A" and the second "B". The plays are given in order as moves, each [row, column], with the first entry being the first player's opening mark.
A player wins the moment they hold all three cells of any row, any column, or either of the two long diagonals, and the game stops there.
Return "A" or "B" for the winner, "Draw" when the board fills with no winner, or "Pending" when the game is unfinished.
Examples
Example 1
- Input
- moves = [[0, 0], [1, 0], [0, 1], [1, 1], [0, 2]]
- Output
- "A"
The first player takes the whole top row on their third mark, which wins.
Example 2
- Input
- moves = [[0, 0], [0, 1], [1, 1], [0, 2], [2, 2]]
- Output
- "A"
The first player's three marks run down the long diagonal from the top left.
Example 3
- Input
- moves = [[0, 0], [1, 1]]
- Output
- "Pending"
Only two marks have been made, so the game is still going.
Constraints
- 1 <= moves.length <= 9
- moves[i].length == 2
- 0 <= moves[i][0] <= 2
- 0 <= moves[i][1] <= 2
- No cell is played twice
- The plays follow the rules of the game
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def tictactoe(moves: list[list[int]]) -> str:public String tictactoe(int[][] moves)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's