Tic Tac Toe Game using Python Posted by Samath 1909 January 09, 2017

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+"  |  " + sq + "  |  "  +sq+ "\n")
print("_____|_____|_____\n")
print("     |     |     ")
print("  "  + sq+"  |  "  +sq + "  |  " + sq + "\n")
print("_____|_____|_____\n" )
print("     |     |     ")
print("  " +  sq +"  |  "  +sq + "  |  " + sq + "\n")
print("     |     |     \n")
choice = input("(Player "+str(player)+") Enter a number: ")

if player == 1:
if choice == 1:
if(sq=='X' or sq=='O'):
print("Invalid Move")
else:
sq = 'X'
player = 2

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

elif choice == 3:
if(sq=='X' or sq=='O'):
print("Invalid Move")
else:
sq = 'X'
player = 2

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

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

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

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

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

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

else:
if choice == 1:
if(sq=='O' or sq=='X'):
print("Invalid Move")
else:
sq = 'O'
player = 1

elif choice == 2:
if(sq=='O' or sq=='X'):
print("Invalid Move")
else:
sq = 'O'
player = 1

elif choice == 3:
if(sq=='O' or sq=='X'):
print("Invalid Move")
else:
sq = 'O'
player = 1

elif choice == 4:
if(sq=='O' or sq=='X'):
print("Invalid Move")
else:
sq = 'O'
player = 1

elif choice == 5:
if(sq=='O' or sq=='X'):
print("Invalid Move")
else:
sq = 'O'
player = 1

elif choice == 6:
if(sq=='O' or sq=='X'):
print("Invalid Move")
else:
sq = 'O'
player = 1

elif choice == 7:
if(sq=='O' or sq=='X'):
print("Invalid Move")
else:
sq = 'O'
player = 1

elif choice == 8:
if(sq=='O' or sq=='X'):
print("Invalid Move")
else:
sq = 'O'
player = 1

elif choice == 9:
if(sq=='O' or sq=='X'):
print("Invalid Move")
else:
sq = 'O'
player = 1
else:
print("Invalid Move")

if (sq == sq  and  sq == sq):
win = 1
elif(sq == sq  and  sq == sq):
win = 1
elif (sq == sq  and  sq == sq):
win = 1
elif(sq == sq  and  sq == sq):
win = 1
elif(sq == sq  and  sq == sq):
win = 1
elif(sq == sq  and  sq == sq):
win = 1
elif(sq == sq  and  sq == sq):
win = 1
elif(sq == sq  and  sq == sq):
win = 1
elif (sq != '1'  and  sq != '2'  and  sq != '3'  and  sq != '4'  and sq != '5'  and  sq != '6'  and  sq != '7'  and  sq != '8'  and  sq != '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+"  |  " + sq + "  |  "  +sq+ "\n")
print("_____|_____|_____\n")
print("     |     |     ")
print("  "  + sq+"  |  "  +sq + "  |  " + sq + "\n")
print("_____|_____|_____\n" )
print("     |     |     ")
print("  " +  sq +"  |  "  +sq + "  |  " + sq + "\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")

```