This question evaluates the ability to validate game states by reasoning about turn counts, win detection, and termination rules, testing competencies in game-state invariants and correctness checks within the Coding & Algorithms domain.
You are given a 3×3 tic-tac-toe board state as an array of 3 strings, each of length 3. Each cell is one of:
'X'
(player X)
'O'
(player O)
'.'
(empty)
Assume players alternate turns starting with X, and the game ends immediately when a player gets 3 in a row (row, column, or diagonal). No moves can be played after the game ends.
Write a function that returns the game status for the given board:
"invalid"
if the board cannot occur from a valid sequence of moves.
"X_wins"
if X has already won in a valid way.
"O_wins"
if O has already won in a valid way.
"draw"
if the board is full and there is no winner.
"X_turn"
if the state is valid and it is X's turn to play next.
"O_turn"
if the state is valid and it is O's turn to play next.
board
:
string[3]
invalid
,
X_wins
,
O_wins
,
draw
,
X_turn
,
O_turn