-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic tac toe.py
581 lines (291 loc) · 12 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
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
import time
import random
# welcome script
time.sleep(1.5)
print('-----------------------------------Welcome to Tic-Tac-Toe-----------------------------------')
time.sleep(0.5)
print('This game is made by Devesh\n')
# variable's
win = 0
lose = 0
X = 'X'
O = 'O'
# definitions
def win_check():
global r1, r2, r3, r4, r5, r6, r7, r8, r9
global X_win, O_win, tie
global u_name, u2_name
global win
if r1 == 'X' and r2 == 'X' and r3 == 'X':
X_win = True
elif r4 == 'X' and r5 == 'X' and r6 == 'X':
X_win = True
elif r7 == 'X' and r8 == 'X' and r9 == 'X':
X_win = True
elif r1 == 'X' and r4 == 'X' and r7 == 'X':
X_win = True
elif r2 == 'X' and r5 == 'X' and r8 == 'X':
X_win = True
elif r3 == 'X' and r6 == 'X' and r9 == 'X':
X_win = True
elif r1 == 'X' and r5 == 'X' and r9 == 'X':
X_win = True
elif r3 == 'X' and r5 == 'X' and r7 == 'X':
X_win = True
elif r1 == 'O' and r2 == 'O' and r3 == 'O':
O_win = True
elif r4 == 'O' and r5 == 'O' and r6 == 'O':
O_win = True
elif r7 == 'O' and r8 == 'O' and r9 == 'O':
O_win = True
elif r1 == 'O' and r4 == 'O' and r7 == 'O':
O_win = True
elif r2 == 'O' and r5 == 'O' and r8 == 'O':
O_win = True
elif r3 == 'O' and r6 == 'O' and r9 == 'O':
O_win = True
elif r1 == 'O' and r5 == 'O' and r9 == 'O':
O_win = True
elif r3 == 'O' and r5 == 'O' and r7 == 'O':
O_win = True
if O_win:
win = True
return print('Winner is', u2_name.upper()), win
if X_win:
win = True
return print('Winner is', u_name.upper(), '\n--Game Ends--\n'), win
if r1 != '-' and r2 != '-' and r3 != '-' and r4 != '-' and r5 != '-' and r6 != '-' and r7 != '-' and r8 != '-' and r9 != '-':
tie = True
return print('This game is a tie'), tie
def rule():
print('It seems you have some Trouble while play game, now worry i will help you.')
print('In Tic-Tac-Toe you have to make a straight line out of 3-O or 3-X')
print('For example:')
print(" X | O | X \n ---------- \n X | X | O \n ----------\n X | O | O \n")
def check_game_o():
global r1, r2, r3, r4, r5, r6, r7, r8, r9, command_2
retry_check = False
if command_2 == 1 and r1 == '-':
r1 = O
elif command_2 == 2 and r2 == '-':
r2 = O
elif command_2 == 3 and r3 == '-':
r3 = O
elif command_2 == 4 and r4 == '-':
r4 = O
elif command_2 == 5 and r5 == '-':
r5 = O
elif command_2 == 6 and r6 == '-':
r6 = O
elif command_2 == 7 and r7 == '-':
r7 = O
elif command_2 == 8 and r8 == '-':
r8 = O
elif command_2 == 9 and r9 == '-':
r9 = O
elif command_2 == 1 and r1 != '-' or command_2 == 2 and r2 != '-' or command_2 == 3 and r3 != '-' or command_2 == 4 and r4 != '-' or command_2 == 5 and r5 != '-' or command_2 == 6 and r6 != '-' or command_2 == 8 and r8 != '-' or command_2 == 7 and r7 != '-' or command_2 == 9 and r9 != '-':
while not retry_check:
command_2 = int(input("Don't be Over smart! Try a place which is available as you can not over write a "
"place: "))
check_game_o()
retry_check = True
elif command_2 > 9:
retry_check = False
while not retry_check:
command_2 = int(input("Please select a number between 1-9 ONLY: "))
check_game_o()
retry_check = True
return r1, r2, r3, r4, r5, r6, r7, r8, r9
def check_game_x():
global r1, r2, r3, r4, r5, r6, r7, r8, r9, command
retry_check = False
if command == 1 and r1 == '-':
r1 = X
elif command == 2 and r2 == '-':
r2 = X
elif command == 3 and r3 == '-':
r3 = X
elif command == 4 and r4 == '-':
r4 = X
elif command == 5 and r5 == '-':
r5 = X
elif command == 6 and r6 == '-':
r6 = X
elif command == 7 and r7 == '-':
r7 = X
elif command == 8 and r8 == '-':
r8 = X
elif command == 9 and r9 == '-':
r9 = X
elif command == 1 and r1 != '-' or command == 2 and r2 != '-' or command == 3 and r3 != '-' or command == 4 and r4 != '-' or command == 5 and r5 != '-' or command == 6 and r6 != '-' or command == 8 and r8 != '-' or command == 7 and r7 != '-' or command == 9 and r9 != '-':
while not retry_check:
command = int(input("Don't be Over smart! Try a place which is available as you can not over write a "
"place: "))
check_game_x()
retry_check = True
elif command > 9:
retry_check = False
while not retry_check:
command = int(input("Please select a number between 1-9 ONLY: "))
check_game_x()
retry_check = True
return r1, r2, r3, r4, r5, r6, r7, r8, r9
def computer_turn():
global r1, r2, r3, r4, r5, r6, r7, r8, r9, command_2
global comp_turn
comp_turn = False
while not comp_turn:
command_2 = random.randrange(1, 10)
if command_2 == 1 and r1 == '-':
r1 = O
comp_turn = True
elif command_2 == 2 and r2 == '-':
r2 = O
comp_turn = True
elif command_2 == 3 and r3 == '-':
r3 = O
comp_turn = True
elif command_2 == 4 and r4 == '-':
r4 = O
comp_turn = True
elif command_2 == 5 and r5 == '-':
r5 = O
comp_turn = True
elif command_2 == 6 and r6 == '-':
r6 = O
comp_turn = True
elif command_2 == 7 and r7 == '-':
r7 = O
comp_turn = True
elif command_2 == 8 and r8 == '-':
r8 = O
comp_turn = True
elif command_2 == 9 and r9 == '-':
r9 = O
comp_turn = True
return r1, r2, r3, r4, r5, r6, r7, r8, r9, print('I choose', command_2)
def write_log():
global u_name, u2_name, X_win, O_win
global r1, r2, r3, r4, r5, r6, r7, r8, r9
log = open('log.txt', 'a')
log.seek(0)
if X_win:
vb = u_name
elif O_win:
vb = u2_name
else:
vb = 'Tie'
log.write('\n {} vs {} | {}'.format(u_name, u2_name, vb).upper())
log.seek(0)
log.write("\n\n {} | {} | {} \n ---------- \n {} | {} | {} \n ----------\n {} | {} | {}\n\n\n".format(r1, r2, r3, r4, r5, r6, r7, r8, r9))
def read_log():
log = open('log.txt', 'r')
log.seek(0)
print('\n\n')
print(log.read(),'\n\n')
rule()
time.sleep(0.5)
print('Type * -h or help * to see how to play this Game...')
time.sleep(0.5)
while True:
r1 = '-'
r2 = '-'
r3 = '-'
r4 = '-'
r5 = '-'
r6 = '-'
r7 = '-'
r8 = '-'
r9 = '-'
u_name = 'Player 1'
u2_name = 'Player 2'
X_win = False
O_win = False
comp_turn = False
win = False
tie = False
input_1 = input('>Tic-Tac-Toe> ').lower()
if input_1 == '-h' or input_1 == 'help' or input_1 == 'options':
print('Welcome to Help box:)')
print('Here are all options and everything you can do with this Game.\n')
print('-----------------------------------------------------------------------')
print('| Options | Use |')
print('-----------------------------------------------------------------------')
print('| -h or help | To see all available options for the game.|')
print('''| About | To see information about the developer of |
| | this Game |''')
print('| Start game or New game | To start a new game. |')
print('| Log/stats/db_load | To see previous stats of the game. |')
print('| Rule | To Know rules of the game. |')
print('| Exit or Quit | To Quit/Exit the game. |')
print('-----------------------------------------------------------------------\n')
elif input_1 == 'log' or input_1 == 'logs' or input_1 == 'db_load' or input_1 == 'stats':
print('reading database...')
time.sleep(2)
read_log()
elif input_1 == 'about':
print('Hello,')
print(' My self Tic-tac-toe and I was created by Vishal.')
print('E-mail: [email protected]')
elif input_1 == 'exit' or input_1 == 'quit':
exit()
elif input_1 == 'start game' or input_1 == 'new game' or input_1 == 'game play' or input_1 == 'game start':
duo_ai = input('Would you link to play with your friend or bot(f/b): ')
if duo_ai == 'friend' or duo_ai == 'f' or duo_ai == 'my friend' or duo_ai == 'duo' or duo_ai == 'human':
game_on = True
print('Hello, I am A.I. robot and i am your umpire today.')
u_name = input('What should i call you player 1(X)? ')
u2_name = input('What should i call you player 2(O)? ')
print("let's see who wins today.")
print("let's start with the game now", u_name.upper(), 'vs', u2_name.upper(), '\n\n')
print("Type 'Rule' to see Rules.")
print(' ', 1, '|', 2, '|', 3, '\n ---------- \n', '', 4, '|', 5, '|', 6, '\n ----------\n', '', 7,
'|', 8, '|', 9, '\n')
while game_on:
command = int(input('>> Please enter the position you want to place the X to(1-9 only): '))
check_game_x()
print('\n ', r1, '|', r2, '|', r3, '\n ---------- \n', '', r4, '|', r5, '|', r6, '\n ----------\n', '',
r7, '|', r8, '|', r9, '\n')
win_check()
if win or tie:
write_log()
break
command_2 = int(input('>> Please enter the position you want to place the O to(1-9 only): '))
check_game_o()
print('\n ', r1, '|', r2, '|', r3, '\n ---------- \n', '', r4, '|', r5, '|', r6, '\n ----------\n', '',
r7, '|', r8, '|', r9, '\n')
win_check()
if win or tie:
write_log()
break
elif duo_ai == 'computer' or duo_ai == 'bot' or duo_ai == 'ai' or duo_ai == 'single' or duo_ai == 'b':
print('Hello, I am A.I. robot and i am your opponent today.')
u_name = input('What should i call you? ')
print("let's see if you can beat me.")
u2_name = 'bot'
print("let's start with the game now", u_name.upper(), '\n\n')
print("Type 'Rule' to see Rules.")
print(' ', 1, '|', 2, '|', 3, '\n ---------- \n', '', 4, '|', 5, '|', 6, '\n ----------\n', '', 7,
'|', 8, '|', 9, '\n')
game_on = True
while game_on:
command = int(input('>> Please enter the position you want to place the X to(1-9 only): '))
check_game_x()
print('\n ', r1, '|', r2, '|', r3, '\n ---------- \n', '', r4, '|', r5, '|', r6, '\n ----------\n', '',
r7, '|', r8, '|', r9, '\n')
win_check()
if win or tie:
write_log()
break
print("It's my turn now")
computer_turn()
print('\n ', r1, '|', r2, '|', r3, '\n ---------- \n', '', r4, '|', r5, '|', r6, '\n ----------\n', '',
r7, '|', r8, '|', r9, '\n')
win_check()
if win or tie:
write_log()
break
elif input_1 == 'rule' or input_1 == 'rules':
rule()
else:
print("It's seems some error occurred, please check your spellings.")