How to make tic tac toe game with Computer moves in python ?

In this post we shall develop a program to play the Tic Tac Toe game using the Python programming language.
The status of the pieces is stored in a list where 0 means empty, 1 for player 1 and 2 for player 2. The list is defined as: l=[[0,0,0],[0,0,0],[0,0,0]]

We have developed a function printBoard that prints the current state of the board.

Next, we have a function gameStatus which chekc the current status of the game after each move. A return value of 0 means the game should continue, 1 if player one wins, 2 if player 2 wins and 3 if the game is a stalemate.

The readMove functions takes 2 inputs from the player in y,x coordinates and returns the values if correct else demands the values once again.
Finally, we have a function computerMove that simply generates a random move from the computer in case you wish to play with the computer. This function will be developed further in the next post.

The rest of the program is an infinite while loop that gives chances to each player by turn, checks for the result and conducts the game.

import random as rnd
l=[[0,0,0],[0,0,0],[0,0,0]]
def printBoard():
    
    for i in range (0,3):
       print(l[i])

def gameStatus():
    p1=0
    p2=0
    blockedlines=0
# 0 fro continue, 1 for player 1, 2 for player 2, 3 for blocked
#Lop for line 0 begins
    for i in range(0,3):
        if(l[0][i]==1):    
            p1=p1+1
        if(l[0][i]==2):
            p2=p2+1 
        if(p1==3):
            return 1
        if(p2==3):
            return 2
    if(p1>0 and p2>0):
        blockedlines=blockedlines+1
        
#Lop for line 1 begins       
    p1=0
    p2=0
    for i in range(0,3):
        if(l[1][i]==1):
            p1=p1+1
        elif(l[1][i]==2):
            p2=p2+1
    if(p1==3):
        return 1
    elif(p2==3):
        return 2
    if(p1>0 and p2>0):
        blockedlines=blockedlines+1

#Lop for line 2 begins
    p1=0
    p2=0
    for i in range(0,3):
        if(l[2][i]==1):
            p1=p1+1
        elif(l[2][i]==2):
            p2=p2+1
    if(p1==3):
        return 1
    elif(p2==3):
        return 2
    if(p1>0 and p2>0):
        blockedlines = blockedlines+1
        
#Loop for line 1 begins
    p1=0
    p2=0
    for i in range(0,3):
        if(l[i][0]==1):
            p1=p1+1
        elif(l[i][0]==3):
            p2=p2+1
    if(p1==3):
        return 1
    elif(p2==3):
        return 2
    if(p1>0 and p2>0):
        blockedlines = blockedlines+1

#Loop for line 2 begins
    p1=0
    p2=0
    for i in range(0,3):
        if(l[i][1]==1):
            p1=p1+1
        elif(l[i][1]==2):
            p2=p2+1
    if(p1==3):
        return 1
    elif(p2==3):
        return 2
    if(p1>0 and p2>0):
        blockedlines=blockedlines+1
        
#Loop for line 3 begins
    p1=0
    p2=0
    for i in range(0,3):
        if(l[i][2]==1):
            p1=p1+1
        elif(l[i][2]==2):
            p2=p2+1
    if(p1==3):
        return 1
    elif(p2==3):
        return 2
    if(p1>0 and p2>0):
        blockedlines=blockedlines+1
        
#loop for left to right digonal
    p1=0
    p2=0
    for i in range(0,3):
        if(l[i][i]==1):
            p1=p1+1
        elif(l[i][i]==2):
            p2=p2+1
    if(p1==3):
        return 1
    elif(p2==3):
        return 2
    if(p1>0 and p2>0):
        blockedlines=blockedlines+1
        
#loop for right to left digonal  
        
    p1=0
    p2=0
    for i in range(0,3):
        if(l[i][2-i]==1):
            p1=p1+1
        elif(l[i][2-i]==2):
            p2=p2+1
    if(p1==3):
        return 1
    elif(p2==3):
        return 2
    if(p1>0 and p2>0):
        blockedlines=blockedlines+1
    if(blockedlines>=8):
        return 3
    return 0
        
     
def readmove(playername):
    while(True):
        print(playername," move")
        if(playername=="Computer"):
            y,x=computerMove()
        else:
            y=int(input("Y="))
            x=int(input("X="))
        y=y-1
        x=x-1
        if(x<0 or x>2 or y<0 or y>2):
            continue
        if(l[y][x]!=0):
            continue;
        return y,x
def computerMove():
    y=rnd.randint(1,3)
    x=rnd.randint(1,3)
    return y,x
playerone=input("Player One = ")
playertwo=input("Player Two = ")
while(True):
    print(computerMove())
    printBoard()
    y,x=readmove(playerone)
    l[y][x]=1
    printBoard()
    result=gameStatus()
    if(result==1):
        print(playerone," wins")
        break
    if(result==3):
        print("Draw")
        break;
    y,x=readmove(playertwo)
    l[y][x]=2
    result=gameStatus()
    if(result==2):
        print(playertwo,"wins")
        break
    if(result==3):
        print("Draw")
        break
    

Output of this game When Two player play this game

Player One = sonu

Player Two = monu
(3, 3)
[0, 0, 0]
[0, 0, 0]
[0, 0, 0]
sonu  move

Y=1

X=1
[1, 0, 0]
[0, 0, 0]
[0, 0, 0]
monu  move

Y=1

X=3
(3, 1)
[1, 0, 2]
[0, 0, 0]
[0, 0, 0]
sonu  move

Y=3

X=1
[1, 0, 2]
[0, 0, 0]
[1, 0, 0]
monu  move

Y=2

X=1
(3, 3)
[1, 0, 2]
[2, 0, 0]
[1, 0, 0]
sonu  move

Y=3

X=3
[1, 0, 2]
[2, 0, 0]
[1, 0, 1]
monu  move

Y=2

X=2
(3, 2)
[1, 0, 2]
[2, 2, 0]
[1, 0, 1]
sonu  move

Y=3

X=2
[1, 0, 2]
[2, 2, 0]
[1, 1, 1]
sonu  wins

Output of this game when player play with Computer

Player One = Computer

Player Two = sonu
(3, 3)
[0, 0, 0]
[0, 0, 0]
[0, 0, 0]
Computer  move
[0, 0, 0]
[0, 0, 0]
[1, 0, 0]
sonu  move

Y=1

X=3
(3, 2)
[0, 0, 2]
[0, 0, 0]
[1, 0, 0]
Computer  move
[0, 1, 2]
[0, 0, 0]
[1, 0, 0]
sonu  move

Y=3

X=3
(2, 2)
[0, 1, 2]
[0, 0, 0]
[1, 0, 2]
Computer  move
[0, 1, 2]
[0, 1, 0]
[1, 0, 2]
sonu  move

Y=3

X=2
(3, 1)
[0, 1, 2]
[0, 1, 0]
[1, 2, 2]
Computer  move
[0, 1, 2]
[1, 1, 0]
[1, 2, 2]
sonu  move

Y=2

X=3
sonu wins

Leave a Reply