-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWelcomeController.java
1258 lines (1059 loc) · 43 KB
/
WelcomeController.java
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
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.example.demo;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.util.*;
// AI --->-1
//USER ---->1
// TIE ---->0
public class WelcomeController implements Initializable {
public ArrayList<TreeNode> FinalNodes=new ArrayList<>();
String turn="";
String whichOneToStart="";
int whichOneHasStarted=0; //--->1 user
//--->0 AI
boolean AIWOn=false;
char userColor='w';
static final int BOARD_SIZE = 8;
static final int SQUARE_SIZE = 80;
static final int [][] indexes=new int[8][8];
static final int SMALL_SQUARE_SIZE = 20;
static final Color SMALL_SQUARE_COLOR =Color.HOTPINK;
final int[] isSmallSquareDark = new int[1];
public static String WINNER="NO ONE";
static final char[][] color=new char[8][8];
//__________________> w=white
//__________________> r=red
// Track the occupied cells on the board
public static Set<Cell> occupiedCells;
@FXML
private Button firstButton;
@FXML
private Button secondButton;
@FXML
private Button AIStartsButton;
@FXML
private Button youStartButton;
//Button homeButton = new Button("Home");
int AIRow=0;
int AIColumn=0;
public Stage primaryStage;
public Parent root;
int choice=0;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
}
public GridPane createGameBoard() {
GridPane board = new GridPane();
boolean isDark = false;
occupiedCells = new HashSet<>();
for (int row = 0; row < BOARD_SIZE; row++) {
isDark = !isDark; // Alternate the color of each row
for (int col = 0; col < BOARD_SIZE; col++) {
Rectangle square = new Rectangle(SQUARE_SIZE, SQUARE_SIZE);
square.setFill(isDark ? Color.DARKGRAY : Color.LIGHTGRAY);
board.add(square, col, row);
isDark = !isDark; // Alternate the color of each square
// Add click event handler to each square
final int rowIndex = row;
final int colIndex = col;
if(whichOneToStart.equalsIgnoreCase("AI")) {
callAI(board);
}
square.setOnMouseClicked(event -> {
if(occupiedCells.size()==63){
Stage primaryStage=(Stage) ((Node) event.getSource()).getScene().getWindow();
showResult(primaryStage);
}
if(indexes[rowIndex][colIndex]==1){ // IF THE PLACE ALREADY TAKEN
System.out.println("Cannot place brick on block: " + rowIndex + ", " + colIndex);
}
else if (canPlaceBrick(rowIndex, colIndex)) {// CHECK IF THE PLACE IS LEGAL
turn="AI";
System.out.println("Placed brick on block: " + rowIndex + ", " + colIndex);
Rectangle smallSquare = new Rectangle(SMALL_SQUARE_SIZE, SMALL_SQUARE_SIZE, SMALL_SQUARE_COLOR);
addSmallSquare(smallSquare,board, rowIndex, colIndex);
char tmp;
if(isSmallSquareDark[0] ==0){ //if the placed square is white
color[rowIndex][colIndex]='w';
smallSquare.setFill(Color.LAVENDER);//inverse the square color
isSmallSquareDark[0] =1;
}
else{
color[rowIndex][colIndex]='r';
smallSquare.setFill(Color.MAROON);
isSmallSquareDark[0] =0;
}
occupiedCells.add(new Cell(rowIndex, colIndex));
indexes[rowIndex][colIndex]=1; //RESERVE INDEX IN THE ARRAy
if (checkWin(rowIndex, colIndex,color[rowIndex][colIndex])) {
showFinalColors();
System.out.print("Congratulations!");
if(isSmallSquareDark[0]==0) {
System.out.println("RED WON");
WINNER="RED";
Stage primaryStage=(Stage) ((Node) event.getSource()).getScene().getWindow();
showResult(primaryStage);
}
else {
WINNER="WHITE";
System.out.println("WHITE WON");
Stage primaryStage=(Stage) ((Node) event.getSource()).getScene().getWindow();
showResult(primaryStage);
}
}
}
else {
System.out.println("Cannot place brick on block: " + rowIndex + ", " + colIndex);
}
if(choice!=1){//IF THE USER WANT'S TO PLAY AGAINST ANOTHER USER
//System.out.println("hihihih");
callAI(board);
}
});
}
}
return board;
}
public void AITurn(){
// I MUST FIGURE ALL OPTIONS AVAILABLE FOR AI AFTER THE USER HAS PLAYED:
// MAKE COPY OF THE CURRENT BOARD OF COLORS:
char[][] colorsCopy = new char[8][8];
int[][] indexesCopy=new int[8][8];
for (int i = 0; i < color.length; i++) {
System.arraycopy(color[i], 0, colorsCopy[i], 0, color[i].length);
}//make copy of the original color's board
for (int i = 0; i < indexes.length; i++) {
System.arraycopy(indexes[i], 0, indexesCopy[i], 0, indexes[i].length);
}//make copy of the original integer's board
//now let's try to make the tree:
TreeNode root=new TreeNode();
root.setColors(colorsCopy);
root.setIndexes(indexesCopy);
Tree tree=new Tree(root);
char currentColor='z';
if(whichOneHasStarted==0) //if the started one is AI, then the AI must be white
currentColor ='w';
else if(whichOneHasStarted==1)
currentColor='r';//if the started one is user, then the AI must be red
boolean check=false;
//NOW LET'S FIND LEGAL PLACES FOR THE AI
//int numberOfAvailablePlaces=0;
boolean tmp=false; // to check if i can place brick in such square
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
if(indexes[i][j]==0) { //IF NOTHING PLACED ON THE CELL, CHECK IF WE CAN PLACE BRICK THERE
tmp = canPlaceBrick(i, j);
if(tmp) {
TreeNode newNode=new TreeNode();//create node
newNode.setColors(colorsCopy,i,j,currentColor);//set the color for in the place of this brick
newNode.setIndexes(indexesCopy,i,j);//set 1 at the place of this brick
newNode.setLastRowPlaced(i);
newNode.setLastColumnPlaced(j);
//add the node to the tree
tree.insert(newNode,root);
//CHECK IF THIS IS A WINNING MOVE
check= checkWin2(i,j,currentColor,newNode.getIndexes(), newNode.getColors());
if(check) {//IF THIS AI MOVE IS A WINNING MOVE, DON'T GENERATE IT'S CHILDES
newNode.setGenerateChildrens(false);
newNode.setValue(-5);//AI HAS WON
AIRow=i;
AIColumn=j;
AIWOn=true;
//break;
}
}//make copy of the original integer's board
}
}
tmp=false;
}
if(currentColor=='w')
userColor='r';
else if(currentColor=='r')
userColor='w';
// NOW I NEED TO GUESS THE NEXT MOVE OF THE USER
for(int m=0;m<root.getChildren().size();m++){
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
if(root.getChildren().get(m).isGenerateChildrens()) { // IF THIS MOVE BY THE AI IS NOT WINNING, GENERATE IT'S CHILDES
if(root.getChildren().get(m).getIndexes(i,j)==0) { //IF NOTHING PLACED ON THE CELL, CHECK IF WE CAN PLACE BRICK THERE
tmp = canPlaceBrick2(i, j,root.getChildren().get(m).getIndexes());
if(tmp) {
TreeNode newNode=new TreeNode();//create node
newNode.setColors(root.getChildren().get(m).getColors(),i,j,userColor);//set the color for the place of this brick
newNode.setIndexes(root.getChildren().get(m).getIndexes(),i,j);//set 1 at the place of this brick
//add the node to the tree
tree.insert(newNode,root.getChildren().get(m));
newNode.setLastRowPlaced(i);
newNode.setLastColumnPlaced(j);
}//make copy of the original integer's board
}
}
tmp=false;
}
}
}
//NOW I SHOULD GIVE VALUES TO THE CHILDES OF CHILDES, BY CHECKING IF THE USER WON!!
int check2=0;
for(int i=0;i<root.getChildren().size();i++)
{
for (int j=0;j<root.getChildren().get(i).getChildren().size();j++)
{
check2=checkWin3(root.getChildren().get(i).getChildren().get(j).getLastRowPlaced(),
root.getChildren().get(i).getChildren().get(j).getLastColumnPlaced(),userColor,
root.getChildren().get(i).getChildren().get(j).getIndexes(), root.getChildren().get(i).getChildren().get(j).getColors());
if(check2==1)
root.getChildren().get(i).getChildren().get(j).setValue(10); //IF IT'S WINNING MOVE
else {
countSquares(root.getChildren().get(i).getChildren().get(j),root.getChildren().get(i).getChildren().get(j).getLastRowPlaced(),
root.getChildren().get(i).getChildren().get(j).getLastColumnPlaced(),userColor,
root.getChildren().get(i).getChildren().get(j).getIndexes(), root.getChildren().get(i).getChildren().get(j).getColors());
int value=findMaximum(root.getChildren().get(i).getChildren().get(j).getCountH(),
root.getChildren().get(i).getChildren().get(j).getCountV(),root.getChildren().get(i).getChildren().get(j).getCountD());
root.getChildren().get(i).getChildren().get(j).setValue(value);
}
}
}
//NOW I NEED TO APPLY THE MINIMAX ALG
int AIEvaluation=0;
// if(whichOneHasStarted==0) {//if the AI STARTED THEN IT'S MAXIMIZING
//AIEvaluation = miniMax(root, 2, Integer.MIN_VALUE, Integer.MAX_VALUE, true);
// }
// else if(whichOneHasStarted==1){
AIEvaluation = miniMax(root, 2, Integer.MIN_VALUE, Integer.MAX_VALUE, false);
// }
int count=0;
//NOW I NEED TO SEARCH FOR THE {i,j} FOR THE AI
for(int i=0;i<root.getChildren().size();i++){
if(root.getChildren().get(i).getValue()==AIEvaluation){
count++;
root.getChildren().get(i).setCount(specialCountSquares(root.getChildren().get(i),root.getChildren().get(i).getLastRowPlaced(),
root.getChildren().get(i).getLastColumnPlaced(),currentColor,
root.getChildren().get(i).getIndexes(),root.getChildren().get(i).getColors()));
root.getChildren().get(i).setCount2(specialCountSquares2(root.getChildren().get(i),root.getChildren().get(i).getLastRowPlaced(),
root.getChildren().get(i).getLastColumnPlaced(),currentColor,
root.getChildren().get(i).getIndexes(),root.getChildren().get(i).getColors()));
}
}
for(int i=0;i<root.getChildren().size();i++){
for(int j=0;j<8;j++){
for(int k=0;k<8;k++){
System.out.print(root.getChildren().get(i).getColors(j,k));
}
System.out.println();
}
System.out.println(root.getChildren().get(i).getCount2()+"{"+root.getChildren().get(i).getLastRowPlaced()+","+root.getChildren().get(i).getLastColumnPlaced()+"}");
System.out.println("Value= "+root.getChildren().get(i).getValue());
System.out.println("Count1= "+root.getChildren().get(i).getCount());
System.out.println();
System.out.println();
}
int id=0;
int max=0;
if(count==root.getChildren().size()) {
for (int i = 0; i < root.getChildren().size(); i++) {
if (max < root.getChildren().get(i).getCount()) {
max = root.getChildren().get(i).getCount();
id = i;
}
}
int id2 = 0;
int max2 = 0;
for (int i = 0; i < root.getChildren().size(); i++) {
if (max2 < root.getChildren().get(i).getCount2()) {
max2 = root.getChildren().get(i).getCount2();
id2 = i;
}
}
if (AIWOn == true) {
} else if (max >= max2) {
AIRow = root.getChildren().get(id).getLastRowPlaced();
AIColumn = root.getChildren().get(id).getLastColumnPlaced();
} else {
AIRow = root.getChildren().get(id2).getLastRowPlaced();
AIColumn = root.getChildren().get(id2).getLastColumnPlaced();
}
}
else{
for(int i=0;i<root.getChildren().size();i++){
if(root.getChildren().get(i).getValue()==AIEvaluation){
AIRow=root.getChildren().get(i).getLastRowPlaced();
AIColumn=root.getChildren().get(i).getLastColumnPlaced();
break;
}
}
}
AIWOn=false;
}
public static int findMaximum(int num1, int num2, int num3) {
int max = num1;
if (num2 > max) {
max = num2;
}
if (num3 > max) {
max = num3;
}
return max;
}
private int miniMax(TreeNode node,int depth,int alpha, int beta,boolean isMaximizingPlayer ){
if(depth==0||node.getChildren().isEmpty())
return node.getValue();
if (isMaximizingPlayer){
int maxEval=Integer.MIN_VALUE;
int eval=0;
for(TreeNode child: node.getChildren()) {
eval = miniMax(child, depth - 1, alpha, beta, false);
maxEval = Integer.max(maxEval, eval);
child.setValue(maxEval);
alpha = Integer.max(alpha, eval);
if (beta <= alpha)
break;
}
return maxEval;
}
else{
int minEval=Integer.MAX_VALUE;
int eval=0;
for(TreeNode child:node.getChildren()){
eval=miniMax(child,depth-1,alpha,beta,true);
minEval=Integer.min(minEval,eval);
child.setValue(minEval);
beta=Integer.min(beta,eval);
if(beta<=alpha)
break;
}
return minEval;
}
}
private boolean canPlaceBrick(int rowIndex, int colIndex) {
if (colIndex == 0 || colIndex == BOARD_SIZE - 1) {
// Brick can be placed directly on the left or right wall
return true;
}
Cell leftCell = new Cell(rowIndex, colIndex - 1);
Cell rightCell = new Cell(rowIndex, colIndex + 1);
// Brick can be placed to the left or right of another brick
return occupiedCells.contains(leftCell) || occupiedCells.contains(rightCell);
}
private boolean canPlaceBrick2(int rowIndex,int colIndex,int [][] indexes){
if (colIndex == 0 || colIndex == BOARD_SIZE - 1) {
// Brick can be placed directly on the left or right wall
return true;
}
if(indexes[rowIndex][colIndex-1]==1||indexes[rowIndex][colIndex+1]==1)
return true;
return false;
}
private int specialCountSquares2(TreeNode node,int rowIndex, int colIndex,char colorOfSmallSquare,int [][] indexes,char [][] color){
int targetValue = indexes[rowIndex][colIndex];
int count=0;
// Check horizontally
int left = colIndex - 1;
while (left >= 0 && indexes[rowIndex][left] == targetValue) {
if(color[rowIndex][left]==colorOfSmallSquare) {
count++;
}
else
break;
left--;
}
int right = colIndex + 1;
while (right < BOARD_SIZE && indexes[rowIndex][right] == targetValue) {
if(color[rowIndex][right]==colorOfSmallSquare) {
count++;
}
else
break;
right++;
}
// Check vertically
int up = rowIndex - 1;
while (up >= 0 && indexes[up][colIndex] == targetValue) {
if(color[up][colIndex]==colorOfSmallSquare) {
count++;
}
else {
break;
}
up--;
}
int down = rowIndex + 1;
while (down < BOARD_SIZE && indexes[down][colIndex] == targetValue) {
if(color[down][colIndex]==colorOfSmallSquare) {
count++;
}
else {
break;
}
down++;
}
// Check diagonally (top-left to bottom-right)
int topLeftRow = rowIndex - 1;
int topLeftCol = colIndex - 1;
while (topLeftRow >= 0 && topLeftCol >= 0 && indexes[topLeftRow][topLeftCol] == targetValue) {
if(color[topLeftRow][topLeftCol]==colorOfSmallSquare) {
count++;
}
else
break;
topLeftRow--;
topLeftCol--;
}
int bottomRightRow = rowIndex + 1;
int bottomRightCol = colIndex + 1;
while (bottomRightRow < BOARD_SIZE && bottomRightCol < BOARD_SIZE && indexes[bottomRightRow][bottomRightCol] == targetValue) {
if(color[bottomRightRow][bottomRightCol]==colorOfSmallSquare) {
count++;
}
else
break;
bottomRightRow++;
bottomRightCol++;
}
// Check diagonally (top-right to bottom-left)
int topRightRow = rowIndex - 1;
int topRightCol = colIndex + 1;
while (topRightRow >= 0 && topRightCol < BOARD_SIZE && indexes[topRightRow][topRightCol] == targetValue) {
if(color[topRightRow][topRightCol]==colorOfSmallSquare) {
count++;
}
else
break;
topRightRow--;
topRightCol++;
}
int bottomLeftRow = rowIndex + 1;
int bottomLeftCol = colIndex - 1;
while (bottomLeftRow < BOARD_SIZE && bottomLeftCol >= 0 && indexes[bottomLeftRow][bottomLeftCol] == targetValue) {
if(color[bottomLeftRow][bottomLeftCol]==colorOfSmallSquare) {
count++;
}
else
break;
bottomLeftRow++;
bottomLeftCol--;
}
System.out.println("COUNT="+count);
return count;
}
private int specialCountSquares(TreeNode node,int rowIndex, int colIndex,char colorOfSmallSquare,int [][] indexes,char [][] color){
int targetValue = indexes[rowIndex][colIndex];
int count=0;
// Check horizontally
int left = colIndex - 1;
while (left >= 0 && indexes[rowIndex][left] == 1) {
if(color[rowIndex][left]!=colorOfSmallSquare)
count++;
else
break;
left--;
}
int right = colIndex + 1;
while (right < BOARD_SIZE && indexes[rowIndex][right] == targetValue) {
if(color[rowIndex][right]!=colorOfSmallSquare)
count++;
else
break;
right++;
}
// Check vertically
int up = rowIndex - 1;
while (up >= 0 && indexes[up][colIndex] == targetValue) {
if(color[up][colIndex]!=colorOfSmallSquare) {
count++;
}
else {
break;
}
up--;
}
int down = rowIndex + 1;
while (down < BOARD_SIZE && indexes[down][colIndex] == targetValue) {
if(color[down][colIndex]!=colorOfSmallSquare) {
count++;
}
else {
break;
}
down++;
}
// Check diagonally (top-left to bottom-right)
int topLeftRow = rowIndex - 1;
int topLeftCol = colIndex - 1;
while (topLeftRow >= 0 && topLeftCol >= 0 && indexes[topLeftRow][topLeftCol] == targetValue) {
if(color[topLeftRow][topLeftCol]!=colorOfSmallSquare)
count++;
else
break;
topLeftRow--;
topLeftCol--;
}
int bottomRightRow = rowIndex + 1;
int bottomRightCol = colIndex + 1;
while (bottomRightRow < BOARD_SIZE && bottomRightCol < BOARD_SIZE && indexes[bottomRightRow][bottomRightCol] == targetValue) {
if(color[bottomRightRow][bottomRightCol]!=colorOfSmallSquare)
count++;
else
break;
bottomRightRow++;
bottomRightCol++;
}
// Check diagonally (top-right to bottom-left)
int topRightRow = rowIndex - 1;
int topRightCol = colIndex + 1;
while (topRightRow >= 0 && topRightCol < BOARD_SIZE && indexes[topRightRow][topRightCol] == targetValue) {
if(color[topRightRow][topRightCol]!=colorOfSmallSquare)
count++;
else
break;
topRightRow--;
topRightCol++;
}
int bottomLeftRow = rowIndex + 1;
int bottomLeftCol = colIndex - 1;
while (bottomLeftRow < BOARD_SIZE && bottomLeftCol >= 0 && indexes[bottomLeftRow][bottomLeftCol] == targetValue) {
if(color[bottomLeftRow][bottomLeftCol]!=colorOfSmallSquare)
count++;
else
break;
bottomLeftRow++;
bottomLeftCol--;
}
return count;
}
private void addSmallSquare(Rectangle smallSquare,GridPane board, int rowIndex, int colIndex) {
smallSquare.setStroke(Color.BLACK);
smallSquare.setStrokeWidth(1);
smallSquare.setArcWidth(5);
smallSquare.setArcHeight(5);
// Center align the small square within the block
int padding = (SQUARE_SIZE - SMALL_SQUARE_SIZE) / 2;
GridPane.setRowIndex(smallSquare, rowIndex);
GridPane.setColumnIndex(smallSquare, colIndex);
GridPane.setMargin(smallSquare, new javafx.geometry.Insets(padding));
board.getChildren().add(smallSquare);
}
private boolean checkWin(int rowIndex, int colIndex,char colorOfSmallSquare) {
int targetValue = indexes[rowIndex][colIndex];
int count = 1;
// Check horizontally
int left = colIndex - 1;
while (left >= 0 && indexes[rowIndex][left] == 1) {
if(color[rowIndex][left]==colorOfSmallSquare)
count++;
else
break;
left--;
}
int right = colIndex + 1;
while (right < BOARD_SIZE && indexes[rowIndex][right] == 1) {
if(color[rowIndex][right]==colorOfSmallSquare)
count++;
else
break;
right++;
}
if (count >= 5) {
return true;
}
// Check vertically
count = 1;
int up = rowIndex - 1;
while (up >= 0 && indexes[up][colIndex] == targetValue) {
if(color[up][colIndex]==colorOfSmallSquare) {
count++;
}
else
break;
up--;
}
int down = rowIndex + 1;
while (down < BOARD_SIZE && indexes[down][colIndex] == targetValue) {
if(color[down][colIndex]==colorOfSmallSquare) {
count++;
}
else
break;
down++;
}
if (count >= 5) {
return true;
}
// Check diagonally (top-left to bottom-right)
count = 1;
int topLeftRow = rowIndex - 1;
int topLeftCol = colIndex - 1;
while (topLeftRow >= 0 && topLeftCol >= 0 && indexes[topLeftRow][topLeftCol] == targetValue) {
if(color[topLeftRow][topLeftCol]==colorOfSmallSquare)
count++;
else
break;
topLeftRow--;
topLeftCol--;
}
int bottomRightRow = rowIndex + 1;
int bottomRightCol = colIndex + 1;
while (bottomRightRow < BOARD_SIZE && bottomRightCol < BOARD_SIZE && indexes[bottomRightRow][bottomRightCol] == targetValue) {
if(color[bottomRightRow][bottomRightCol]==colorOfSmallSquare)
count++;
else
break;
bottomRightRow++;
bottomRightCol++;
}
if (count >= 5) {
return true;
}
// Check diagonally (top-right to bottom-left)
count = 1;
int topRightRow = rowIndex - 1;
int topRightCol = colIndex + 1;
while (topRightRow >= 0 && topRightCol < BOARD_SIZE && indexes[topRightRow][topRightCol] == targetValue) {
if(color[topRightRow][topRightCol]==colorOfSmallSquare)
count++;
else
break;
topRightRow--;
topRightCol++;
}
int bottomLeftRow = rowIndex + 1;
int bottomLeftCol = colIndex - 1;
while (bottomLeftRow < BOARD_SIZE && bottomLeftCol >= 0 && indexes[bottomLeftRow][bottomLeftCol] == targetValue) {
if(color[bottomLeftRow][bottomLeftCol]==colorOfSmallSquare)
count++;
else
break;
bottomLeftRow++;
bottomLeftCol--;
}
if (count >= 5) {
return true;
}
return false;
}
private boolean checkWin2(int rowIndex, int colIndex,char colorOfSmallSquare,int [][] indexes,char [][] color) {
int targetValue = indexes[rowIndex][colIndex];
int count = 1;
// Check horizontally
int left = colIndex - 1;
while (left >= 0 && indexes[rowIndex][left] == targetValue) {
if(color[rowIndex][left]==colorOfSmallSquare)
count++;
else
break;
left--;
}
int right = colIndex + 1;
while (right < BOARD_SIZE && indexes[rowIndex][right] == targetValue) {
if(color[rowIndex][right]==colorOfSmallSquare)
count++;
else
break;
right++;
}
if (count >= 5) {
return true;
}
// Check vertically
count = 1;
int up = rowIndex - 1;
while (up >= 0 && indexes[up][colIndex] == targetValue) {
if(color[up][colIndex]==colorOfSmallSquare) {
count++;
}
else
break;
up--;
}
int down = rowIndex + 1;
while (down < BOARD_SIZE && indexes[down][colIndex] == targetValue) {
if(color[down][colIndex]==colorOfSmallSquare) {
count++;
}
else
break;
down++;
}
if (count >= 5) {
return true;
}
// Check diagonally (top-left to bottom-right)
count = 1;
int topLeftRow = rowIndex - 1;
int topLeftCol = colIndex - 1;
while (topLeftRow >= 0 && topLeftCol >= 0 && indexes[topLeftRow][topLeftCol] == targetValue) {
if(color[topLeftRow][topLeftCol]==colorOfSmallSquare)
count++;
else
break;
topLeftRow--;
topLeftCol--;
}
int bottomRightRow = rowIndex + 1;
int bottomRightCol = colIndex + 1;
while (bottomRightRow < BOARD_SIZE && bottomRightCol < BOARD_SIZE && indexes[bottomRightRow][bottomRightCol] == targetValue) {
if(color[bottomRightRow][bottomRightCol]==colorOfSmallSquare)
count++;
else
break;
bottomRightRow++;
bottomRightCol++;
}
if (count >= 5) {
return true;
}
// Check diagonally (top-right to bottom-left)
count = 1;
int topRightRow = rowIndex - 1;
int topRightCol = colIndex + 1;
while (topRightRow >= 0 && topRightCol < BOARD_SIZE && indexes[topRightRow][topRightCol] == targetValue) {
if(color[topRightRow][topRightCol]==colorOfSmallSquare)
count++;
else
break;
topRightRow--;
topRightCol++;
}
int bottomLeftRow = rowIndex + 1;
int bottomLeftCol = colIndex - 1;
while (bottomLeftRow < BOARD_SIZE && bottomLeftCol >= 0 && indexes[bottomLeftRow][bottomLeftCol] == targetValue) {
if(color[bottomLeftRow][bottomLeftCol]==colorOfSmallSquare)
count++;
else
break;
bottomLeftRow++;
bottomLeftCol--;
}
if (count >= 5) {
return true;
}
return false;
}
private int checkWin3(int rowIndex, int colIndex,char colorOfSmallSquare,int [][] indexes,char [][] color) {
int targetValue = indexes[rowIndex][colIndex];
int count = 1;
// Check horizontally
int left = colIndex - 1;
while (left >= 0 && indexes[rowIndex][left] == targetValue) {
if(color[rowIndex][left]==colorOfSmallSquare)
count++;
else
break;
left--;
}
int right = colIndex + 1;
while (right < BOARD_SIZE && indexes[rowIndex][right] == targetValue) {
if(color[rowIndex][right]==colorOfSmallSquare)
count++;
else
break;
right++;
}
if (count >= 5) {
return 1;
}
// Check vertically
count = 1;
int up = rowIndex - 1;
while (up >= 0 && indexes[up][colIndex] == targetValue) {
if(color[up][colIndex]==colorOfSmallSquare) {
count++;
}
else
break;
up--;
}
int down = rowIndex + 1;
while (down < BOARD_SIZE && indexes[down][colIndex] == targetValue) {
if(color[down][colIndex]==colorOfSmallSquare) {
count++;
}
else
break;
down++;
}
if (count >= 5) {
return 1;
}
// Check diagonally (top-left to bottom-right)
count = 1;
int topLeftRow = rowIndex - 1;
int topLeftCol = colIndex - 1;
while (topLeftRow >= 0 && topLeftCol >= 0 && indexes[topLeftRow][topLeftCol] == targetValue) {
if(color[topLeftRow][topLeftCol]==colorOfSmallSquare)
count++;
else
break;
topLeftRow--;
topLeftCol--;
}
int bottomRightRow = rowIndex + 1;
int bottomRightCol = colIndex + 1;
while (bottomRightRow < BOARD_SIZE && bottomRightCol < BOARD_SIZE && indexes[bottomRightRow][bottomRightCol] == targetValue) {
if(color[bottomRightRow][bottomRightCol]==colorOfSmallSquare)
count++;
else
break;
bottomRightRow++;
bottomRightCol++;
}
if (count >= 5) {
return 1;
}
// Check diagonally (top-right to bottom-left)
count = 1;
int topRightRow = rowIndex - 1;
int topRightCol = colIndex + 1;
while (topRightRow >= 0 && topRightCol < BOARD_SIZE && indexes[topRightRow][topRightCol] == targetValue) {
if(color[topRightRow][topRightCol]==colorOfSmallSquare)
count++;
else
break;
topRightRow--;
topRightCol++;
}
int bottomLeftRow = rowIndex + 1;
int bottomLeftCol = colIndex - 1;
while (bottomLeftRow < BOARD_SIZE && bottomLeftCol >= 0 && indexes[bottomLeftRow][bottomLeftCol] == targetValue) {
if(color[bottomLeftRow][bottomLeftCol]==colorOfSmallSquare)
count++;
else
break;
bottomLeftRow++;
bottomLeftCol--;
}
if (count >= 5) {