Skip to content
Onsite

Design Tic-Tac-Toe

MediumP3Coding

Design a Tic-Tac-Toe game on an n x n board between two players, 1 and 2.

Implement the TicTacToe class:

  • TicTacToe(n) creates an empty board.
  • move(row, col, player) places player player's mark at (row, col) and returns:
    • 0 if nobody has won yet,
    • 1 or 2 if that player has now completed a full row, column or diagonal of n of their marks.

Moves are always valid: the cell is empty and the game has not already been won. Each call must run in O(1) time, so do not rescan the board.

Example 1

Input
["TicTacToe","move","move","move","move","move","move","move"] [[3],[0,0,1],[0,2,2],[2,2,1],[1,1,2],[2,0,1],[1,0,2],[2,1,1]]
Output
[null,0,0,0,0,0,0,1]
Why
Player 1 fills column 1 with the last move and wins.

Constraints

  • 2 <= n <= 100
  • player is 1 or 2
  • 0 <= row, col < n
  • At most n * n calls.

Hints

Run checks the 2 visible tests, Submit all 8.