The Classic Tic-Tac-Toe  with Python

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.
https://www.python.org/downloads/

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

You can download the the Classic Tic-Tac-Toe.py from GitHub using the given link:

Link for the code in GitHub

Example Input:

O O X
X O X
X X O

Output:

Player O

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[0]+y[0]+z[0])
vy = list(x[1]+y[1]+z[1])
vz = list(x[2]+y[2]+z[2])

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[0]+ y[1]+z[2])
dy = list(x[2]+ y[1] + z[0])
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:

Input:
O X O
O O X
X X X
Output:
Player X

But,

Input:
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[0]+y[0]+z[0])
vy = list(x[1]+y[1]+z[1])
vz = list(x[2]+y[2]+z[2])

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.

Now,

Input:
X O X
X O X
O O X
Output:
Player X

But,

Input:
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[0]+ y[1]+z[2])
dy = list(x[2]+ y[1] + z[0])

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,

Input:
X O X
O X X
X O O
Output:
Player X

But,

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

“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.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *