Tic Tac Toe Game using Python
Posted by Samath
Last Updated: January 17, 2024

This is a Tic Tac Toe game written using the python programming language. This is a Tic Tac Toe game in which two players alternately put Xs and Os in compartments of a figure formed by two vertical lines crossing two horizontal lines and each tries to get a row of three Xs or three Os before the opponent does.

Code:

sq = ['o','1','2','3','4','5','6','7','8','9']
player = 1
choice = 0
win = -1

while(win == -1):
    print"\tTic Toc Toe Game\n\n"
    print"Player 1 (X)   or   Player 2 (0)\n"
    print"\n"
    print("     |     |     \n" )
    print("  "  + sq[1]+"  |  " + sq[2] + "  |  "  +sq[3]+ "\n")
    print("_____|_____|_____\n")
    print("     |     |     ")
    print("  "  + sq[4]+"  |  "  +sq[5] + "  |  " + sq[6] + "\n")
    print("_____|_____|_____\n" )
    print("     |     |     ")
    print("  " +  sq[7] +"  |  "  +sq[8] + "  |  " + sq[9] + "\n")
    print("     |     |     \n")
    choice = input("(Player "+str(player)+") Enter a number: ")
	
    if player == 1:
        if choice == 1:
            if(sq[1]=='X' or sq[1]=='O'):
                print("Invalid Move")
            else:
                sq[1] = 'X'
                player = 2
	    
        elif choice == 2:
            if(sq[2]=='X' or sq[2]=='O'):
                print("Invalid Move")
            else:
                sq[2] = 'X'
                player = 2
			
        elif choice == 3: 
            if(sq[3]=='X' or sq[3]=='O'):
                print("Invalid Move")
            else:
                sq[3] = 'X'
                player = 2

        elif choice == 4:
            if(sq[4]=='X' or sq[4]=='O'):
                print("Invalid Move")
            else:
                sq[4] = 'X'
                player = 2	
			
        elif choice == 5:
            if(sq[5]=='X' or sq[5]=='O'):
                print("Invalid Move")
            else:
                sq[5] = 'X'
                player = 2		
			
        elif choice == 6:
            if(sq[6]=='X' or sq[6]=='O'):
                print("Invalid Move")
            else:
                sq[6] = 'X'
                player = 2	
			
        elif choice == 7:
            if(sq[7]=='X' or sq[7]=='O'):
                print("Invalid Move")
            else:
                sq[7] = 'X'
                player = 2

        elif choice == 8:
            if(sq[8]=='X' or sq[8]=='O'):
                print("Invalid Move")
            else:
                sq[8] = 'X'	
                player = 2
			
        elif choice == 9:
            if(sq[9]=='X' or sq[9]=='O'):
                print("Invalid Move")
            else:
                sq[9] = 'X'
                player = 2	
        else:
            print("Invalid Move")

    else:
        if choice == 1:
            if(sq[1]=='O' or sq[1]=='X'):
                print("Invalid Move")
            else:
                sq[1] = 'O'
                player = 1	
			
        elif choice == 2:
            if(sq[2]=='O' or sq[2]=='X'):
                print("Invalid Move")
            else:
                sq[2] = 'O'
                player = 1
			
        elif choice == 3:
            if(sq[3]=='O' or sq[3]=='X'):
                print("Invalid Move")
            else:
                sq[3] = 'O'	
                player = 1
		
        elif choice == 4:
            if(sq[4]=='O' or sq[4]=='X'):
                print("Invalid Move")
            else:
                sq[4] = 'O'	
                player = 1	
			
        elif choice == 5:
            if(sq[5]=='O' or sq[5]=='X'):
                print("Invalid Move")
            else:
                sq[5] = 'O'
                player = 1		
			
        elif choice == 6:
            if(sq[6]=='O' or sq[6]=='X'):
                print("Invalid Move")
            else:
                sq[6] = 'O'
                player = 1		
			
        elif choice == 7:
            if(sq[7]=='O' or sq[7]=='X'):
                print("Invalid Move")
            else:
                sq[7] = 'O'	
                player = 1	
			
        elif choice == 8:
            if(sq[8]=='O' or sq[8]=='X'):
                print("Invalid Move")
            else:
                sq[8] = 'O'	
                player = 1	
			
        elif choice == 9:
            if(sq[9]=='O' or sq[9]=='X'):
                print("Invalid Move")
            else:
                sq[9] = 'O'	
                player = 1	
        else:
            print("Invalid Move")
			
	
	
	
	
    if (sq[1] == sq[2]  and  sq[2] == sq[3]):
        win = 1
    elif(sq[4] == sq[5]  and  sq[5] == sq[6]):
        win = 1
    elif (sq[7] == sq[8]  and  sq[8] == sq[9]):
        win = 1
    elif(sq[1] == sq[4]  and  sq[4] == sq[7]):
        win = 1
    elif(sq[2] == sq[5]  and  sq[5] == sq[8]):
        win = 1
    elif(sq[3] == sq[6]  and  sq[6] == sq[9]):
        win = 1
    elif(sq[1] == sq[5]  and  sq[5] == sq[9]):
        win = 1
    elif(sq[3] == sq[5]  and  sq[5] == sq[7]):
        win = 1
    elif (sq[1] != '1'  and  sq[2] != '2'  and  sq[3] != '3'  and  sq[4] != '4'  and sq[5] != '5'  and  sq[6] != '6'  and  sq[7] != '7'  and  sq[8] != '8'  and  sq[9] != '9'):
        win = 0
    else:
        win = -1

