Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Tic-Tac-Toe Winner
EASY
20 mins
11 upvotes
Arrays
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Arrays
-
-
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Linked List
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Tic-Tac-Toe Winner

Contributed by
Vishal Modani
Easy
yellow-spark
0/40
Avg time to solve 20 mins
Success Rate 80 %
Share
11 upvotes

Problem Statement

Two players, named ‘player1’ and ‘player2’, play a tic-tac-toe game on a grid of size ‘3 x 3’. Given an array ‘moves’ of size ‘n’, where each element of the array is a tuple of the form (row, column) representing a position on the grid. Players place their characters alternatively in the sequence of positions given in ‘moves’. Consider that ‘player1’ makes the first move. Your task is to return the winner of the game, i.e., the winning player’s name. If there is no winner and some positions remain unmarked, return ‘uncertain’. Otherwise, the game ends in a draw, i.e., when all positions are marked without any winner, return ‘draw’.

The rules of tic-tac-toe are as follows :

1. At the start of the game, all grid positions are empty.

2. The players take turns to place their characters alternatively into empty positions. ‘player1’ always places character ‘X’ and ‘player2’ always places character ‘O’.

3. A player will never place characters into filled positions.

4. The game ends when all the positions are filled.

5. The game also ends when any row, column, or diagonal contains three same characters (i.e., either ‘X’ or ‘O’). In this case, the winner is the player whose character occupies these three positions.

6. Once the game ends, no more moves are played.

Example :

n = 5, moves = {{0,2}, {0,0}, {1,1}, {2,2}, {2,0}}

example1

First, an ‘X’ is placed at (0,2) by ‘player1’, then an ‘O’ at (0,0) by ‘player2’ and so on. After performing the given five moves, the grid contains three ‘X’s along a diagonal; hence the winner is ‘player1’. So the answer is ‘player1’.

Note :

1. The array ‘moves’ doesn’t contain any repeating positions, and all positions are valid.
2. The array ‘moves’ follows all the rules of tic-tac-toe.
3. You do not need to print anything; it has already been taken care of. Just implement the function
Detailed explanation ( Input/output format, Notes, Constraints, Images )

Sample input 1 :

2
9
0 0
0 1
1 1
1 0
2 1
2 2
2 0
0 2
1 2
5
0 0
1 1
0 2
2 2
2 1

Sample output 1 :

draw
uncertain
Explanation of sample input 1 :
Test Case 1:

example2

There is no row, column, or diagonal with three same characters after performing the given nine moves. Hence there is no winner. With no more moves to make (all grid positions are marked), the game ends in a draw. So, the answer is ‘draw’.

Test Case 2:

example3

There is no row, column, or diagonal with three same characters after performing the given five moves. Hence there is no winner as of now. With four grid positions remaining unmarked, the winner of the game is uncertain. So, the answer is ‘uncertain’.

Sample input 2 :

2
6
0 0
1 1
2 2
0 2
1 0
2 0
5
0 1
1 2
2 1
1 0
1 1

Sample output 2 :

player2
player1
Reset Code
Full screen
Auto
copy-code
Console