Two players take turns playing Connect Tic-Tac-Toe, a gravity variant of tic-tac-toe, on a 3 x 3 grid. Player A always goes first and places X, and player B places O. Players alternate placing their symbols.
Unlike standard tic-tac-toe, this variant has gravity: every symbol falls as far down its column as it can, settling on the bottom of the grid or on top of another symbol. A move chooses a column, and the symbol lands in the lowest empty cell of that column. Because every symbol lands as low as it can, no column ever has a gap.
Given an array moves where each moves[i] = [row_i, col_i] describes the intended cell for the i-th move (player A on even indices, player B on odd indices), simulate the game with gravity and return:
-
"A"
if player A wins (three
X
symbols in a row, column, or diagonal),
-
"B"
if player B wins (three
O
symbols in a row, column, or diagonal),
-
"Draw"
if all 9 cells are filled with no winner,
-
"Pending"
if the game has not finished and no winner has been determined.
Because of gravity, the row_i value in moves[i] is ignored for placement: the piece is dropped into column col_i and lands on the lowest available row of that column. Row 0 is the top row and row 2 is the bottom row.
Function Signature
def gravity_tic_tac_toe_winner(moves: list[list[int]]) -> str:
Rules
-
Move
i
belongs to player A (
X
) when
i
is even and to player B (
O
) when
i
is odd, whatever happened on earlier moves.
-
If column
col_i
is already full, the move places nothing and is skipped. That player's turn is used up, and the next move still belongs to the player given by its own index.
-
The game ends as soon as a player has three of their symbols in a line. That player is the answer, and any later moves are ignored.
-
If nobody has won after all moves, return
"Draw"
when all 9 cells are filled and
"Pending"
otherwise.
Constraints
-
1 <= len(moves) <= 30
-
len(moves[i]) == 2
-
0 <= row_i, col_i <= 2
-
All moves are within a 3 x 3 grid.
Examples
Input: moves = [[0,0],[0,1],[0,0],[0,1],[0,0]]
Output: "A"
Final board:
X . .
X O .
X O .
A drops three pieces into column 0 and B drops two into column 1. A's pieces stack up to fill column 0, so A wins on the fifth move.
Input: moves = [[0,0],[0,1],[0,2],[0,0],[0,1],[0,2]]
Output: "Pending"
Final board:
. . .
O X O
X O X
A plays columns 0, 2 and 1, and B plays columns 1, 0 and 2. No line holds three matching symbols and three cells are still empty.
Input: moves = [[0,0],[0,0],[0,0],[0,0],[0,1],[0,1],[0,2]]
Output: "A"
Final board:
X . .
O O .
X X X
Column 0 is full after the first three moves, so B's move at index 3 is skipped. A plays column 1 at index 4, B stacks on top of it at index 5, and A completes the bottom row at index 6.