192. Design Tic Tac Toe Game


Design Tic Tac Toe Game
Create a class named TicTacGame that manages a Tic-tac-toe game played by two players on an m x m board.

The game receives moves one by one. Every move is guaranteed to be valid and is made on a currently empty cell.

Player 1 and player 2 take turns placing their marks on the board.

A player wins immediately after placing marks in all m cells of any one row, any one column, the main diagonal, or the anti-diagonal.

After a player wins, no additional moves will be made.

After each move, return the winning player number if that move causes a win. If there is no winner yet, return 0.

Constructor Signature

TicTacGame(int m)
  • Initializes an empty m x m Tic-tac-toe board.
  • m is the number of rows and columns in the board.

Method Signature

int doMove(int row, int col, int player)
  • Places the current player's mark at cell row,col.
  • row is the zero-based row index of the move.
  • col is the zero-based column index of the move.
  • player is the player making the move.
  • Returns 0 if there is no winner after this move.
  • Returns 1 if player 1 wins after this move.
  • Returns 2 if player 2 wins after this move.

Rules

  • Each move is valid.
  • Each move is played on an empty cell.
  • Rows and columns are zero-indexed.
  • A player wins by filling an entire row, column, main diagonal, or anti-diagonal.
  • Once a player wins, there will be no more calls to doMove.

Constraints

  • 1 <= m <= 200
  • 0 <= row < m
  • 0 <= col < m
  • player is either 1 or 2
  • Every doMove call uses an empty cell.
  • At most m2 calls will be made to doMove.
  • No doMove call will be made after a winner has already been returned.

Examples

Example 1

TicTacGame game = new TicTacGame(3);
game.doMove(row = 0, col = 0, player = 1) returns 0
Player 1 marks cell 0,0. No row, column, or diagonal is complete yet.

game.doMove(row = 0, col = 1, player = 2) returns 0
Player 2 marks cell 0,1. There is still no winner.

game.doMove(row = 1, col = 1, player = 1) returns 0
Player 1 marks cell 1,1. The main diagonal is not complete yet.

game.doMove(row = 0, col = 2, player = 2) returns 0
Player 2 marks cell 0,2. The first row contains marks from both players, so no one wins.

game.doMove(row = 2, col = 2, player = 1) returns 1
Player 1 now occupies cells 0,0, 1,1, and 2,2, so player 1 wins through the main diagonal.

Example 2

TicTacGame game = new TicTacGame(4);
game.doMove(row = 0, col = 3, player = 2) returns 0
game.doMove(row = 0, col = 0, player = 1) returns 0
game.doMove(row = 1, col = 3, player = 2) returns 0
game.doMove(row = 1, col = 0, player = 1) returns 0
game.doMove(row = 2, col = 3, player = 2) returns 0
game.doMove(row = 2, col = 0, player = 1) returns 0
game.doMove(row = 3, col = 3, player = 2) returns 2
Player 2 occupies every cell in column 3, so player 2 wins.

Example 3

TicTacGame game = new TicTacGame(2);
game.doMove(row = 0, col = 1, player = 1) returns 0
game.doMove(row = 0, col = 0, player = 2) returns 0
game.doMove(row = 1, col = 0, player = 1) returns 1
Player 1 occupies cells 0,1 and 1,0, so player 1 wins through the anti-diagonal.




Please use Laptop/Desktop or any other large screen to add/edit code.