Design A tic-tac-toe game this is played between and the players on a n x N grid.
Assume the following rules:
A move is guaranteed to being valid and is placed on an empty block.
Once a winning condition is reached, no more moves is allowed.
A player succeeds in placing N of their marks in a horizontal, vertical, or diagonal row wins the game.
Example:
Given n = 3, assume that player 1 is "X" and Player 2 are "O" in the board.
TicTacToe toe = new TicTacToe (3);
Toe.move (0, 0, 1); -Returns 0 (no one wins)
| x| | |
| | | | Player 1 makes a move at (0, 0).
| | | |
Toe.move (0, 2, 2); -Returns 0 (no one wins)
| x| | o|
| | | | Player 2 makes a move at (0, 2).
| | | |
Toe.move (2, 2, 1); -Returns 0 (no one wins)
| x| | o|
| | | | Player 1 makes a move at (2, 2).
| | | X|
Toe.move (1, 1, 2); -Returns 0 (no one wins)
| x| | o|
| | o| | Player 2 makes a move at (1, 1).
| | | X|
Toe.move (2, 0, 1); -Returns 0 (no one wins)
| x| | o|
| | o| | Player 1 makes a move at (2, 0).
| x| | X|
Toe.move (1, 0, 2); -Returns 0 (no one wins)
| x| | o|
| o| o| | Player 2 makes a move at (1, 0).
| x| | X|
Toe.move (2, 1, 1); -Returns 1 (Player 1 wins)
| x| | o|
| o| o| | Player 1 makes a move at (2, 1).
| X| X| X|
Follow up:
Could do better than O (N2) per move () operation?
Hint:
Could extra space such that move () operation can is done in O (1)?
You need arrays:int rows[n], int cols[n], plus, variables:diagonal, anti_diagonal.
S
[Leetcode] Design Tic-Tac-Toe-Tic-tac-Chess game