-
Notifications
You must be signed in to change notification settings - Fork 2
/
TicTacToe.V.3.py
145 lines (130 loc) · 2.77 KB
/
TicTacToe.V.3.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
Grid={'1':' ', '2':' ', '3':' ','4':' ','5':' ','6':' ','7':' ','8':' ','9':' '}
# print(Grid)
def s(x=1,y=1):
for i in range(5):
if x==1:
print(' ',end='')
elif x==2:
print('_',end='')
if y==1:
print('|',end='')
elif y==2:
print('')
elif y==3:
print(end='')
#printing Grid
def printTTT(a):
c=1
for v in a.values():
if c==3 or c==6:
print(' ',v,' ')
s(2)
s(2)
s(2,2)
c+=1
continue
elif c==9:
print(' ',v,' ')
s()
s()
s(1,2)
print('')
break
elif c==1 or c==4 or c==7:
s()
s()
s(1,2)
print(' ',v,' ',end='|')
c+=1
continue
print(' ',v,' ',end='|')
c+=1
#the basic structure
# | |
# -+-+-
# |X|
# -+-+-
# | |
# | |
# X | | O
# _____|_____|_____
# | |
# | |
# _____|_____|_____
# | |
# | |
# | |
#Game logic
def game():
player='O'
count=0
while True:
printTTT(Grid)
print("Enter position for ", player,'\n ')
pos=input()
if pos!='1' and pos!='2' and pos !='3' and pos!='4'and pos!='5'and pos!='6'and pos!='7'and pos!='8'and pos!='9':
print('Invalid Response,Try Again')
continue
elif Grid[pos]==' ':
Grid[pos]=player
count+=1
else:
print("Position already taken by ", Grid[pos], "Try another position")
continue
if count>=5:
if Grid['1']==Grid['2']==Grid['3']!=' ':
printTTT(Grid)
print('',player, "Has Won!!")
break
elif Grid['4']==Grid['5']==Grid['6']!=' ':
printTTT(Grid)
print('',player, "Has Won!!")
break
elif Grid['7']==Grid['8']==Grid['9']!=' ':
printTTT(Grid)
print('',player, "Has Won!!")
break
elif Grid['1']==Grid['5']==Grid['9']!=' ':
printTTT(Grid)
print('',player, "Has Won!!")
break
elif Grid['3']==Grid['5']==Grid['7']!=' ':
printTTT(Grid)
print('',player, "Has Won!!")
break
elif Grid['1']==Grid['4']==Grid['7']!=' ':
printTTT(Grid)
print('',player, "Has Won!!")
break
elif Grid['5']==Grid['2']==Grid['8']!=' ':
printTTT(Grid)
print('',player, "Has Won!!")
break
elif Grid['3']==Grid['6']==Grid['9']!=' ':
printTTT(Grid)
print('',player, "Has Won!!")
break
if count>8:
printTTT(Grid)
print(" ****TIE****")
break
if player=='O':
player='X'
else:
player='O'
game()
print(" Game Over!\n Try Again?\n Yes(y)/No(n)")
while True:
restart=input()
if restart=='Y' or restart=='y':
for k in Grid:
Grid[k]=' '
game()
print(" Game Over!\n Try Again?\n Yes(y)/No(n)")
continue
elif restart=='n' or restart=='N':
print(" Okay, Bye!\n\n ****THE END****")
break
else :
print(" Wrong Input\n Try Again!")
continue