#### End WHile


print"\tTic Toc Toe Game\n\n"
print"Player 1 (X)   or   Player 2 (0)\n"
print"\n"


print("     |     |     \n" )
print("  "  + sq[1]+"  |  " + sq[2] + "  |  "  +sq[3]+ "\n")
print("_____|_____|_____\n") 
print("     |     |     ")
print("  "  + sq[4]+"  |  "  +sq[5] + "  |  " + sq[6] + "\n")
print("_____|_____|_____\n" )
print("     |     |     ")
print("  " +  sq[7] +"  |  "  +sq[8] + "  |  " + sq[9] + "\n")
print("     |     |     \n")

       
	
if(win==1):
    if(player == 1):
        player = 2
    elif(player == 2):
        player = 1
		
    print("Player "+str(player)+" win ")	
else:
    print("Game draw")
	

 

Related Content
Tic Tac Toe Game using C#
Tic Tac Toe Game using C#
Samath | Jan 17, 2024
2 Player Tic Tac Toe Game in C++
2 Player Tic Tac Toe Game in C++
Samath | Jan 06, 2021
The Matching Game in Java
The Matching Game in Java
Samath | Mar 20, 2015
Hangman Game in C++
Hangman Game in C++
Samath | Jan 17, 2024
Hangman Game using C#
Hangman Game using C#
Samath | Jan 17, 2024
Animal Game in Java
Animal Game in Java
gideonna | Apr 13, 2016
Number Guessing Game using C++
Number Guessing Game using C++
Samath | Jan 04, 2017
Blackjack Game in C++
Blackjack Game in C++
Samath | Jan 05, 2017
Checkers Game using C++
Checkers Game using C++
Samath | Jan 17, 2024
Minesweeper Game using C++
Minesweeper Game using C++
Samath | Jan 05, 2017
Craps Dice game in C++
Craps Dice game in C++
Samath | Jan 06, 2021
Paper Strip Game using Python
Paper Strip Game using Python
Samath | Jan 17, 2024
John Conway's Game of Life using C++
John Conway's Game of Life using C++
Samath | Jan 04, 2017
C++ Rock Paper Scissors Game
C++ Rock Paper Scissors Game
Samath | Jan 05, 2017