Configurable Tic-Tac-Toe Game
Learn this problemProblem statement
Implement a generalized tic-tac-toe game on an initially empty n x n board. Players X and O alternate turns, with X moving first. Process the ordered list of move attempts in moves.
A valid move places the current player's mark in an empty in-bounds cell. A player wins immediately after occupying every cell in any row, any column, the main diagonal, or the anti-diagonal. If all cells are occupied without a winner, the game ends in a draw.
Return one status for each attempted move. Return INVALID for an out-of-bounds or occupied cell; an invalid attempt does not change the board or advance the turn. Return CONTINUE after a valid nonterminal move, X_WINS or O_WINS after a winning move, and DRAW after a valid move that fills the board without a winner. After the game has ended, return GAME_OVER for every remaining attempt without changing the state.
Function
playTicTacToe(n: int, moves: int[][]) → String[]Examples
Example 1
n = 3moves = [[0,0],[1,1],[0,1],[2,2],[0,2],[2,0]]return = ["CONTINUE","CONTINUE","CONTINUE","CONTINUE","X_WINS","GAME_OVER"]X completes the top row with the fifth valid move. The final attempt occurs after the win, so its status is GAME_OVER.
Example 2
n = 2moves = [[0,0],[0,0],[1,0],[0,1],[1,1],[2,0]]return = ["CONTINUE","INVALID","CONTINUE","X_WINS","GAME_OVER","GAME_OVER"]The second attempt targets an occupied cell, so it is invalid and remains O's turn. After O moves to [1,0], X completes the top row at [0,1]. Later attempts return GAME_OVER.
Example 3
n = 3moves = [[0,0],[0,1],[0,2],[1,1],[1,0],[1,2],[2,1],[2,0],[2,2]]return = ["CONTINUE","CONTINUE","CONTINUE","CONTINUE","CONTINUE","CONTINUE","CONTINUE","CONTINUE","DRAW"]All nine cells become occupied and neither player completes a row, column, or main diagonal, so the final status is DRAW.
Constraints
1 <= n <= 1000.1 <= moves.length <= 200000.moves[i].length == 2.-10^9 <= moves[i][0], moves[i][1] <= 10^9.- Every invocation starts with an empty board and player
Xmoving first.