-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTacToe.py
383 lines (332 loc) · 12 KB
/
TicTacToe.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
'''
Tic Tac Toe Game:
Conditional statements and loops used.
'''
# initialized the empty list as board.
lis01 = [" ", "|", " ", "|", " "]
lis02 = ["-", "-", "-", "-", "-"]
lis03 = [" ", "|", " ", "|", " "]
lis04 = ["-", "-", "-", "-", "-"]
lis05 = [" ", "|", " ", "|", " "]
# condition for strikes for 'O' and 'X'.
oStrik = ["O", "O", "O"]
xStrik = ["X", "X", "X"]
print("\n{0:*^66}\n\n".format(" TIC TAC TOE "))
flag = True
while flag:
# player01's and player02's mark taken.
player01 = input("Player one choose your mark ('O' or 'X'):").strip().upper()
if player01 == "O":
player02 = "X"
print("\nplayer01: '{}'\nplayer02: '{}'\n\n".format(player01, player02))
flag = False
elif player01 == "X":
player02 = "O"
print("\nplayer01: '{}'\nplayer02: '{}'\n\n".format(player01, player02))
flag = False
else:
print("Invalid operator entered!\n\n")
# instructions printed.
print("Instructions:\nYou have to select the places by entering the numbers.")
print("Starting from top left to the bottom like this:\n")
print("{0:2}|{1:2}|{2:2}\n----------\n{3:2}|{4:2}|{5:2}\n----------\n{6:2}|{7:2}|{8:2}\n\n".format(0,2,4,6,8,10,12,14,16))
#for keeping the check on the outer loop
limit = 0
#for player selection.
count = 1
# loop for not to exceed beyond 9.
while limit <10:
# whenever value of count equals to 1, its player01 turn.
while count == 1:
playerOneInput = input("mark your place player01: ").strip()
# check condition.
if playerOneInput.isdigit() == False:
print("Invalid input!")
else:
playerOneInput = int(playerOneInput)
# if spot exists.
if playerOneInput in (0,2,4,6,8,10,12,14,16):
# if spot between 0 to 4 then update the lis01 list
if playerOneInput >=0 and playerOneInput <=4:
# if spot is already taken.
if lis01[playerOneInput] == "X" or lis01[playerOneInput] == "O":
print("Spot already marked!")
else:
# if spot is empty then update and display list as a board.
lis01[playerOneInput] = player01
lineOne = a = lineTwo = b = lineThree = ""
for x in lis01:
lineOne += x
for x in lis02:
a += x
for x in lis03:
lineTwo += x
for x in lis04:
b += x
for x in lis05:
lineThree += x
print ("{}\n{}\n{}\n{}\n{}\n\n".format(lineOne, a, lineTwo, b, lineThree))
# winning chances set in form of lists.
win01 = [lineOne[0], lineTwo[0], lineThree[0]]
win02 = [lineOne[2], lineTwo[2], lineThree[2]]
win03 = [lineOne[4], lineTwo[4], lineThree[4]]
win04 = [lineOne[0], lineTwo[2], lineThree[4]]
win05 = [lineOne[4], lineTwo[2], lineThree[0]]
win06 = [lineOne[0], lineOne[2], lineOne[4]]
win07 = [lineTwo[0], lineTwo[2], lineTwo[4]]
win08 = [lineThree[0], lineThree[2], lineThree[4]]
# all winning chances are grouped together.
win = [lineOne[::2], lineTwo[::2], lineThree[::2], win01, win02, win03, win04, win05, win06, win07, win08]
# if 'O' strik matches the winning lists.
if oStrik in win:
print("'O' wins!\n\n")
count = 0
break
# else if 'X' strik matches the winning lists.
elif xStrik in win:
print("'X' wins!\n\n")
count = 0
break
# else if its a draw.
elif lineOne[::2].count(" ") == 0 and lineTwo[::2].count(" ") == 0 and lineThree[::2].count(" ") == 0:
print("It's a Draw!\n\n")
count = 0
# else game not over yet then goes to player02.
else:
count = 2
# if spot between 6 to 10 then update the lis03 list
elif playerOneInput >=6 and playerOneInput <=10:
# setting to lis03 actual index.
playerOneInput -=6
# checking if spot is empty.
if lis03[playerOneInput] == "X" or lis03[playerOneInput] == "O":
print("Spot already marked!")
else:
# if empty then displayed the updated list as a board.
lis03[playerOneInput] = player01
lineOne = a = lineTwo = b = lineThree = ""
for x in lis01:
lineOne += x
for x in lis02:
a += x
for x in lis03:
lineTwo += x
for x in lis04:
b += x
for x in lis05:
lineThree += x
print ("{}\n{}\n{}\n{}\n{}\n\n".format(lineOne, a, lineTwo, b, lineThree))
# winning chances set in form of lists.
win01 = [lineOne[0], lineTwo[0], lineThree[0]]
win02 = [lineOne[2], lineTwo[2], lineThree[2]]
win03 = [lineOne[4], lineTwo[4], lineThree[4]]
win04 = [lineOne[0], lineTwo[2], lineThree[4]]
win05 = [lineOne[4], lineTwo[2], lineThree[0]]
win06 = [lineOne[0], lineOne[2], lineOne[4]]
win07 = [lineTwo[0], lineTwo[2], lineTwo[4]]
win08 = [lineThree[0], lineThree[2], lineThree[4]]
# all winning chances are grouped together.
win = [lineOne[::2], lineTwo[::2], lineThree[::2], win01, win02, win03, win04, win05, win06, win07, win08]
# if 'O' strik matches the winning lists.
if oStrik in win:
print("'O' wins!\n\n")
count = 0
break
# else if 'X' strik matches the winning lists.
elif xStrik in win:
print("'X' wins!\n\n")
count = 0
break
# else if its a draw.
elif lineOne[::2].count(" ") == 0 and lineTwo[::2].count(" ") == 0 and lineThree[::2].count(" ") == 0:
print("It's a Draw!\n\n")
count = 0
# else game not over yet then goes to player02.
else:
count = 2
# if spot between 12 to 16 then update the lis05 list
elif playerOneInput >=12 and playerOneInput <=16:
# setting to lis05 actual index.
playerOneInput -=12
# checking if spot is empty.
if lis05[playerOneInput] == "X" or lis05[playerOneInput] == "O":
print("Spot already marked!")
else:
# if empty then displayed the updated list as a board.
lis05[playerOneInput] = player01
lineOne = a = lineTwo = b = lineThree = ""
for x in lis01:
lineOne += x
for x in lis02:
a += x
for x in lis03:
lineTwo += x
for x in lis04:
b += x
for x in lis05:
lineThree += x
print ("{}\n{}\n{}\n{}\n{}\n\n".format(lineOne, a, lineTwo, b, lineThree))
# winning chances set in form of lists.
win01 = [lineOne[0], lineTwo[0], lineThree[0]]
win02 = [lineOne[2], lineTwo[2], lineThree[2]]
win03 = [lineOne[4], lineTwo[4], lineThree[4]]
win04 = [lineOne[0], lineTwo[2], lineThree[4]]
win05 = [lineOne[4], lineTwo[2], lineThree[0]]
win06 = [lineOne[0], lineOne[2], lineOne[4]]
win07 = [lineTwo[0], lineTwo[2], lineTwo[4]]
win08 = [lineThree[0], lineThree[2], lineThree[4]]
# all winning chances are grouped together.
win = [lineOne[::2], lineTwo[::2], lineThree[::2], win01, win02, win03, win04, win05, win06, win07, win08]
# if 'O' strik matches the winning lists.
if oStrik in win:
print("'O' wins!\n\n")
count = 0
break
# else if 'X' strik matches the winning lists.
elif xStrik in win:
print("'X' wins!\n\n")
count = 0
break
# else if its a draw.
elif lineOne[::2].count(" ") == 0 and lineTwo[::2].count(" ") == 0 and lineThree[::2].count(" ") == 0:
print("It's a Draw!\n\n")
count = 0
# else game not over yet then goes to player02.
else:
count = 2
# spot not on the board.
else:
print("Invalid spot!\n\n")
'''
if count = 2 then its player02 turn.
Aproach for player02, is same as of player01.
'''
while count == 2:
playerTwoInput = input("mark your place player02: ").strip()
if playerTwoInput.isdigit() == False:
print("Invalid input!")
else:
playerTwoInput = int(playerTwoInput)
if playerTwoInput in (0,2,4,6,8,10,12,14,16):
if playerTwoInput >=0 and playerTwoInput <=4:
if lis01[playerTwoInput] == "X" or lis01[playerTwoInput] == "O":
print("Spot already marked!")
else:
lis01[playerTwoInput] = player02
lineOne = a = lineTwo = b = lineThree = ""
for x in lis01:
lineOne += x
for x in lis02:
a += x
for x in lis03:
lineTwo += x
for x in lis04:
b += x
for x in lis05:
lineThree += x
print ("{}\n{}\n{}\n{}\n{}\n\n".format(lineOne, a, lineTwo, b, lineThree))
win01 = [lineOne[0], lineTwo[0], lineThree[0]]
win02 = [lineOne[2], lineTwo[2], lineThree[2]]
win03 = [lineOne[4], lineTwo[4], lineThree[4]]
win04 = [lineOne[0], lineTwo[2], lineThree[4]]
win05 = [lineOne[4], lineTwo[2], lineThree[0]]
win06 = [lineOne[0], lineOne[2], lineOne[4]]
win07 = [lineTwo[0], lineTwo[2], lineTwo[4]]
win08 = [lineThree[0], lineThree[2], lineThree[4]]
win = [lineOne[::2], lineTwo[::2], lineThree[::2], win01, win02, win03, win04, win05, win06, win07, win08]
if oStrik in win:
print("'O' wins!\n\n")
count = 0
break
elif xStrik in win:
print("'X' wins!\n\n")
count = 0
break
elif lineOne[::2].count(" ") == 0 and lineTwo[::2].count(" ") == 0 and lineThree[::2].count(" ") == 0:
print("It's a Draw!\n\n")
count = 0
else:
count = 1
elif playerTwoInput >=6 and playerTwoInput <=10:
playerTwoInput -=6
if lis03[playerTwoInput] == "X" or lis03[playerTwoInput] == "O":
print("Spot already marked!")
else:
lis03[playerTwoInput] = player02
lineOne = a = lineTwo = b = lineThree = ""
for x in lis01:
lineOne += x
for x in lis02:
a += x
for x in lis03:
lineTwo += x
for x in lis04:
b += x
for x in lis05:
lineThree += x
print ("{}\n{}\n{}\n{}\n{}\n\n".format(lineOne, a, lineTwo, b, lineThree))
win01 = [lineOne[0], lineTwo[0], lineThree[0]]
win02 = [lineOne[2], lineTwo[2], lineThree[2]]
win03 = [lineOne[4], lineTwo[4], lineThree[4]]
win04 = [lineOne[0], lineTwo[2], lineThree[4]]
win05 = [lineOne[4], lineTwo[2], lineThree[0]]
win06 = [lineOne[0], lineOne[2], lineOne[4]]
win07 = [lineTwo[0], lineTwo[2], lineTwo[4]]
win08 = [lineThree[0], lineThree[2], lineThree[4]]
win = [lineOne[::2], lineTwo[::2], lineThree[::2], win01, win02, win03, win04, win05, win06, win07, win08]
if oStrik in win:
print("'O' wins!\n\n")
count = 0
break
elif xStrik in win:
print("'X' wins!\n\n")
count = 0
break
elif lineOne[::2].count(" ") == 0 and lineTwo[::2].count(" ") == 0 and lineThree[::2].count(" ") == 0:
print("It's a Draw!\n\n")
count = 0
else:
count = 1
elif playerTwoInput >=12 and playerTwoInput <=16:
playerTwoInput -=12
if lis05[playerTwoInput] == "X" or lis05[playerTwoInput] == "O":
print("Spot already marked!")
else:
lis05[playerTwoInput] = player02
lineOne = a = lineTwo = b = lineThree = ""
for x in lis01:
lineOne += x
for x in lis02:
a += x
for x in lis03:
lineTwo += x
for x in lis04:
b += x
for x in lis05:
lineThree += x
print ("{}\n{}\n{}\n{}\n{}\n\n".format(lineOne, a, lineTwo, b, lineThree))
win01 = [lineOne[0], lineTwo[0], lineThree[0]]
win02 = [lineOne[2], lineTwo[2], lineThree[2]]
win03 = [lineOne[4], lineTwo[4], lineThree[4]]
win04 = [lineOne[0], lineTwo[2], lineThree[4]]
win05 = [lineOne[4], lineTwo[2], lineThree[0]]
win06 = [lineOne[0], lineOne[2], lineOne[4]]
win07 = [lineTwo[0], lineTwo[2], lineTwo[4]]
win08 = [lineThree[0], lineThree[2], lineThree[4]]
win = [lineOne[::2], lineTwo[::2], lineThree[::2], win01, win02, win03, win04, win05, win06, win07, win08]
if oStrik in win:
print("'O' wins!\n\n")
count = 0
break
elif xStrik in win:
print("'X' wins!\n\n")
count = 0
break
elif lineOne[::2].count(" ") == 0 and lineTwo[::2].count(" ") == 0 and lineThree[::2].count(" ") == 0:
print("It's a Draw!\n\n")
count = 0
else:
count = 1
else:
print("Invalid spot!\n\n")
limit +=1