Design Tic-Tac-Toe

Understand and solve the interview question "Design Tic-Tac-Toe."

Description

Suppose that two players are playing a game of tic-tac-toe on an n x n board. They are 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 n of their marks in a horizontal, vertical, or diagonal row wins the game.

Your task is to implement a TicTacToe class, which will be used by two players to play the game and win fairly.

Given the 2D array containing the inputs for the TicTacToe object and the move function, you have to implement the TicTacToe class.

Assume that you have the following input array:

[[3], [0, 0, 1], [0, 2, 2], [2, 2, 1], [1, 1, 2], [2, 0, 1], [1, 0, 2], [2, 1, 1]]

Here, the first index of the 2D array is the input for the TicTacToe object and the rest of the indexes will be used as input for the move function.

Let’s look at the illustration of how the TicTacToe class will use the inputs given below.

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.