-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnect4.py
More file actions
872 lines (627 loc) · 22.5 KB
/
connect4.py
File metadata and controls
872 lines (627 loc) · 22.5 KB
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
# Emmanuel A Oppong
# 13th October, 2022
import random, os, time
ROWS=6
COLS=7
# definining letters as global constant of a tuple
letters = ('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z')
# creating the main board as a list
board=[]
for row in range(ROWS):
row_list=[]
for col in range(COLS):
row_list.append(" ")
board.append(row_list)
# definining players as global constants
players=("X","O","V","H","M")
# main gameplay
def main():
num_moves=0
print_board()
start=0
if start ==0:
selection = True
#get number of players before game starts
#make a selection of number of players while selection is true and start counter still equal to 0
while selection and start ==0:
prompt = "Enter a number of players: 2-5 players allowed... "
NUM_PLAYERS = input(prompt)
s=checkNumPlayers(NUM_PLAYERS)
if s == True:
#increase start to help break from requiring the number of players loop
start+=1
# the number of players about to play the game is printed
print(NUM_PLAYERS,"players will be playing this game")
#break from selection loop by setting it to False
selection = False
else:
print("Incorrect input type. Re-enter. ")
num_players =int(NUM_PLAYERS)
#define the first turn for any number of players
first_turn = random.randint(0, num_players-1)
#SETTING (first_turn) for 2 players
if num_players ==2:
if first_turn ==0:
player1=players[0]
player2=players[1]
else:
player1=players[1]
player2=players[0]
#SETTING (first_turn) for 3 players
elif num_players ==3:
if first_turn ==0:
player1=players[0]
player2=players[1]
player3=players[2]
elif first_turn ==1:
player1=players[1]
player2=players[0]
player3=players[2]
else:
player1=players[2]
player2=players[0]
player3=players[1]
#SETTING (first_turn) for 4 players
elif num_players ==4:
if first_turn ==0:
player1=players[0]
player2=players[1]
player3=players[2]
player4=players[3]
elif first_turn ==1:
player1=players[1]
player2=players[0]
player3=players[2]
player4=players[3]
elif first_turn==2:
player1=players[2]
player2=players[0]
player3=players[1]
player4=players[3]
else:
player1=players[3]
player2=players[0]
player3=players[1]
player4=players[2]
#SETTING (first_turn) for 5 players
else:
if first_turn ==0:
player1=players[0]
player2=players[1]
player3=players[2]
player4=players[3]
player5=players[4]
elif first_turn ==1:
player1=players[1]
player2=players[0]
player3=players[2]
player4=players[3]
player5=players[4]
elif first_turn==2:
player1=players[2]
player2=players[0]
player3=players[1]
player4=players[3]
player5=players[4]
elif first_turn==3:
player1=players[3]
player2=players[0]
player3=players[1]
player4=players[2]
player5=players[4]
else:
player1=players[4]
player2=players[0]
player3=players[1]
player4=players[2]
player5=players[3]
#defining game instructions
print("Moves are the columns eg. A or B :")
# Game loop runs while this variable is still True. Game terminates if gameon becomes false at the end of the game due to a user input.
game_on = True
#play game only when no exit flag has been called
while game_on:
num_moves+=1
#how to play game for a respective number of players
#if 2 players playing
if num_players ==2:
if num_moves<2:
#printing to the screen which player goes first as obtain from the first turn
print(players[first_turn], "will go first. ")
if (num_moves-1)%2 ==0:
player_ch= player1
elif (num_moves-1)%2 == 1:
player_ch = player2
putChecker(player_ch)
update_board()
#if 3 players playing
elif num_players ==3:
if num_moves<2:
print(players[first_turn], "will go first. ")
if (num_moves-1)%3 ==0 :
player_ch = player1
elif (num_moves-1)%3 ==1:
player_ch = player2
else:
player_ch = player3
putChecker(player_ch)
update_board()
#if 4 players playing
elif num_players ==4:
if num_moves<2:
print(players[first_turn], "will go first. ")
if (num_moves-1)%4 ==0 :
player_ch = player1
elif (num_moves-1)%4 ==1:
player_ch = player2
elif (num_moves-1)%4 ==2 :
player_ch = player3
else:
player_ch = player4
putChecker(player_ch)
update_board()
#if 5 players playing
else:
if num_moves<2:
print(players[first_turn], "will go first. ")
if (num_moves-1)%5 ==0:
player_ch = player1
elif (num_moves-1)%5 ==1:
player_ch =player2
elif (num_moves-1)%5 ==2:
player_ch =player3
elif (num_moves-1)%5 ==3:
player_ch =player4
else:
player_ch =player5
putChecker(player_ch)
update_board()
#executes only if a winner has been spotted and the winnercheck function returns true and breaks the gameplay
if winnerCheck() == True:
#winner of two TWO PLAYERS
if num_players ==2:
if (num_moves-1) %2 == 0:
print('Player',player1, "won!")
#lines added to replay the game if the 2 players want to
prompt=("Do you want to play again Y or y/ N or n for yes or no... anything apart from these breaks the game ")
str = input(prompt)
s = playAgain(str)
if s == True:
if str== 'y' or str=="Y" :
#This is printed to the screen if the players key in y or Y which means they want to play again
print(' Yes you do, Lets go once again ')
#lines of code to reset the board for a new game
for row in range(ROWS):
for col in range(COLS):
board[row][col]=' '
main()
elif str=="n" or str == "N":
print(' No you dont, Goodbye! ')
#game_on sets to false by a user keying in n/N in this case breaks the game and it ends abruptly
game_on = False
else:
#game_on sets to false by a user keying in an incorrect value like just the enter key without any input or
# .... something other than y,Y,n,N in this case breaks the game and it ends abruptly
print("Sorry incorrect input type. Goodbye! ")
game_on = False
if (num_moves-1) %2 == 1:
print('Player',player2, "won!")
#lines added to replay the game if the 2 players want to
prompt=("Do you want to play again Y or y/ N or n for yes or no... anything apart from these breaks the game ")
str = input(prompt)
s = playAgain(str)
if s == True:
if str== 'y' or str=="Y" :
print(' Yes you do, Lets go once again ')
for row in range(ROWS):
for col in range(COLS):
board[row][col]=' '
main()
elif str=="n" or str == "N":
print(' No you dont, Goodbye! ')
game_on = False
else:
print("Sorry incorrect input type. Goodbye! ")
game_on = False
#winner of THREE PLAYERS
elif num_players ==3:
if (num_moves-1) %3 == 0:
print('Player',player1, "won!")
#lines added to replay the game if the players want to
prompt=("Do you want to play again Y or y/ N or n for yes or no... anything apart from these breaks the game ")
str = input(prompt)
s = playAgain(str)
if s == True:
if str== 'y' or str=="Y" :
print(' Yes you do, Lets go once again ')
for row in range(ROWS):
for col in range(COLS):
board[row][col]=' '
main()
elif str=="n" or str == "N":
print(' No you dont, Goodbye! ')
game_on = False
else:
print("Sorry incorrect input type. Goodbye! ")
game_on = False
if (num_moves-1) %3 == 1:
print('Player',player2, "won!")
#lines added to replay the game if the 2 players want to
prompt=("Do you want to play again Y or y/ N or n for yes or no... anything apart from these breaks the game ")
str = input(prompt)
s = playAgain(str)
if s == True:
if str== 'y' or str=="Y" :
print(' Yes you do, Lets go once again ')
for row in range(ROWS):
for col in range(COLS):
board[row][col]=' '
main()
elif str=="n" or str == "N":
print(' No you dont, Goodbye! ')
game_on = False
else:
print("Sorry incorrect input type. Goodbye! ")
game_on = False
if (num_moves-1) %3 == 2:
print('Player',player3, "won!")
#lines added to replay the game if the 2 players want to
prompt=("Do you want to play again Y or y/ N or n for yes or no... anything apart from these breaks the game ")
str = input(prompt)
s = playAgain(str)
if s == True:
if str== 'y' or str=="Y" :
print(' Yes you do, Lets go once again ')
for row in range(ROWS):
for col in range(COLS):
board[row][col]=' '
main()
elif str=="n" or str == "N":
print(' No you dont, Goodbye! ')
game_on = False
else:
print("Sorry incorrect input type. Goodbye! ")
game_on = False
#winner of FOUR PLAYERS
elif num_players == 4:
if (num_moves-1) %4 == 0:
print('Player',player1, "won!")
#lines added to replay the game if the players want to
prompt=("Do you want to play again Y or y/ N or n for yes or no...anything apart from these breaks the game ")
str = input(prompt)
s = playAgain(str)
if s == True:
if str== 'y' or str=="Y" :
print(' Yes you do, Lets go once again ')
for row in range(ROWS):
for col in range(COLS):
board[row][col]=' '
main()
elif str=="n" or str == "N":
print(' No you dont, Goodbye! ')
game_on = False
else:
print("Sorry incorrect input type. Goodbye! ")
game_on = False
if (num_moves-1) %4 == 1:
print('Player',player2, "won!")
#lines added to replay the game if the 2 players want to
prompt=("Do you want to play again Y or y/ N or n for yes or no... anything apart from these breaks the game ")
str = input(prompt)
s = playAgain(str)
if s == True:
if str== 'y' or str=="Y" :
print(' Yes you do, Lets go once again ')
for row in range(ROWS):
for col in range(COLS):
board[row][col]=' '
main()
elif str=="n" or str == "N":
print(' No you dont, Goodbye! ')
game_on = False
else:
print("Sorry incorrect input type. Goodbye! ")
game_on = False
if (num_moves-1) %4 == 2:
print('Player',player3, "won!")
#lines added to replay the game if the 2 players want to
prompt=("Do you want to play again Y or y/ N or n for yes or no... anything apart from these breaks the game ")
str = input(prompt)
s = playAgain(str)
if s == True:
if str== 'y' or str=="Y" :
print(' Yes you do, Lets go once again ')
for row in range(ROWS):
for col in range(COLS):
board[row][col]=' '
main()
elif str=="n" or str == "N":
print(' No you dont, Goodbye! ')
game_on = False
else:
print("Sorry incorrect input type. Goodbye! ")
game_on = False
if (num_moves-1) %4 == 3:
print('Player',player4, "won!")
#lines added to replay the game if the 2 players want to
prompt=("Do you want to play again Y or y/ N or n for yes or no...anything apart from these breaks the game ")
str = input(prompt)
s = playAgain(str)
if s == True:
if str== 'y' or str=="Y" :
print(' Yes you do, Lets go once again ')
for row in range(ROWS):
for col in range(COLS):
board[row][col]=' '
main()
elif str=="n" or str == "N":
print(' No you dont, Goodbye! ')
game_on = False
else:
print("Sorry incorrect input type. Goodbye! ")
game_on = False
#winner of FIVE PLAYERS
elif num_players ==5:
if (num_moves-1) %5 == 0: #FIVE PLAYERS
print('Player',player1, "won!")
#lines added to replay the game if the players want to
prompt=("Do you want to play again Y or y/ N or n for yes or no... anything apart from these breaks the game ")
str = input(prompt)
s = playAgain(str)
if s == True:
if str== 'y' or str=="Y" :
print(' Yes you do, Lets go once again ')
for row in range(ROWS):
for col in range(COLS):
board[row][col]=' '
main()
elif str=="n" or str == "N":
print(' No you dont, Goodbye! ')
game_on = False
else:
print("Sorry incorrect input type. Goodbye! ")
game_on = False
if (num_moves-1) %5 == 1:
print('Player',player2, "won!")
#lines added to replay the game if the 2 players want to
prompt=("Do you want to play again Y or y/ N or n for yes or no... anything apart from these breaks the game ")
str = input(prompt)
s = playAgain(str)
if s == True:
if str== 'y' or str=="Y" :
print(' Yes you do, Lets go once again ')
for row in range(ROWS):
for col in range(COLS):
board[row][col]=' '
main()
elif str=="n" or str == "N":
print(' No you dont, Goodbye! ')
game_on = False
else:
print("Sorry incorrect input type. Goodbye! ")
game_on = False
if (num_moves-1) %5 == 2:
print('Player',player3, "won!")
#lines added to replay the game if the 2 players want to
prompt=("Do you want to play again Y or y/ N or n for yes or no... anything apart from these breaks the game ")
str = input(prompt)
s = playAgain(str)
if s == True:
if str== 'y' or str=="Y" :
print(' Yes you do, Lets go once again ')
for row in range(ROWS):
for col in range(COLS):
board[row][col]=' '
main()
elif str=="n" or str == "N":
print(' No you dont, Goodbye! ')
game_on = False
else:
print("Sorry incorrect input type. Goodbye! ")
game_on = False
if (num_moves-1) %5 == 3:
print('Player',player4, "won!")
#lines added to replay the game if the 2 players want to
prompt=("Do you want to play again Y or y/ N or n for yes or no... anything apart from these breaks the game ")
str = input(prompt)
s = playAgain(str)
if s == True:
if str== 'y' or str=="Y" :
print(' Yes you do, Lets go once again ')
for row in range(ROWS):
for col in range(COLS):
board[row][col]=' '
main()
elif str=="n" or str == "N":
print(' No you dont, Goodbye! ')
game_on = False
else:
print("Sorry incorrect input type. Goodbye! ")
game_on = False
if (num_moves-1) %5 == 4:
print('Player',player5, "won!")
#lines added to replay the game if the 2 players want to
prompt=("Do you want to play again Y or y/ N or n for yes or no... anything apart from these breaks the game ")
str = input(prompt)
s = playAgain(str)
if s == True:
if str== 'y' or str=="Y" :
print(' Yes you do, Lets go once again ')
for row in range(ROWS):
for col in range(COLS):
board[row][col]=' '
main()
elif str=="n" or str == "N":
print(' No you dont, Goodbye! ')
game_on = False
else:
print("Sorry incorrect input type. Goodbye! ")
game_on = False
# winner function from above is run first then the board full function will tell if its a DRAW after the winner function gives a no outcome whatsoever
elif boardFull() == True:
print("All the columns are full with NO winner. The game ends in a Draw, WOW Brainiacs")
#lines added to replay the game if the 2 players want to
prompt=("Do you want to play again Y or y/ N or n for yes or no... anything apart from these breaks the game ")
str = input(prompt)
s = playAgain(str)
if s == True:
if str== 'y' or str=="Y" :
print(' Yes you do, Lets go once again ')
for row in range(ROWS):
for col in range(COLS):
board[row][col]=' '
main()
elif str=="n" or str == "N":
print(' No you dont, Goodbye! ')
game_on = False
else:
print("Sorry incorrect input type. Goodbye! ")
game_on = False
#function that checks if only the top row is full as checker is placed from bottom up to predict that the board is full
def boardFull():
for r in range(1):
for c in range(COLS):
if board[r][c] == " ":
return False
return True
#function that checks for a winner vertically, horizontally, positive diagonally and negative diagonally and sets winner to TRUE if it finds one such case
def winnerCheck():
#CHECK A WINNER VERTICALLY
for c in range(COLS):
for r in range(ROWS-1,2,-1):
if (board[r][c] == "X" and board[r-1][c] == "X" and board[r-2][c] == "X" and board[r-3][c] == "X") or \
(board[r][c] == "O" and board[r-1][c] == "O" and board[r-2][c] == "O" and board[r-3][c] == "O") or \
(board[r][c] == "M" and board[r-1][c] == "M" and board[r-2][c] == "M" and board[r-3][c] == "M") or \
(board[r][c] == "H" and board[r-1][c] == "H" and board[r-2][c] == "H" and board[r-3][c] == "H") or \
(board[r][c] == "V" and board[r-1][c] == "V" and board[r-2][c] == "V" and board[r-3][c] == "V"):
return True
#CHECK A WINNER HORIZONTALLY
for c in range(COLS-3):
for r in range(ROWS-1,2,-1):
if (board[r][c] == "X" and board[r][c+1] == "X" and board[r][c+2] == "X" and board[r][c+3] == "X") or \
(board[r][c] == "O" and board[r][c+1] == "O" and board[r][c+2] == "O" and board[r][c+3] == "O") or \
(board[r][c] == "M" and board[r][c+1] == "M" and board[r][c+2] == "M" and board[r][c+3] == "M") or \
(board[r][c] == "H" and board[r][c+1] == "H" and board[r][c+2] == "H" and board[r][c+3] == "H") or \
(board[r][c] == "V" and board[r][c+1] == "V" and board[r][c+2] == "V" and board[r][c+3] == "V"):
return True
#CHECK A WINNER ON POSITIVELY SLOPED DIAGONAL
for c in range(COLS-3):
for r in range(ROWS-1,2,-1):
if (board[r][c] == "X" and board[r-1][c+1] == "X" and board[r-2][c+2] == "X" and board[r-3][c+3] == "X") or \
(board[r][c] == "O" and board[r-1][c+1] == "O" and board[r-2][c+2] == "O" and board[r-3][c+3] == "O") or \
(board[r][c] == "M" and board[r-1][c+1] == "M" and board[r-2][c+2] == "M" and board[r-3][c+3] == "M") or \
(board[r][c] == "H" and board[r-1][c+1] == "H" and board[r-2][c+2] == "H" and board[r-3][c+3] == "H") or \
(board[r][c] == "V" and board[r-1][c+1] == "V" and board[r-2][c+2] == "V" and board[r-3][c+3] == "V"):
return True
#CHECK A WINNER ON NEGATIVELY SLOPED DIAGONAL
for c in range(COLS-1,2,-1):
for r in range(ROWS-1,2,-1):
if (board[r][c] == "X" and board[r-1][c-1] == "X" and board[r-2][c-2] == "X" and board[r-3][c-3] == "X") or \
(board[r][c] == "O" and board[r-1][c-1] == "O" and board[r-2][c-2] == "O" and board[r-3][c-3] == "O") or \
(board[r][c] == "M" and board[r-1][c-1] == "M" and board[r-2][c-2] == "M" and board[r-3][c-3] == "M") or \
(board[r][c] == "H" and board[r-1][c-1] == "H" and board[r-2][c-2] == "H" and board[r-3][c-3] == "H") or \
(board[r][c] == "V" and board[r-1][c-1] == "V" and board[r-2][c-2] == "V" and board[r-3][c-3] == "V"):
return True
# function that uses checkInput function before placing a checker X or O or M or V or H on the board and can tell if a column is full
def putChecker(player_ch):
#constantly accept input for the number of players
while True:
prompt='Player '+ player_ch + ',please enter a column'+': '
str = input(prompt)
# call check inputChecker function to check for correct
# ... input type else return false and print re-enter
s = checkInput(str)
if s == True:
c = (ord(str[0]) - 65)
#checks if column letter entered is on the board and if not prints whats below
if c<0 or c>=COLS:
print("Out of range. Re-enter. ")
#checks if a column full and prints whats below
elif colFull(str) == True:
print("Column", str, "is full, Enter a different column letter")
return
else:
#places checker by first looping to see if a bottom row in a column is not empty
if c==0 or c==1 or c == 2 or c==3 or c==4 or c==5 or c==6 or c == 7 or c==8 or c==9 or c==10 \
or c==11 or c==12 or c==13 or c==14 or c == 15 or c==16 or c==17 or c==18 or c==19 or c==20 \
or c==21 or c==22 or c ==23 or c==24 or c==25 or c==26 :
for r in range(ROWS-1,-1,-1):
if board[r][c] == " ":
board[r][c] = player_ch
return
# print board and break from while loop
print_board()
break
# asks user to re-enter input as checkinput failed
else:
print("Incorrect input type. Re-enter. ")
# function that checks to see if user wants to play again
def playAgain(str):
if len(str)<1 or len(str)>1:
return False
elif len(str)==1 and str[0] in ('Y','N','y','n'):
return True
elif len(str)==1 and str[0] not in ('Y','N','y','n'):
return False
else:
return False
# function that checks to see if a column is full or not
def colFull(cell):
c = (ord(cell)-65)
for r in range(0,ROWS):
if board[r][c] != " ":
return True
else:
return False
# checks user input as a correct number of players
# .... required for a gameplay
def checkNumPlayers(str):
if len(str)<1 or len(str)>1:
return False
elif len(str)==1 and str.isdigit() and 2<= int(str) <=5:
return True
else:
return False
# function that prints the 2D board
def print_board():
#prints column header letters
for i in letters[:COLS]:
if(i == 'A'):
print(' '+ i , end=' ')
else:
print(' '+ i , end=' ')
#print row numbers by side of table
print("\n +"+"---+"*COLS)
for row in range(ROWS):
print(" "+" | ", end="")
for col in range(COLS):
print(board[row][col]+" | ", end="")
print("\n +" + "---+" * COLS)
def update_board():
#sleep,clear the screen and print recently updated board
time.sleep(0.5)
os.system("clear")
print_board()
# checks user input as correct position on the board
# .... and in the correct format of capital letter for a column
def checkInput(str):
if len(str)<1 or len(str)>1:
return False
elif len(str)==1 and str[0] in letters:
return True
elif str[0] not in letters:
return False
else:
return False
# function that checks to see if user wants to play again
def playAgain(str):
if len(str)<1 or len(str)>1:
return False
elif len(str)==1 and str[0] in ('Y','N','y','n'):
return True
elif len(str)==1 and str[0] not in ('Y','N','y','n'):
return False
else:
return False
if __name__ == "__main__":
main()