# Python Tic Tac Toe | Competitive Coding Questions

Python Competitive Coding Questions | Tic Tac Toe: This practice is based on Tic Tac Toe in Python programming language.
Submitted by Prerana Jain, on April 24, 2020

Question:

You will be given a completed board of Tic-Tac-Toe. The cells will be represented by 'X' and 'O'. If there are any empty cells, they will be represented by '#'. 'X' is used by Player 1 and 'O' is used by Player 2. Given the board, print Player 1 if player 1 wins, print Player 2 if player 2 wins, and print "Draw" in case of a Tie. It is guaranteed that all tic-tac-toe boards given in input are valid.

Input:

3x3 Tic Tac Toe board. 3 lines containing 3 characters each separated by spaces.

```    X  O  X
O  X  X
O  O  X
```

Output:

Print who wins in a single line.

```    Player 1
```

Explanation/Solution:

In this question we have given 3x3 Tic Tac Toe board and we have to find which player won. The player who wins has the same 3 consecutive symbols like 'X' and 'O'. Both the symbols are represented by the different players like 'X' for player 1 and 'O' for player 2. We can solve this question by making the 3x3 list by using python 3.

To solve this question, we are using Python3.

Code:

```arr = []

# taking input 3 X3 matrix
for i in range(3):
arr.append(list(input().split()))

if ((arr == arr and arr == arr and arr == 'X') or
(arr == arr and arr == arr and arr == 'X') or
(arr == arr and arr == arr and arr == 'X') or
(arr == arr and arr == arr and arr == 'X') or
(arr == arr and arr == arr and arr == 'X') or
(arr == arr and arr == arr and arr == 'X') or
(arr == arr and arr == arr and arr == 'X') or
(arr == arr and arr == arr and arr == 'X')):
print("Player 1")# player 1 win
elif((arr == arr and arr == arr and arr == 'O') or
(arr == arr and arr == arr and arr == 'O') or
(arr == arr and arr == arr and arr == 'O') or
(arr == arr and arr == arr and arr == 'O') or
(arr == arr and arr == arr and arr == 'O') or
(arr == arr and arr == arr and arr == 'O') or
(arr == arr and arr == arr and arr == 'O') or
(arr == arr and arr == arr and arr == 'O')):
print("Player 2")# player 2 win
else :
print("Draw")# Match is drawn no body win
```

Output

```RUN 1:
X X X
O O X
O O #
Player 1

RUN 2:
O X O
X O X
O # #
Player 2
```

Preparation

What's New

Top Interview Coding Problems/Challenges!