-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnect-4.py
More file actions
984 lines (964 loc) · 52.8 KB
/
Connect-4.py
File metadata and controls
984 lines (964 loc) · 52.8 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
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
# CSCE 160 - Prof. Kim - HW3
#Names: Rares-Mihail Neagu, Tyler Fortune
#Dates:10/22/2021,10/23/2021,10/24/2021,10/26/2021
#Connect4 Game Program
#------------------IMPORTS------------------------------------
import random
#------------------FUNCTIONS----------------------------------
#--------GRAPHICS FUNCTIONS-----------------------------------
def displayBoard(Board):
#this function displays the gameboard on the screen
for j in range(7):
print("|",j,end=" ")
print("|")
print("-----------------------------")
for i in range(6):
print("-----------------------------")
for j in range(7):
print("|",Board[i][j],end=" ")
print("|")
print("-----------------------------")
#--------STATUS FUNCTIONS--------------------------------------
def StoreBoardState(B):
#this function takes in the gameboard
#this function stores the state of the gameboard in another matrix to avoid common reference
#creating a matrix to store the state
K=[0]*6
for k in range(6):
K[k]=[" "] * 7#copying element by element
#copying element by element
for i in range(0,6,1):#copying element by element
for j in range(0,7,1):
K[i][j]=B[i][j]
return K
def checkWin(B):
#this function checks if the game was won
#uses the variable k to control the starting point in the table
#thus, for columns k has the values 0,1,2,3, while for rows 0,1,2
win=False#creates a flag to hold the win information
#1.check the horizontal lines
for i in range(6):
for k in range(4):
if(B[i][k]==B[i][k+1] and B[i][k+1]==B[i][k+2] and B[i][k+2]==B[i][k+3] and B[i][k]!=" "):
win=True #if 4 horizontal tiles are the same then there is a win
break#and it breaks out of the inner loop
if(win):#after finding a win it breaks out of the outer loop
break
#2.check the vertical lines
for j in range(7):
for k in range(3):
if(B[k][j]==B[k+1][j] and B[k+1][j]==B[k+2][j] and B[k+2][j]==B[k+3][j] and B[k][j]!=" "):
win=True#if 4 vertical tiles are the same then there is a win
break#and it breaks out of the inner loop
if(win):#after finding a win it breaks out of the outer loop
break
#3.check the diagonals
#3.1 check the major diagonals
for k in range(5,2,-1):
for p in range(3,7):#uses also the variable p to control the column starting point in the table
if(B[k][p]==B[k-1][p-1] and B[k-1][p-1]==B[k-2][p-2] and B[k-2][p-2]==B[k-3][p-3] and B[k][p]!=" "):
win=True#if a 4 tiles in a diagonal are the same there is win
break#and it breaks out of the inner loop
if(win):#after finding a win it breaks out of the outer loop
break
#3.2 check the minor diagonals
for k in range(5,2,-1):
for p in range(4):#uses also the variable p to control the column starting point in the table
if(B[k][p]==B[k-1][p+1] and B[k-1][p+1]==B[k-2][p+2] and B[k-2][p+2]==B[k-3][p+3] and B[k-3][p+3]!=" "):
win=True#if a 4 tiles in a diagonal are the same there is win
break#and it breaks out of the inner loop
if(win):#after finding a win it breaks out of the outer loop
break
return win
def checkTie(B):
#this function takes in the gameboard
#it returns if there is a tie
tie=False#creates a flag to hold the tie information
if(checkWin(B)==False):#checks if there is no win
if(fullBoard(B)==True):#checks if the board is full
tie=True
return tie
def fullColumnCheck(B,choice):
#this function takes in the gameboard and the column choice of the user
#it returns if the row is full or not, in the variable "full"
full=True#create a flag and assume it is full/true
for i in range(6):
if(B[i][choice]==" "):
full=False#when a blank has been found, it becomes false
break#as soon as it finds a blank, it breaks
return full
def fullBoard(B):
#this function takes in the gameboard and checks if it's full
full=True#create a flag and assume it is full/true
#checks if all columns are full
for j in range(0,7,1):
if(fullColumnCheck(B,j)==False):
full=False
break#as soon as an empty spot was found, it breaks
return full
#------RULE-BASED FUNCTIONS--------------------------------------
def ColumnReassign(B,choice,t):
#this function takes in the gameboard, the choice, and the turn
#this function reassigns the value of the column, if it's full
#returns y, the new column
x=fullColumnCheck(B,choice)#checks if the column is full
if(x==False):
y=choice#no reassigning needed
else:#it has to reassign the column
if(t==0):#it's the player's turn
y=int(input("The column you chose is full,enter another one:"))
while(fullColumnCheck(B,y)==True):#force the player to do it
y=int(input("The column you chose is full,enter another one:"))
elif(t==1):#it's the computer's turn
y=random.randint(0,6)
while(fullColumnCheck(B,y)==True):
y=random.randint(0,6)
return y
def rowPlacement(B,choice,t):
#this function takes the column from the input,the gameboard,and the turn
#checks if the rows are empty and which one should be filled
for i in range(5,-1,-1):
if(B[i][choice]==" "):
row=i#stores the row of the box that should be filled
break#after finding the lowest empty box, it breaks
return row
#---------------AI FUNCTIONS-------------------------------------
def fourBlocking(B):
#this function takes in the gameboard
#this function allows the computer to block three-in-a-rows
#the returned variable x stores the column number
#1.blocking horizontally
found=False#creates a flag to keep track if a block has been found
for i in range(5,-1,-1):
for p in range(0,5,1):#using the variable p to control the column starting point
if(B[i][p]==B[i][p+1] and B[i][p+1]==B[i][p+2] and B[i][p]!=" " and B[i][p]=="X"):#check if there's a three-in-a-row
if(p==0 or p==4):#taking care of edgecases
if(i==5):#taking care of the row edgecase
if(B[i][3]==" "):#check if the adjacent box is empty
found=True
x=3#the index in the middle is the only blocking strategy for both edgecases
break#after finding a block, break
else:#the box is full, no blocking needed
x=-1
else:#check the other cases
if(B[i][3]==" "):#check if the adjacent box is empty
#it is empty, so it has to block
if(B[i+1][3]!=" "):#check if the box below is empty
found=True
x=3#the index in the middle is the only blocking strategy for both edgecases
break#after finding a block, break
else:#it is empty,no blocking needed
x=-1
else:#the box is full, no blocking needed
x=-1#assign to x a sentinel value if no three-in-a-row was found
else:#check the other cases
if(B[i][p-1]==" " and B[i][p+3]==" "):#check if both the adjacent boxes are empty
if(i==5):#taking care of the row edgecase
choice=random.randint(0,1)#uses the random variable to randomly block before or after
if(choice==0):#blocks at p-1
found=True
x=p-1
break#after finding a block, break
elif(choice==1):#blocks at p+3
found=True
x=p+3
break#after finding a block, break
else:#check the other cases
#check if there are pawns under boxes adjacent to the three-in-a-row
if(B[i+1][p-1]!=" " and B[i+1][p+3]!=" "):#both of them are filled
choice=random.randint(0,1)#uses the random variable to randomly block before or after
if(choice==0):#blocks at p-1
found=True
x=p-1
break#after finding a block, break
elif(choice==1):#blocks at p+3
found=True
x=p+3
break#after finding a block, break
elif(B[i+1][p-1]!=" "):#the box under p-1 is full
found=True
x=p-1
break#after finding a block, break
elif(B[i+1][p+3]!=" "):#the box under p+3 is full
found=True
x=p+3
break#after finding a block, break
else:#the adjacent boxes are empty,no blocking needed
x=-1
elif(B[i][p-1]==" "):#blocks at p-1
#check if there are pawns under boxes adjacent to the three-in-a-row
if(i==5):#taking care of row edgecase
found=True
x=p-1
break#after finding a block, break
else:#check the other cases
if(B[i+1][p-1]!=" "):#blocking needed
found=True
x=p-1
break#after finding a block, break
else:#no pawns, no blocking needed
x=-1
elif(B[i][p+3]==" "):#blocks at p+3
#check if there are pawns under boxes adjacent to the three-in-a-row
if(i==5):#taking care of row edgecase
found=True
x=p+3
break#after finding a block, break
else:#check the other cases
if(B[i+1][p+3]!=" "):#blocking needed
found=True
x=p+3
break#after finding a block, break
else:#no pawns, no blocking needed
x=-1
else:#all are full,no blocking needed
x=-1#assign to x a sentinel value if no blocking is needed
else:#there is no three-in-a-row, no blocking needed
x=-1#assign to x a sentinel value if no blocking is needed
#as soon as it founds a block, it breaks out of the outer loop
if(found==True):
break
#2.blocking vertically
if(found==False):#if no horizontal block is needed,it checks for vertical ones
for j in range(0,7,1):
for k in range(5,2,-1):#using the variable k to control the row starting point
if(B[k][j]==B[k-1][j] and B[k-1][j]==B[k-2][j] and B[k][j]!=" " and B[k][j]=="X"):#check if there's a three-in-a-row
if(B[k-3][j]==" "):#check if the box above is empty
found=True
x=j
break#after finding a block, break
else:
x=-1#assign to x a sentinel value if no blocking is needed
else:#there is no three-in-a-row, no blocking needed
x=-1#assign to x a sentinel value if no blocking is needed
#as soon as it founds a block, it breaks out of the outer loop
if(found==True):
break
#3.blocking diagonally
#3.1 blocking major diagonals
if(found==False):
for k in range(5,2,-1):#again, using k and p to control the starting points
for p in range(3,7,1):
#check if there's a three-in-a-row
if(B[k-2][p-2]==B[k-1][p-1] and B[k-1][p-1]==B[k][p] and B[k][p]!=" "):
if(B[k-3][p-3]==" "):#check if the fourth box is empty or not
#check if the boxes under the fourth are full
full=True#create a flag to see if the three boxes under are full
for i in range(5,k-2,-1):
if(B[i][p-3]==" "):
full=False
break#as soon as it finds one empty,it breaks
if(full==True):
found=True
x=p-3
break#after finding a block, break
else:
x=-1#assign to x a sentinel value if no blocking is needed
else:
x=-1#assign to x a sentinel value if no blocking is needed
#as soon as it founds a block, it breaks out of the outer loop
if(found==True):
break
#3.2 blocking minor diagonals
if(found==False):
for k in range(5,2,-1):#again, using k and p to control the starting points
for p in range(0,4,1):
#check if there's a three-in-a-row
if(B[k][p]==B[k-1][p+1] and B[k-1][p+1]==B[k-2][p+2] and B[k][p]!=" "):
if(B[k-3][p+3]==" "):#check if the fourth box is empty or not
#check if the boxes under the fourth are full
full=True#create a flag to see if the three boxes under are full
for i in range(5,k-2,-1):
if(B[i][p+3]==" "):
full=False
break#as soon as it finds one empty,it breaks
if(full==True):
found=True
x=p+3
break#after finding a block, break
else:
x=-1#assign to x a sentinel value if no blocking is needed
else:
x=-1#assign to x a sentinel value if no blocking is needed
#as soon as it founds a block, it breaks out of the outer loop
if(found==True):
break
return x
def XX_XtypeBlocking(B):
#this function takes in the gameboard
#it blocks XX_X or X_XX traps
#it returns the variable x storing the value of the column
#1.blocking horizontally
#1.1 blocking XX_X horizontally
found=False#creates a flag to keep track if a block has been found
for i in range(5,-1,-1):
for p in range(0,4,1):#using the variable p to control the column starting point
#check if there is a XX_X trap
if(B[i][p]==B[i][p+1] and B[i][p+1]==B[i][p+3] and B[i][p+2]==" " and B[i][p]!=" "):
if(i==5):#take care of the edge case
found=True
x=p+2
break#when it finds a block, it breaks
else:#takes care of the other cases
if(B[i+1][p+2]!=" "):#checks if the box under is full
found=True
x=p+2
break#when it finds a block, it breaks
else:
x=-1#assign to x a sentinel value if no blocking is needed
else:
x=-1#assign to x a sentinel value if no blocking is needed
#as soon as it founds a block, it breaks out of the outer loop
if(found==True):
break
#1.2 blocking X_XX horizontally
if(found==False):
for i in range(5,-1,-1):
for p in range(0,4,1):#using the variable p to control the column starting point
#check if there is a XX_X trap
if(B[i][p]==B[i][p+2] and B[i][p+2]==B[i][p+3] and B[i][p+1]==" " and B[i][p]!=" "):
if(i==5):#take care of the edge case
found=True
x=p+1
break#when it finds a block, it breaks
else:#takes care of the other cases
if(B[i+1][p+1]!=" "):#checks if the box under is full
found=True
x=p+1
break#when it finds a block, it breaks
else:
x=-1#assign to x a sentinel value if no blocking is needed
else:
x=-1#assign to x a sentinel value if no blocking is needed
#as soon as it founds a block, it breaks out of the outer loop
if(found==True):
break
#2-there is no vertical trap
#3.blocking diagonally
#3.1 blocking the major diagonals
#3.1.1 blocking XX_X on the major diagonals
if(found==False):
for k in range(5,2,-1):
for p in range(3,7,1):#again, using k and p to control the starting points
#check if there is a XX_X trap
if(B[k][p]==B[k-2][p-2] and B[k-2][p-2]==B[k-3][p-3] and B[k-1][p-1]==" " and B[k][p]!=" "):
if(i==5):#take care of the edge case
found=True
x=p-1
break#when it finds a block, it breaks
else:#takes care of the other cases
if(B[k][p-1]!=" "):#checks if the box under is full
found=True
x=p-1
break#when it finds a block, it breaks
else:
x=-1#assign to x a sentinel value if no blocking is needed
else:
x=-1#assign to x a sentinel value if no blocking is needed
#as soon as it founds a block, it breaks out of the outer loop
if(found==True):
break
#3.1.2 blocking X_XX on the major diagonals
if(found==False):
for k in range(5,2,-1):
for p in range(3,7,1):#again, using k and p to control the starting points
#check if there is a XX_X trap
if(B[k][p]==B[k-1][p-1] and B[k-1][p-1]==B[k-3][p-3] and B[k-2][p-2]==" " and B[k][p]!=" "):
if(i==5):#take care of the edge case
found=True
x=p-2
break#when it finds a block, it breaks
else:#takes care of the other cases
if(B[k-1][p-2]!=" "):#checks if the box under is full
found=True
x=p-2
break#when it finds a block, it breaks
else:
x=-1#assign to x a sentinel value if no blocking is needed
else:
x=-1#assign to x a sentinel value if no blocking is needed
#as soon as it founds a block, it breaks out of the outer loop
if(found==True):
break
#3.2 blocking the minor diagonals
#3.2.1 blocking XX_X on the minor diagonals
if(found==False):
for k in range(5,2,-1):
for p in range(0,4,1):#again, using k and p to control the starting points
#check if there is a XX_X trap
if(B[k][p]==B[k-2][p+2] and B[k-2][p+2]==B[k-3][p+3] and B[k-1][p+1]==" " and B[k][p]!=" "):
if(i==5):#take care of the edge case
found=True
x=p+1
break#when it finds a block, it breaks
else:#takes care of the other cases
if(B[k][p+1]!=" "):#checks if the box under is full
found=True
x=p+1
break#when it finds a block, it breaks
else:
x=-1#assign to x a sentinel value if no blocking is needed
else:
x=-1#assign to x a sentinel value if no blocking is needed
#as soon as it founds a block, it breaks out of the outer loop
if(found==True):
break
#3.2.2 blocking X_XX on the minor diagonals
if(found==False):
for k in range(5,2,-1):
for p in range(0,4,1):#again, using k and p to control the starting points
#check if there is a XX_X trap
if(B[k][p]==B[k-1][p+1] and B[k-1][p+1]==B[k-3][p+3] and B[k-2][p+2]==" " and B[k][p]!=" "):
if(i==5):#take care of the edge case
found=True
x=p+2
break#when it finds a block, it breaks
else:#takes care of the other cases
if(B[k-1][p+2]!=" "):#checks if the box under is full
found=True
x=p+2
break#when it finds a block, it breaks
else:
x=-1#assign to x a sentinel value if no blocking is needed
else:
x=-1#assign to x a sentinel value if no blocking is needed
#as soon as it founds a block, it breaks out of the outer loop
if(found==True):
break
return x
def ZeroesContinue(B):
#this function takes in the gameboard
#it builds three zeroes from two and four from three
#it returns the variable var which holds one of the values:-1,0,1,2,3,4
#0=vertical,1=left,2=right,3=left fiagonal,4=right diagonal
found=False#creates a flag to see if something was found
#1.vertical continue
for j in range(0,7,1):
for k in range(5,0,-1):#using the variable k to control the row starting point
#checks if there are two zeroes in a row
if(B[k][j]==B[k-1][j] and B[k][j]=="O"):
if(B[k-2][j]==" "):
found=True
var=0
break
else:
var=-1#assign a sentinel value if nothing was found
else:
var=-1#assign a sentinel value if nothing was found
if(found==True):
break
#2.horizontal continue
if(found==False):
for i in range(5,-1,-1):
for p in range(0,6,1):#using the variable p to control the column starting point
if(p==0):#taking care of the first column edgecase
if(i==5):#taking care of the row edgecase
#checks if there are two zeroes in a row
if(B[i][p]==B[i][p+1] and B[i][p]=="O"):
if (B[i][p+2]==" "):
found=True
var=2
break
else:
var=-1#assign a sentinel value if nothing was found
else:
var=-1#assign a sentinel value if nothing was found
else:#taking care of the other rows
#checks if there are two zeroes in a row
if(B[i][p]==B[i][p+1] and B[i][p]=="O"):
if (B[i][p+2]==" " and B[i+1][p+2]!=" "):
found=True
var=2
break
else:
var=-1#assign a sentinel value if nothing was found
else:
var=-1#assign a sentinel value if nothing was found
elif(p==5):#taking care of the last column edgecase
if(i==5):#taking care of the row edgecase
#checks if there are two zeroes in a row
if(B[i][p]==B[i][p+1] and B[i][p]=="O"):
if (B[i][p-1]==" "):
found=True
var=1
break
else:
var=-1#assign a sentinel value if nothing was found
else:
var=-1#assign a sentinel value if nothing was found
else:#taking care of the other rows
#checks if there are two zeroes in a row
if(B[i][p]==B[i][p+1] and B[i][p]=="O"):
if (B[i][p-1]==" " and B[i+1][p-1]!=" "):
found=True
var=1
break
else:
var=-1#assign a sentinel value if nothing was found
else:
var=-1#assign a sentinel value if nothing was found
else:#taking care of the other columns
if(i==5):#taking care of the row edgecase
#checks if there are two zeroes in a row
if(B[i][p]==B[i][p+1] and B[i][p]=="O"):
if(B[i][p-1]==" " and B[i][p+2]==" "):
found=True
rand=random.randint(1,2)#chooses randomly between the two possible places
var=rand
break
elif(B[i][p-1]==" "):#checks if the box before is empty
found=True
var=1
break
elif(B[i][p+2]==" "):#checks if the box after is empty
found=True
var=2
break
else:
var=-1#assign a sentinel value if nothing was found
else:
var=-1#assign a sentinel value if nothing was found
else:#takes care of the other rows
#checks if there are two zeroes in a row
if(B[i][p]==B[i][p+1] and B[i][p]=="O"):
if(B[i][p-1]==" " and B[i][p+2]==" " and B[i+1][p-1]!=" " and B[i+1][p+2]==" "):
found=True
rand=random.randint(1,2)#chooses randomly between the two possible places
var=rand
break
elif(B[i][p-1]==" " and B[i+1][p-1]!=" "):#checks if the box before is empty
found=True
var=1
break
elif(B[i][p+2]==" " and B[i+1][p+2]!=" "):#checks if the box after is empty
found=True
var=2
break
else:
var=-1#assign a sentinel value if nothing was found
else:
var=-1#assign a sentinel value if nothing was found
if(found==True):
break
#3.diagonal continue
#3.1 major diagonal continue
if(found==False):
for p in range(2,7,1):
for k in range(5,1,-1):#again, using p and k as starting points
#check if there are two zeroes in a row
if(B[k][p]==B[k-1][p-1] and B[k][p]=="O"):
if(B[k-2][p-2]==" " and B[k-1][p-2]!=" "):
found=True
var=3
break
else:
var=-1#assign a sentinel value if nothing was found
else:
var=-1#assign a sentinel value if nothing was found
if(found==True):
break
#3.2 minor diagonals continue
if(found==False):
for p in range(0,5,1):
for k in range(5,1,-1):#again, using p and k as starting points
#check if there are two zeroes in a row
if(B[k][p]==B[k-1][p+1] and B[k][p]=="O"):
if(B[k-2][p+2]==" " and B[k-1][p+2]!=" "):
found=True
var=4
break
else:
var=-1#assign a sentinel value if nothing was found
else:
var=-1#assign a sentinel value if nothing was found
if(found==True):
break
return var
def ZeroBuilder(B):
#this function takes in the gameboard
#it builds on an already present zeroes instead of choosing randomly
#it returns the variable x which stores the column
#1.it searches the matrix for a zero
found=False #creates a flag to keep track if it can build
var=ZeroesContinue(B)
print(var)
if(var==-1):
for i in range(5,-1,-1):
for j in range(0,7,1):
#it checks if there is a zero present
if(B[i][j]=="O"):
if(i==5):#taking care of the first row edgecase
if(j==0):#taking care of first coulmn edgecase
if(B[i-1][j]==" "):#checks if the box above is empty
found=True#a vertical build can be done
x=j
break#break as soon as it finds a build
elif(B[i][j+1]==" "):#looks for a right build
found=True#a right build can be done
x=j+1
break#break as soon as it finds a build
elif(B[i][j+1]!=" " and B[i-1][j+1]==" "):#looks for a right diagonal build
found=True#a right diagonalbuild can be done
x=j+1
break#break as soon as it finds a build
else:
x=-1#a sentinel value is assigned to x if it cannot build
elif(j==6):#taking care of the last column edgecase
if(B[i-1][j]==" "):#checks if the box above is empty
found=True#a vertical build can be done
x=j
break#break as soon as it finds a build
elif(B[i][j-1]==" "):#looks for a left build
found=True#a left build can be done
x=j-1
break#break as soon as it finds a build
elif(B[i][j-1]!=" " and B[i-1][j-1]==" "):#looks for a left diagonal build
found=True#a left diagonal build can be done
x=j-1
break#break as soon as it finds a build
else:
x=-1#a sentinel value is assigned to x if it cannot build
else:#takes care of the other column cases
if(B[i-1][j]==" "):#checks if the box above is empty
found=True#a vertical build can be done
x=j
break#break as soon as it finds a build
elif(B[i][j+1]==" "):#looks for a right build
found=True#a right build can be done
x=j+1
break#break as soon as it finds a build
elif(B[i][j+1]!=" " and B[i-1][j+1]==" "):#looks for a right diagonal build
found=True#a right diagonalbuild can be done
x=j+1
break#break as soon as it finds a build
elif(B[i][j-1]==" "):#looks for a left build
found=True#a left build can be done
x=j-1
break#break as soon as it finds a build
elif(B[i][j-1]!=" " and B[i-1][j-1]==" "):#looks for a left diagonal build
found=True#a left diagonal build can be done
x=j-1
break#break as soon as it finds a build
else:
x=-1#a sentinel value is assigned to x if it cannot build
elif(i==0):#taking care of the last row edgecase
if(j==0):#taking care of first coulmn edgecase
#checks if it can do a right build
if(B[i][j+1]==" " and B[i+1][j+1]!=" "):
found=True#a right build can be done
x=j+1
break#break as soon as it finds a build
else:
x=-1#a sentinel value is assigned to x if it cannot build
elif(j==6):#taking care of the last column edgecase
#checks if it can do a left build
if(B[i][j-1]==" " and B[i+1][j-1]!=" "):
found=True#a left build can be done
x=j-1
break#break as soon as it finds a build
else:
x=-1#a sentinel value is assigned to x if it cannot build
else:#takes care of the other column cases
#checks if it can do a right build
if(B[i][j+1]==" " and B[i+1][j+1]!=" "):
found=True#a right build can be done
x=j+1
break#break as soon as it finds a build
#checks if it can do a left build
elif(B[i][j-1]==" " and B[i+1][j-1]!=" "):
found=True#a left build can be done
x=j-1
break#break as soon as it finds a build
else:
x=-1#a sentinel value is assigned to x if it cannot build
else:#takes care of the other row cases
if(j==0):#taking care of first coulmn edgecase
if(B[i-1][j]==" "):#checks if the box above is empty
found=True#a vertical build can be done
x=j
break#break as soon as it finds a build
#checks if it can do a right build
if(B[i][j+1]==" " and B[i+1][j+1]!=" "):
found=True#a right build can be done
x=j+1
break#break as soon as it finds a build
elif(B[i][j+1]!=" " and B[i-1][j+1]==" "):#looks for a right diagonal build
found=True#a right diagonalbuild can be done
x=j+1
break#break as soon as it finds a build
else:
x=-1#a sentinel value is assigned to x if it cannot build
elif(j==6):#taking care of the last column edgecase
if(B[i-1][j]==" "):#checks if the box above is empty
found=True#a vertical build can be done
x=j
break#break as soon as it finds a build
#checks if a left build can be done
elif(B[i][j-1]==" " and B[i+1][j-1]!=" "):
found=True#a left build can be done
x=j-1
break#break as soon as it finds a build
elif(B[i][j-1]!=" " and B[i-1][j-1]==" "):#looks for a left diagonal build
found=True#a left diagonal build can be done
x=j-1
break#break as soon as it finds a build
else:
x=-1#a sentinel value is assigned to x if it cannot build
else:#takes care of the other column cases
if(B[i-1][j]==" "):#checks if the box above is empty
found=True#a vertical build can be done
x=j
break#break as soon as it finds a build
#checks if it can do a right build
if(B[i][j+1]==" " and B[i+1][j+1]!=" "):
found=True#a right build can be done
x=j+1
break#break as soon as it finds a build
elif(B[i][j+1]!=" " and B[i-1][j+1]==" "):#looks for a right diagonal build
found=True#a right diagonalbuild can be done
x=j+1
break#break as soon as it finds a build
#checks if a left build can be done
elif(B[i][j-1]==" " and B[i+1][j-1]!=" "):
found=True#a left build can be done
x=j-1
break#break as soon as it finds a build
elif(B[i][j-1]!=" " and B[i-1][j-1]==" "):#looks for a left diagonal build
found=True#a left diagonal build can be done
x=j-1
break#break as soon as it finds a build
else:
x=-1#a sentinel value is assigned to x if it cannot build
else:
x=-1#a sentinel value is assigned to x if it cannot build
if(found==True):
break
else:
for i in range(5,-1,-1):
for j in range(0,7,1):
#it checks if there is a zero present
if(B[i][j]=="O"):
if(i==5):#taking care of the first row edgecase
if(j==0):#taking care of first coulmn edgecase
if(B[i-1][j]==" " and var==0):#checks if the box above is empty
found=True#a vertical build can be done
x=j
break#break as soon as it finds a build
elif(B[i][j+1]==" " and var==2):#looks for a right build
found=True#a right build can be done
x=j+1
break#break as soon as it finds a build
elif(B[i][j+1]!=" " and B[i-1][j+1]==" " and var==4):#looks for a right diagonal build
found=True#a right diagonalbuild can be done
x=j+1
break#break as soon as it finds a build
else:
x=-1#a sentinel value is assigned to x if it cannot build
elif(j==6):#taking care of the last column edgecase
if(B[i-1][j]==" " and var==0):#checks if the box above is empty
found=True#a vertical build can be done
x=j
break#break as soon as it finds a build
elif(B[i][j-1]==" " and var==1):#looks for a left build
found=True#a left build can be done
x=j-1
break#break as soon as it finds a build
elif(B[i][j-1]!=" " and B[i-1][j-1]==" " and var==3):#looks for a left diagonal build
found=True#a left diagonal build can be done
x=j-1
break#break as soon as it finds a build
else:
x=-1#a sentinel value is assigned to x if it cannot build
else:#takes care of the other column cases
if(B[i-1][j]==" " and var==0):#checks if the box above is empty
found=True#a vertical build can be done
x=j
break#break as soon as it finds a build
elif(B[i][j+1]==" " and var==2):#looks for a right build
found=True#a right build can be done
x=j+1
break#break as soon as it finds a build
elif(B[i][j+1]!=" " and B[i-1][j+1]==" " and var==4):#looks for a right diagonal build
found=True#a right diagonalbuild can be done
x=j+1
break#break as soon as it finds a build
elif(B[i][j-1]==" " and var==1):#looks for a left build
found=True#a left build can be done
x=j-1
break#break as soon as it finds a build
elif(B[i][j-1]!=" " and B[i-1][j-1]==" " and var==3):#looks for a left diagonal build
found=True#a left diagonal build can be done
x=j-1
break#break as soon as it finds a build
else:
x=-1#a sentinel value is assigned to x if it cannot build
elif(i==0):#taking care of the last row edgecase
if(j==0):#taking care of first coulmn edgecase
#checks if it can do a right build
if(B[i][j+1]==" " and B[i+1][j+1]!=" " and var==2):
found=True#a right build can be done
x=j+1
break#break as soon as it finds a build
else:
x=-1#a sentinel value is assigned to x if it cannot build
elif(j==6):#taking care of the last column edgecase
#checks if it can do a left build
if(B[i][j-1]==" " and B[i+1][j-1]!=" " and var==1):
found=True#a left build can be done
x=j-1
break#break as soon as it finds a build
else:
x=-1#a sentinel value is assigned to x if it cannot build
else:#takes care of the other column cases
#checks if it can do a right build
if(B[i][j+1]==" " and B[i+1][j+1]!=" " and var==2):
found=True#a right build can be done
x=j+1
break#break as soon as it finds a build
#checks if it can do a left build
elif(B[i][j-1]==" " and B[i+1][j-1]!=" " and var==1):
found=True#a left build can be done
x=j-1
break#break as soon as it finds a build
else:
x=-1#a sentinel value is assigned to x if it cannot build
else:#takes care of the other row cases
if(j==0):#taking care of first coulmn edgecase
if(B[i-1][j]==" " and var==0):#checks if the box above is empty
found=True#a vertical build can be done
x=j
break#break as soon as it finds a build
#checks if it can do a right build
if(B[i][j+1]==" " and B[i+1][j+1]!=" " and var==2):
found=True#a right build can be done
x=j+1
break#break as soon as it finds a build
elif(B[i][j+1]!=" " and B[i-1][j+1]==" " and var==4):#looks for a right diagonal build
found=True#a right diagonalbuild can be done
x=j+1
break#break as soon as it finds a build
else:
x=-1#a sentinel value is assigned to x if it cannot build
elif(j==6):#taking care of the last column edgecase
if(B[i-1][j]==" " and var==0):#checks if the box above is empty
found=True#a vertical build can be done
x=j
break#break as soon as it finds a build
#checks if a left build can be done
elif(B[i][j-1]==" " and B[i+1][j-1]!=" " and var==1):
found=True#a left build can be done
x=j-1
break#break as soon as it finds a build
elif(B[i][j-1]!=" " and B[i-1][j-1]==" " and var==3):#looks for a left diagonal build
found=True#a left diagonal build can be done
x=j-1
break#break as soon as it finds a build
else:
x=-1#a sentinel value is assigned to x if it cannot build
else:#takes care of the other column cases
if(B[i-1][j]==" " and var==0):#checks if the box above is empty
found=True#a vertical build can be done
x=j
break#break as soon as it finds a build
#checks if it can do a right build
if(B[i][j+1]==" " and B[i+1][j+1]!=" " and var==2):
found=True#a right build can be done
x=j+1
break#break as soon as it finds a build
elif(B[i][j+1]!=" " and B[i-1][j+1]==" " and var==4):#looks for a right diagonal build
found=True#a right diagonalbuild can be done
x=j+1
break#break as soon as it finds a build
#checks if a left build can be done
elif(B[i][j-1]==" " and B[i+1][j-1]!=" " and var==1):
found=True#a left build can be done
x=j-1
break#break as soon as it finds a build
elif(B[i][j-1]!=" " and B[i-1][j-1]==" " and var==3):#looks for a left diagonal build
found=True#a left diagonal build can be done
x=j-1
break#break as soon as it finds a build
else:
x=-1#a sentinel value is assigned to x if it cannot build
else:
x=-1#a sentinel value is assigned to x if it cannot build
if(found==True):
break
return x
#-----------MAIN FUNCTION-------------------------------
def main():
#creates the gameboard which is a matrix
gameBoard=[0]*6
for i in range(6):
gameBoard[i]=[" "]*7#copying element by element
displayBoard(gameBoard)#displays the gameboard on the screen
print("------------------------------")
print("Your choices are:")
print("1.No")
print("2.Yes")
print("------------------------------")
undo_choice=int(input("Do you want to be able to undo your moves during the game?(1/2):"))
#it starts the game itself
while(True):#creates an infinite loop for the game
K=StoreBoardState(gameBoard)#saves the state of gamboard before the turns begin
turn=0#this variable holds the turn, it is used to trigger different
#actions when it's the player's turn
#The player's turn
x=int(input("Enter the column:"))#asks the player for the column
x=ColumnReassign(gameBoard,x,turn)#it reassigns it if it's full
r0=rowPlacement(gameBoard,x,turn)#finds the row index of the box that should be filled
gameBoard[r0][x]="X"#modifies the gambeoard by filling the right spot
check1=checkWin(gameBoard)
if(check1==True):#check if the player has won after making his move
displayBoard(gameBoard)
print("You won!")
break
if(checkTie(gameBoard)):#check if there is a tie after the player's move
displayBoard(gameBoard)
print("It's a tie!")
break
turn=1#The computer's turn
z=fourBlocking(gameBoard)
if(z!=-1):#check if the computer has to block a three-in-a row
#the computer has to block
r1=rowPlacement(gameBoard,z,turn)
gameBoard[r1][z]="O"
else:#the computer does not have to to block a three-in a row
w=XX_XtypeBlocking(gameBoard)
if(w!=-1):#check if there is XX_X type trap to be blocked
r1=rowPlacement(gameBoard,w,turn)
gameBoard[r1][w]="O"
else:#the computer does not have to block so it tries to build
build=ZeroBuilder(gameBoard)
if(ZeroBuilder!=-1):#the computer can build zero structures
build=ColumnReassign(gameBoard,build,turn)
r1=rowPlacement(gameBoard,build,turn)
gameBoard[r1][build]="O"
else:#the computer does not have to do anything so it chooses randomly
rand=random.randint(0,6)
rand=ColumnReassign(gameBoard,rand,turn)#it reassigns it if it's full
r1=rowPlacement(gameBoard,rand,turn)#finds the row index of the box that should be filled
gameBoard[r1][rand]="O"
check2=checkWin(gameBoard)
if(check2==True):#check if the computer has won after making his move
displayBoard(gameBoard)
print("You lost!")
break
if(checkTie(gameBoard)):#check if there is a tie after the computer's move
displayBoard(gameBoard)
print("It's a tie!")
break
displayBoard(gameBoard)#displays the gameboard on the screen
#printing the continue/undo menu
if(undo_choice==2):
print("------------------------------")
print("Do you want to continue or to undo your action?")
print("1.Continue")
print("2.Undo")
print("------------------------------")
undo=int(input("Do you want to undo your move?:"))
if(undo==2):#it undoes the move
gameBoard=K#it rolls back to the state before the moves
displayBoard(gameBoard)#displays the gameboard on the screen
#------------MAIN PROGRAM---------------------------------
main()