Tic Tac Toe

#First we need a matrix on which the symbols X 
#and O will be placed
matrix=[['*','*','*'],['*','*','*'],['*','*','*']]

#Then create 2 players namely p1 and p2 and assign respective symbols
p1='x'
p2='o'

#Now we will display the empty matrix
#Create a function print_board
def print_board(matrix):
    for row in matrix:
        row="   |   ".join(row)
        row=row.strip()
        row=row+"\n"
        print(row)

#Another function to enter player's choice of position to place the  #symbol
def write_symbol(row,col,symbol):
    if row<0 and row>2 or col<0 or col>2:
        print("Invalid Move")  
        return(False)
    elif matrix[row][col]!='*':
        print("Already Occupied")
        return(False)
    else:
        matrix[row][col]=symbol
        return(True)    #The symbol is placed successfully

#Function to check if the player has won after placing the symbol
#All the possible ways of winning the games is mentioned here
def check_for_win(symbol):
    if matrix[0][0]==symbol and matrix[0][1]==symbol and matrix[0][2]==symbol:   #check Row0
        return(True)
    elif matrix[1][0]==symbol and matrix[1][1]==symbol and matrix[1][2]==symbol:
        return(True)
    elif matrix[2][0]==symbol and matrix[2][1]==symbol and matrix[2][2]==symbol:
        return(True)
    elif matrix[0][0]==symbol and matrix[1][0]==symbol and matrix[2][0]==symbol:
        return(True)
    elif matrix[0][1]==symbol and matrix[1][1]==symbol and matrix[2][1]==symbol:
        return(True)
    elif matrix[0][2]==symbol and matrix[1][2]==symbol and matrix[2][2]==symbol:
        return(True)
    elif matrix[0][0]==symbol and matrix[1][1]==symbol and matrix[2][2]==symbol:
        return(True)
    elif matrix[0][2]==symbol and matrix[1][1]==symbol and matrix[2][0]==symbol:
        return(True)
    else:
        return(False)

#Function to check if the game has ended by draw
def check_draw():
    for i in matrix:    #i is row
        for j in i:   #j is column
            if (j=='*'):
                return(False
    return(True)

#Initialising win=0 and draw=0 because none of the player has won nor 
win=0
draw=0

#print_board(matrix) function is being called to begin the game 
#with the empty board
print_board(matrix)
while(win==0 and draw==0):    
#Only if there is no win and no draw the game continues
    print("Player 1")
    row=int(input("Enter row number: "))  
    #Getting input for x position for p1
    col=int(input("Enter column number: ")) 
    #Getting input for y position for p1
    if write_symbol(row,col,p1):
        if check_for_win(p1):   
        #Calling the function check_for_win for p1
            print("Player 1 wins!")
            win=1   
            #p1 has won, win=1 and the game ends
        elif check_draw():    
        #check_draw() function is called
            print("Draw")
            draw=1    
            #draw=1 and the game ends
        else:
            f=1   
            #Flag variable
            print("Player 2")
            row=int(input("Enter row number: "))  
            #Getting input for x position for p2
            col=int(input("Enter column number: ")) 
            #Getting input for y position for p2
            if write_symbol(row,col,p2):    
            #Calling the function check_for_win for p2
                if check_for_win(p2):
                    print("Player 2 wins!")
                    win=1   
                    #p2 has won, win=1 and the game ends
                elif check_draw():
                    print("Draw")
                    draw=1    
                    #draw=1 and the game ends
                else:
                    f=0   
                    #flag=1 and the game continues
            f=1   
            #flag=1 and the game ends
    print_board(matrix)   
    #Printing the final board and the game ends
print("Game Over")

Output:


If you have any doubts, feel free to post it in comment Section.






Comments

Popular posts from this blog

Guessing the Number Game

Find the final amount after Discount

Simple Arithmetic Calculator - User Defined Function