In this article, we have built a program to find the player who wins the Tic Tac Toe with the Python code. Please install python 3.8 from the link given below to run this code.

I prefer you to use atom text editor to type your code or to save the given code:
https://atom.io/

Link for the code in GitHub

O O X
X O X
X X O

Output:

Player O
Recommended: Quadratic Equation Code in Python

Code:

x,y,z= input().split(),input().split(),input().split()
xwin,owin = 0,0
#Horizontal Wins Check
for i in x:
if x.count(i)==3 and i == 'X':
print('Player X')
xwin=1
break
elif x.count(i)==3 and i == 'O':
print('Player O')
owin = 1
break
for i in y:
if y.count(i)==3 and i == 'X':
print('Player X')
xwin=1
break
elif y.count(i)==3 and i == 'O':
print('Player O')
owin = 1
break
for i in z:
if z.count(i)==3 and i == 'X':
print('Player X')
xwin=1
break
elif z.count(i)==3 and i == 'O':
print('Player O')
owin = 1
break
#Vertical Wins Check
vx = list(x+y+z)
vy = list(x+y+z)
vz = list(x+y+z)

if xwin ==0 and owin ==0:
for i in vx:
if vx.count(i)==3 and i == 'X':
print('Player X')
xwin=1
break
elif vx.count(i)==3 and i == 'O':
print('Player O')
owin = 1
break
for i in vy:
if vy.count(i)==3 and i == 'X':
print('Player X')
xwin=1
break
elif vy.count(i)==3 and i == 'O':
print('Player O')
owin = 1
break
for i in vz:
if vz.count(i)==3 and i == 'X':
print('Player X')
xwin=1
break
elif vz.count(i)==3 and i == 'O':
print('Player O')
owin = 1
break
#Diagonal Wins Check

dx = list(x+ y+z)
dy = list(x+ y + z)
if xwin ==0 and owin ==0:
for i in dx:
if dx.count(i) == 3 and i=='X':
print('Player X')
xwin = 1
break
elif dx.count(i)==3 and i == 'O':
print('Player O')
owin = 1
break
for i in dy:
if dy.count(i)==3 and i == 'X':
print('Player X')
xwin=1
break
elif dy.count(i)==3 and i == 'O':
print('Player O')
owin = 1
break
if xwin ==0 and owin==0:
print('Tie')

Code Explanation:

In this code, we get three inputs from the user, where each character is separated

The first three For loops checks whether any Horizontal wins exists.

for i in x:
if x.count(i)==3 and i == 'X':
print('Player X')
xwin=1
break
elif x.count(i)==3 and i == 'O':
print('Player O')
owin = 1
break
for i in y:
if y.count(i)==3 and i == 'X':
print('Player X')
xwin=1
break
elif y.count(i)==3 and i == 'O':
print('Player O')
owin = 1
break
for i in z:
if z.count(i)==3 and i == 'X':
print('Player X')
xwin=1
break
elif z.count(i)==3 and i == 'O':
print('Player O')
owin = 1
break

For Example:

O X O
O O X
X X X
Player X

But,

X O X
X O X
O O X
Output:
No Output

This is because our code does not know how to deal with vertical wins.

In order to correct this, we must replace the values of x, y and z with the first, second and third values of the three inputs respectively with the three new variables vx, vy and vz.

vx = list(x+y+z)
vy = list(x+y+z)
vz = list(x+y+z)

After this we repeat our for loops again, but this time, with the three new variables.

if xwin ==0 and owin ==0:
for i in vx:
if vx.count(i)==3 and i == 'X':
print('Player X')
xwin=1
break
elif vx.count(i)==3 and i == 'O':
print('Player O')
owin = 1
break
for i in vy:
if vy.count(i)==3 and i == 'X':
print('Player X')
xwin=1
break
elif vy.count(i)==3 and i == 'O':
print('Player O')
owin = 1
break
for i in vz:
if vz.count(i)==3 and i == 'X':
print('Player X')
xwin=1
break
elif vz.count(i)==3 and i == 'O':
print('Player O')
owin = 1
break

We got an If Statement before the Loop that checks whether any player won horizontally. If they did, the following 4 loops will not get executed.

Recommended: Pythagorean Theorem Professional Calculator -Python

Now,

X O X
X O X
O O X
Player X

But,

X O X
O X X
X O O
Output:
No Output

Here, X wins Diagonally, but our code does not do well with Diagonal wins. At this point we declare variable dx that takes the first value of x, second value of y and third value of z , and variable dy that takes third value of x, second value of y and first value of z.

dx = list(x+ y+z)
dy = list(x+ y + z)

Now again we bring our for loops with the same condition.

if xwin ==0 and owin ==0:
for i in dx:
if dx.count(i) == 3 and i=='X':
print('Player X')
xwin = 1
break
elif dx.count(i)==3 and i == 'O':
print('Player O')
owin = 1
break
for i in dy:
if dy.count(i)==3 and i == 'X':
print('Player X')
xwin=1
break
elif dy.count(i)==3 and i == 'O':
print('Player O')
owin = 1
break

This time,

X O X
O X X
X O O
Player X

But,

O X O
X X O
O O X
No Output
Recommended: Hofstadter Q-Sequence in Python

“No Output” does not look good for our code, hence bring a condition where, if nobody wins, the code prints out “Tie”.

if xwin ==0 and owin==0:
print('Tie')

And now, our code works perfectly fine.

Conclusion:

The above article explains the Tic-Tac-Toe using python coding. If you want to download the code from GitHub, click here. If you have any questions or feedback on this, feel free to post in the comments section below.