Problem
Ask
Submissions

Problem: Design Tic-Tac-Toe

Medium
30 min
Discover how to implement a Tic-Tac-Toe class that manages moves and winning conditions on an n by n board. Learn to track player moves efficiently, check for winners after each move, and handle game logic constraints. This lesson helps you understand game design patterns and state tracking vital for scalable solutions.

Statement

Suppose that two players are playing a tic-tac-toe game on an n×nn \times n board. They’re following specific rules to play and win the game:

  • A move is guaranteed to be valid if a mark is placed on an empty block.
  • No more moves are allowed once a winning condition is reached.
  • A player who succeeds in placing nn of their marks in a horizontal, vertical, or diagonal row wins the game.

Implement a TicTacToe class, which will be used by two players to play the game and win fairly.

Keep in mind the following functionalities that need to be implemented:

  • Constructor, the constructor, which initializes an object of TicTacToe, allowing the players to play on a board of size n×nn \times n.
  • move(row, col, player) indicates that the player with the ID, player, places their mark on the cell (row, col). The move is guaranteed to be a valid move. At each move, this function returns the player ID if the current player wins and returns 00 if no one wins.

Constraints:

  • 2n92 \leq n \leq 9

  • player should be either 1 or 2.

  • 00 \leq row, col <n< n

  • Every call to move() will be with a unique row, col combination.

  • The move() function will be called at most n2n^2 times.

Problem
Ask
Submissions

Problem: Design Tic-Tac-Toe

Medium
30 min
Discover how to implement a Tic-Tac-Toe class that manages moves and winning conditions on an n by n board. Learn to track player moves efficiently, check for winners after each move, and handle game logic constraints. This lesson helps you understand game design patterns and state tracking vital for scalable solutions.

Statement

Suppose that two players are playing a tic-tac-toe game on an n×nn \times n board. They’re following specific rules to play and win the game:

  • A move is guaranteed to be valid if a mark is placed on an empty block.
  • No more moves are allowed once a winning condition is reached.
  • A player who succeeds in placing nn of their marks in a horizontal, vertical, or diagonal row wins the game.

Implement a TicTacToe class, which will be used by two players to play the game and win fairly.

Keep in mind the following functionalities that need to be implemented:

  • Constructor, the constructor, which initializes an object of TicTacToe, allowing the players to play on a board of size n×nn \times n.
  • move(row, col, player) indicates that the player with the ID, player, places their mark on the cell (row, col). The move is guaranteed to be a valid move. At each move, this function returns the player ID if the current player wins and returns 00 if no one wins.

Constraints:

  • 2n92 \leq n \leq 9

  • player should be either 1 or 2.

  • 00 \leq row, col <n< n

  • Every call to move() will be with a unique row, col combination.

  • The move() function will be called at most n2n^2 times.