...

/

Design Tic-Tac-Toe

Design Tic-Tac-Toe

Try to solve the Design Tic-Tac-Toe problem.

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.

Examples

Press + to interact
canvasAnimation-image
1 / 3

Understanding the problem

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps us to check if you’re solving the correct problem:

Design Tic-Tac-Toe

1

We are given the current state of a game on a 3×33 \times 3 board, showing the moves made so far, where Player 1’s moves are denoted by a cross, and Player 2’s moves are denoted by a circle.

The following moves are now made by the players:

move(0, 2, 2)
move(1, 0, 1)

What would the result be?

A)

Player 1 wins the game.

B)

Player 2 wins the game.

C)

No one wins the game.

Question 1 of 30 attempted

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Note: This puzzle relates only to the function, move(). The data structures that are set up in Constructor have been mentioned in the first card.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4

Try it yourself

Implement your solution in the following coding playground.

Press + to interact
Python
usercode > tic_tac_toe.py
class TicTacToe:
# Constructor will be used to initialize TicTacToe data members
def __init__(self, n):
# Write your code here
pass
# move will be used to play a move by a specific player and identify who
# wins at each move
def move(self, row, col, player):
# Replace this placeholder return statement with your code
return -1
Design Tic-Tac-Toe

Access this course and 1200+ top-rated courses and projects.