-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTic_Tac_Toe.py
73 lines (65 loc) · 2.21 KB
/
Tic_Tac_Toe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#function tp print board
def printboard(x):
print("{}|{}|{}".format(x[1],x[2],x[3]))
print("_|_|_")
print("{}|{}|{}".format(x[4],x[5],x[6]))
print("_|_|_")
print("{}|{}|{}".format(x[7],x[8],x[9]))
#Function to check whether any player wins
def checkwin(x):
return x[1]==x[2]==x[3]!=' ' or x[4]==x[5]==x[6]!=' ' or x[7]==x[8]==x[9]!=' '\
or x[1]==x[4]==x[7]!=' ' or x[5]==x[2]==x[8]!=' ' or x[9]==x[6]==x[3]!=' '\
or x[1]==x[5]==x[9]!=' ' or x[7]==x[5]==x[3]!=' '
#function to ask repeat game again
def ask():
var=input("Do u want to continue y/n: ")
if var=='y' or var=='Y':
tictac()
#main tictactoe code
def tictac():
print('Indices are from 1 to 9 from top to bottom')
iterate=0
x=[]
for i in range(0,10):
x.append(' ')
printboard(x)
p_1=input('what are u(player1) want to start with? Hint:enter X or O :')
while (p_1 !='x' and p_1 !='X' and p_1 !='o' and p_1 !='O'):
p_1=input('Invalid! enter again ')
if p_1=='X' or p_1=='x':
p_2='O'
else:
p_2='X'
win=False
iterate+=1
while not win and iterate!=10:
if iterate%2==1:
index1=int(input('player1:where u want to enter? '))
while index1<1 or index1>9 :
index1=int(input("Invalid index! enter again: "))
while x[index1]!=' ' :
index1=int(input("Invalid! enter again: "))
x[index1]=p_1
printboard(x)
if checkwin(x):
win=True
print ("congrats! player1 wins")
ask()
else:
index2=int(input('player2:where u want to enter? '))
while index2<1 or index2>9 :
index2=int(input("Invalid index! enter again: "))
while x[index2]!=' ' :
index2=int(input("Invalid! enter again: "))
x[index2]=p_2
printboard(x)
if checkwin(x):
win=True
print ("congrats! player2 wins")
ask()
iterate+=1
if (iterate==10 and win==False):
print("match draws!")
ask()
#call to tictac function
tictac()