TIC TAC TOE GAME Program in Python
Program:
import random as rdm
import time as t
def intro() :
line = "Game created by DATA DEMONS"
for i in line:
print(i, end = '')
t.sleep(0.05)
print()
line6 = "Rules :\n1) Don't repeat numbers.\n2) No empty Enters."
for i in line6:
print(i, end = '')
t.sleep(0.03)
print()
line1 = "3) Select your position accordingly:\n1 2 3\n4 5 6\n7 8 9\n"
for i in line1:
print(i, end = '')
t.sleep(0.05)
print()
intro()
def outro():
line = "\nMATCH OVER\n\n"
for i in line:
print(i, end = '')
t.sleep(0.1)
print()
line1 = "See you soon - DATA DEMONS"
for i in line1:
print(i, end = '')
t.sleep(0.1)
print()
def players():
print()
line2 = "Player's turn"
for i in line2:
print(i, end = '')
t.sleep(0.05)
print()
def comps():
print()
line3 = "Computers's turn"
for i in line3:
print(i, end = '')
t.sleep(0.05)
print()
def player_wins():
print()
line4 = "PLAYER WON"
for i in line4:
print(i, end = '')
t.sleep(0.05)
print()
def computer_wins():
print()
line4 = "COMPUTER WON"
for i in line4:
print(i, end = '')
t.sleep(0.05)
print()
def tie():
print()
line4 = "TIE"
for i in line4:
print(i, end = '')
t.sleep(0.05)
print()
a = input("who is going to select and start first COMPUTER or PLAYER ?")
Dict = {}
List1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for i in range(1, 10):
Dict[i] = '_'
if a.upper() == "PLAYER":
c = input("X or O")
if c.lower() == 'x':
player = 'X'
comp = 'O'
while True:
players()
b = int(input("Position :"))
List1.remove(b)
Dict[b] = player
print(Dict[1], Dict[2], Dict[3])
print(Dict[4], Dict[5], Dict[6])
print(Dict[7], Dict[8], Dict[9])
if (Dict[1] == Dict[5]) and (Dict[5] == Dict[9]) and (Dict[9]) == player:
player_wins()
break
elif (Dict[3] == Dict[5]) and (Dict[5] == Dict[7]) and (Dict[7]) == player:
player_wins()
break
elif (Dict[1] == Dict[4]) and (Dict[4] == Dict[7]) and (Dict[8]) == player:
player_wins()
break
elif (Dict[3] == Dict[6]) and (Dict[6] == Dict[9]) and (Dict[9]) == player:
player_wins()
break
elif (Dict[1] == Dict[2]) and (Dict[2] == Dict[3]) and (Dict[3]) == player:
player_wins()
break
elif (Dict[4] == Dict[5]) and (Dict[5] == Dict[6]) and (Dict[6]) == player:
player_wins()
break
elif (Dict[7] == Dict[8]) and (Dict[8] == Dict[9]) and (Dict[9]) == player:
player_wins()
break
elif (Dict[2] == Dict[5]) and (Dict[5] == Dict[8]) and (Dict[8]) == player:
player_wins()
break
elif List1 == []:
tie()
break
print()
comps()
r = rdm.choice(List1)
List1.remove(r)
Dict[r] = comp
print(Dict[1], Dict[2], Dict[3])
print(Dict[4], Dict[5], Dict[6])
print(Dict[7], Dict[8], Dict[9])
if (Dict[1] == Dict[5]) and (Dict[5] == Dict[9]) and (Dict[9]) == comp:
computer_wins()
break
elif (Dict[3] == Dict[5]) and (Dict[5] == Dict[7]) and (Dict[7]) == comp:
computer_wins()
break
elif (Dict[1] == Dict[4]) and (Dict[4] == Dict[7]) and (Dict[8]) == comp:
computer_wins()
break
elif (Dict[3] == Dict[6]) and (Dict[6] == Dict[9]) and (Dict[9]) == comp:
computer_wins()
break
elif (Dict[1] == Dict[2]) and (Dict[2] == Dict[3]) and (Dict[3]) == comp:
computer_wins()
break
elif (Dict[4] == Dict[5]) and (Dict[5] == Dict[6]) and (Dict[6]) == comp:
computer_wins()
break
elif (Dict[7] == Dict[8]) and (Dict[8] == Dict[9]) and (Dict[9]) == comp:
computer_wins()
break
elif (Dict[2] == Dict[5]) and (Dict[5] == Dict[8]) and (Dict[8]) == comp:
printcomputer_wins()
break
elif List1 == []:
tie()
break
else:
player = 'O'
comp = 'X'
while True:
players()
b = int(input("Position :"))
List1.remove(b)
Dict[b] = player
print(Dict[1], Dict[2], Dict[3])
print(Dict[4], Dict[5], Dict[6])
print(Dict[7], Dict[8], Dict[9])
if (Dict[1] == Dict[5]) and (Dict[5] == Dict[9]) and (Dict[9]) == player:
player_wins()
break
elif (Dict[3] == Dict[5]) and (Dict[5] == Dict[7]) and (Dict[7]) == player:
player_wins()
break
elif (Dict[1] == Dict[4]) and (Dict[4] == Dict[7]) and (Dict[8]) == player:
player_wins()
break
elif (Dict[3] == Dict[6]) and (Dict[6] == Dict[9]) and (Dict[9]) == player:
player_wins()
break
elif (Dict[1] == Dict[2]) and (Dict[2] == Dict[3]) and (Dict[3]) == player:
player_wins()
break
elif (Dict[4] == Dict[5]) and (Dict[5] == Dict[6]) and (Dict[6]) == player:
player_wins()
break
elif (Dict[7] == Dict[8]) and (Dict[8] == Dict[9]) and (Dict[9]) == player:
player_wins()
break
elif (Dict[2] == Dict[5]) and (Dict[5] == Dict[8]) and (Dict[8]) == player:
player_wins()
break
elif List1 == []:
tie()
break
print()
comps()
r = rdm.choice(List1)
List1.remove(r)
Dict[r] = comp
print(Dict[1], Dict[2], Dict[3])
print(Dict[4], Dict[5], Dict[6])
print(Dict[7], Dict[8], Dict[9])
if (Dict[1] == Dict[5]) and (Dict[5] == Dict[9]) and (Dict[9]) == comp:
player_wins()
break
elif (Dict[3] == Dict[5]) and (Dict[5] == Dict[7]) and (Dict[7]) == comp:
player_wins()
break
elif (Dict[1] == Dict[4]) and (Dict[4] == Dict[7]) and (Dict[8]) == comp:
player_wins()
break
elif (Dict[3] == Dict[6]) and (Dict[6] == Dict[9]) and (Dict[9]) == comp:
player_wins()
break
elif (Dict[1] == Dict[2]) and (Dict[2] == Dict[3]) and (Dict[3]) == comp:
player_wins()
break
elif (Dict[4] == Dict[5]) and (Dict[5] == Dict[6]) and (Dict[6]) == comp:
player_wins()
break
elif (Dict[7] == Dict[8]) and (Dict[8] == Dict[9]) and (Dict[9]) == comp:
player_wins()
break
elif (Dict[2] == Dict[5]) and (Dict[5] == Dict[8]) and (Dict[8]) == comp:
player_wins()
break
elif List1 == []:
tie()
break
else:
while True:
comp = "X"
player = "O"
comps()
r = rdm.choice(List1)
List1.remove(r)
Dict[r] = comp
print(Dict[1], Dict[2], Dict[3])
print(Dict[4], Dict[5], Dict[6])
print(Dict[7], Dict[8], Dict[9])
if (Dict[1] == Dict[5]) and (Dict[5] == Dict[9]) and (Dict[9]) == comp:
computer_wins()
break
elif (Dict[3] == Dict[5]) and (Dict[5] == Dict[7]) and (Dict[7]) == comp:
computer_wins()
break
elif (Dict[1] == Dict[4]) and (Dict[4] == Dict[7]) and (Dict[8]) == comp:
computer_wins()
break
elif (Dict[3] == Dict[6]) and (Dict[6] == Dict[9]) and (Dict[9]) == comp:
computer_wins()
break
elif (Dict[1] == Dict[2]) and (Dict[2] == Dict[3]) and (Dict[3]) == comp:
computer_wins()
break
elif (Dict[4] == Dict[5]) and (Dict[5] == Dict[6]) and (Dict[6]) == comp:
computer_wins()
break
elif (Dict[7] == Dict[8]) and (Dict[8] == Dict[9]) and (Dict[9]) == comp:
computer_wins()
break
elif (Dict[2] == Dict[5]) and (Dict[5] == Dict[8]) and (Dict[8]) == comp:
computer_wins()
break
elif List1 == []:
tie()
break
print()
players()
b = int(input("Position :"))
List1.remove(b)
Dict[b] = player
print(Dict[1], Dict[2], Dict[3])
print(Dict[4], Dict[5], Dict[6])
print(Dict[7], Dict[8], Dict[9])
if (Dict[1] == Dict[5]) and (Dict[5] == Dict[9]) and (Dict[9]) == player:
player_wins()
break
elif (Dict[3] == Dict[5]) and (Dict[5] == Dict[7]) and (Dict[7]) == player:
player_wins()
break
elif (Dict[1] == Dict[4]) and (Dict[4] == Dict[7]) and (Dict[8]) == player:
player_wins()
break
elif (Dict[3] == Dict[6]) and (Dict[6] == Dict[9]) and (Dict[9]) == player:
player_wins()
break
elif (Dict[1] == Dict[2]) and (Dict[2] == Dict[3]) and (Dict[3]) == player:
player_wins()
break
elif (Dict[4] == Dict[5]) and (Dict[5] == Dict[6]) and (Dict[6]) == player:
player_wins()
break
elif (Dict[7] == Dict[8]) and (Dict[8] == Dict[9]) and (Dict[9]) == player:
player_wins()
break
elif (Dict[2] == Dict[5]) and (Dict[5] == Dict[8]) and (Dict[8]) == player:
player_wins()
break
elif List1 == []:
tie()
break
outro()
Programmer details : Arun Meyyappan PL
Software used: IDLE python
Version : 3.10.5
Documents and files download link
Python File(.py)
Text File(.txt)
0 Comments