Tic-Tac-Toe with Automatic Turns and Board Snapshots
DatabricksSoftware Engineer · OnsiteLast updated
Implement two-player Tic-Tac-Toe with automatic turn changes, board snapshots, rejected moves, winning lines, and full-board draws.
Implement the state transitions of a two-player Tic-Tac-Toe game. A successful move places the current player's mark, automatically switches players if play can continue, and produces a board snapshot for display. A win or a full-board draw ends the game.
Implement play_tic_tac_toe(moves: int[][]) -> string[][]. Each input row is an attempted [row, column]. Return one output row per attempt, using seven strings in this exact order:
[accepted, board_row_0, board_row_1, board_row_2, status, next_player, attempted_player]
acceptedis"1"when a mark was placed and"0"otherwise.- Each board row is a length-three string using
X,O, and.. statusisIN_PROGRESS,X_WON,O_WON, orDRAW.next_playerisXorOduring play, otherwise-.attempted_playeris the player whose turn it was before the attempt; after the game ends it is-for all further attempts.
This homogeneous string-matrix encoding makes snapshots portable across languages. A caller can print each snapshot after receiving it.
Constraints & Assumptions
- The board is 3 by 3 and initially empty.
Xstarts. - There are at most 100 attempts, each with integer coordinates from 0 through 2.
- An occupied-cell attempt is rejected without changing the board or player.
- Attempts after a terminal result are rejected without changing the final state.
- Check for a win before declaring a full board a draw.
- Every output row is an independent snapshot; later moves cannot change prior results.
- Starting-player, invalid-move, and serialization rules are explicit practice assumptions. The reported automated-player extension is a separate exercise.
Examples
moves = [[0,0],[1,1]]
result = [
["1","X..","...","...","IN_PROGRESS","O","X"],
["1","X..",".O.","...","IN_PROGRESS","X","O"]
]
moves = [[0,0],[0,0]]
result = [
["1","X..","...","...","IN_PROGRESS","O","X"],
["0","X..","...","...","IN_PROGRESS","O","O"]
]
Hints
Hint 1
Keep the transition order explicit: Separate checking whether a move is allowed, applying it, detecting a terminal result, and choosing whose turn comes next.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.