-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAIMoves.java
More file actions
749 lines (714 loc) · 39.6 KB
/
AIMoves.java
File metadata and controls
749 lines (714 loc) · 39.6 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
import java.util.Arrays;
import java.util.ArrayList;
class AIMoves {
String[][] chessBoard;
boolean kingSafe = true;
// The Objects below are used for the player's chess pieces.
Pawn pawn9 = new Pawn();
Pawn pawn10 = new Pawn();
Pawn pawn11 = new Pawn();
Pawn pawn12 = new Pawn();
Pawn pawn13 = new Pawn();
Pawn pawn14 = new Pawn();
Pawn pawn15 = new Pawn();
Pawn pawn16 = new Pawn();
rook rook3 = new rook();
rook rook4 = new rook();
knight knight3 = new knight();
knight knight4 = new knight();
bishop bishop3 = new bishop();
bishop bishop4 = new bishop();
Queen queen2 = new Queen();
King king2 = new King();
Pawn[] pawnObjects = {pawn9, pawn10, pawn11, pawn12, pawn13, pawn14, pawn15, pawn16};
rook[] rookObjects = {rook3, rook4};
bishop[] bishopObjects = {bishop3, bishop4};
knight[] knightObjects = {knight3, knight4};
String[] AIPieces = {"|PA|", "|PB|", "|PC|", "|PD|", "|PE|", "|PF|", "|PG|", "|PH|",
"|R3|", "|R4|",
"|B3|", "|B4|",
"|K3|", "|K4|",
"|Q2|", "|Ki|"};
String[] playerPieces = {"|P1|", "|P2|", "|P3|", "|P4|", "|P5|", "|P6|", "|P7|", "|P8|",
"|R1|", "|R2|",
"|B1|", "|B2|",
"|K1|", "|K2|",
"|Q1|", "|KI|"};
// The function below is used to check if the player has taken one of the AI's pieces.
public void AIChecker(String chessPiece) {
for (int i = 0; i < AIPieces.length; i++) {
//System.out.println(chessPiece);
//System.out.println(i);
if (chessPiece.equals("|__|")) {
return;
} else if (chessPiece.equals(AIPieces[i]) && i < 8) {
// Used to loop through the AIPawnObjects to set their values to dead.
for (int x = 0; x < pawnObjects.length; x++) {
if ((pawnObjects[x].chessPiece).equals(chessPiece)) {
pawnObjects[x].dead = true;
//System.out.println("pawn");
return;
}
}
} else if (i == 8 || i == 9) {
for (int x = 0; x < rookObjects.length; x++) {
if ((rookObjects[x].chessPiece).equals(chessPiece)) {
rookObjects[x].dead = true;
//System.out.println("rook");
return;
}
}
} else if (i == 10 || i == 11) {
for (int x = 0; x < bishopObjects.length; x++) {
if ((bishopObjects[x].chessPiece).equals(chessPiece)) {
bishopObjects[x].dead = true;
//System.out.println("bishop");
return;
}
}
} else if (i == 12 || i == 13) {
for (int x = 0; x < knightObjects.length; x++) {
if ((knightObjects[x].chessPiece).equals(chessPiece)) {
knightObjects[x].dead = true;
//System.out.println("knight");
return;
}
}
} else if (i == 15) {
queen2.dead = true;
//System.out.println("king");
return;
} else if (i == 14) {
king2.dead = true;
//System.out.println("queen");
return;
}
}
}
public void rookAssignment() {
rook3.rowAssignment(8);
rook3.columnAssignment(1);
rook3.chessBoardAssignemnt(chessBoard);
rook3.chessPieceLook("|R3|");
rook3.setSpace(rook3.row, rook3.column);
rook4.rowAssignment(8);
rook4.columnAssignment(8);
rook4.chessBoardAssignemnt(chessBoard);
rook4.chessPieceLook("|R4|");
rook4.setSpace(rook4.row, rook4.column);
}
// The function below are used to assign the bishops on the chessboard.
public void bishopAssignment() {
bishop3.rowAssignment(8);
bishop3.columnAssignment(2);
bishop3.chessBoardAssignemnt(chessBoard);
bishop3.chessPieceLook("|B3|");
bishop3.setSpace(bishop3.row, bishop3.column);
bishop4.rowAssignment(8);
bishop4.columnAssignment(7);
bishop4.chessBoardAssignemnt(chessBoard);
bishop4.chessPieceLook("|B4|");
bishop4.setSpace(bishop4.row, bishop4.column);
}
// The function is used to setup the horses on the board.
public void knightAssignment() {
knight3.rowAssignment(8);
knight3.columnAssignment(3);
knight3.chessBoardAssignemnt(chessBoard);
knight3.chessPieceLook("|K3|");
knight3.setSpace(knight3.row, knight3.column);
knight4.rowAssignment(8);
knight4.columnAssignment(6);
knight4.chessBoardAssignemnt(chessBoard);
knight4.chessPieceLook("|K4|");
knight4.setSpace(knight4.row, knight4.column);
}
// The function is used to setup the queen on the board.
public void queenAssignment() {
// AI Team Assignment
queen2.rowAssignment(8);
queen2.columnAssignment(4);
queen2.chessBoardAssignemnt(chessBoard);
queen2.chessPieceLook("|Q2|");
queen2.setSpace(queen2.row, queen2.column);
}
// The function is used to setup the king on the board.
public void kingAssignment() {
// AI Team Assignment
king2.rowAssignment(8);
king2.columnAssignment(5);
king2.chessBoardAssignemnt(chessBoard);
king2.chessPieceLook("|Ki|");
king2.setSpace(king2.row, king2.column);
}
// The function below is used to store the assignment of the pawns.
// This is the inital setup function.
public void pawnAssignment() {
pawn9.rowAssignment(7);
pawn9.columnAssignment(1);
pawn9.chessBoardAssignemnt(chessBoard);
pawn9.chessPieceLook("|PA|");
pawn9.setSpace(pawn9.row, pawn9.column);
pawn10.rowAssignment(7);
pawn10.columnAssignment(2);
pawn10.chessBoardAssignemnt(chessBoard);
pawn10.chessPieceLook("|PB|");
pawn10.setSpace(pawn10.row, pawn10.column);
pawn11.rowAssignment(7);
pawn11.columnAssignment(3);
pawn11.chessBoardAssignemnt(chessBoard);
pawn11.chessPieceLook("|PC|");
pawn11.setSpace(pawn11.row, pawn11.column);
pawn12.rowAssignment(7);
pawn12.columnAssignment(4);
pawn12.chessBoardAssignemnt(chessBoard);
pawn12.chessPieceLook("|PD|");
pawn12.setSpace(pawn12.row, pawn12.column);
pawn13.rowAssignment(7);
pawn13.columnAssignment(5);
pawn13.chessBoardAssignemnt(chessBoard);
pawn13.chessPieceLook("|PE|");
pawn13.setSpace(pawn13.row, pawn13.column);
pawn14.rowAssignment(7);
pawn14.columnAssignment(6);
pawn14.chessBoardAssignemnt(chessBoard);
pawn14.chessPieceLook("|PF|");
pawn14.setSpace(pawn14.row, pawn14.column);
pawn15.rowAssignment(7);
pawn15.columnAssignment(7);
pawn15.chessBoardAssignemnt(chessBoard);
pawn15.chessPieceLook("|PG|");
pawn15.setSpace(pawn15.row, pawn15.column);
pawn16.rowAssignment(7);
pawn16.columnAssignment(8);
pawn16.chessBoardAssignemnt(chessBoard);
pawn16.chessPieceLook("|PH|");
pawn16.setSpace(pawn16.row, pawn16.column);
}
// Set the chessBoard to the one that the player plays on.
public void chessBoardSetterFunc(String[][] boardToPLayOn) {
this.chessBoard = boardToPLayOn;
rookAssignment();
bishopAssignment();
knightAssignment();
queenAssignment();
kingAssignment();
pawnAssignment();
}
// King Move function
// Used to move the king to safety.
// The parameters are used to determine the possible locations that the king can move to.
public void kingMove(boolean Ahead, boolean Back, boolean Right, boolean Left, boolean TopLeft, boolean TopRight, boolean BottomLeft, boolean BottomRight) {
// Move Up and to the Top Left
if (TopLeft == true && king2.row - 1 > 0 && king2.column - 1 > 0 && (this.chessBoard[king2.row - 1][king2.column - 1]).equals("|__|")) {
king2.row -= 1;
king2.column -= 1;
kingDefendFunc();
} // Move Up and to the Top Right
if (TopRight == true && king2.row - 1 > 0 && king2.column + 1 < 9 && (this.chessBoard[king2.row - 1][king2.column + 1]).equals("|__|")) {
king2.row -= 1;
king2.column += 1;
kingDefendFunc();
} // Move Down and to the Left
else if (BottomLeft == true && king2.row + 1 < 9 && king2.column - 1 > 0 && (this.chessBoard[king2.row + 1][king2.column - 1]).equals("|__|")) {
king2.row += 1;
king2.column -= 1;
kingDefendFunc();
} // Move Down and to the Right
else if (BottomRight == true && king2.row + 1 < 0 && king2.column + 1 < 8 && (this.chessBoard[king2.row + 1][king2.column + 1]).equals("|__|")) {
king2.row += 1;
king2.column += 1;
kingDefendFunc();
} // Move Right
else if (Right == true && king2.column + 1 < 8 && (this.chessBoard[king2.row][king2.column + 1]).equals("|__|")) {
king2.column += 1;
kingDefendFunc();
} // Move Left
else if (Left == true && king2.column - 1 > 0 && (this.chessBoard[king2.row][king2.column - 1]).equals("|__|")) {
king2.column -= 1;
kingDefendFunc();
} // Move Ahead
else if (Ahead == true && king2.column - 1 > 0 && (this.chessBoard[king2.row - 1][king2.column]).equals("|__|")) {
king2.row -= 1;
kingDefendFunc();
} // Move Back
else if (Back == true && king2.row + 1 < 9 && (this.chessBoard[king2.row + 1][king2.column]).equals("|__|")) {
king2.row += 1;
kingDefendFunc();
} else {
System.out.print("Checkmate");
System.exit(0);
}
}
// The function below is executed when the King chess piece is under attack.
public void kingDefendFunc() {
// The function below is used to check possible attacks from all directions.
for (int i = 1; i < 8; i++) {
// Used to check all of the player pieces.
for (int x = 0; x < playerPieces.length; x++) {
// Pawn attack from the Top Left
if (x < 8 && (this.chessBoard[king2.row - 1][king2.column - 1]).equals(playerPieces[x])) {
kingMove(true, true, true, true, false, true, true, true);
}
// Pawn attack from the right
else if (x < 8 && (this.chessBoard[king2.row - 1][king2.column + 1]).equals(playerPieces[x])) {
kingMove(true, true, true, true, true, false, true, true);
}
// Attack from Rook Straight Above
else if (x == 8 || x == 9 && (this.chessBoard[king2.row - i][king2.column]).equals(x) || (this.chessBoard[king2.row - i][king2.column]).equals(playerPieces[14])) {
kingMove(false, false, true, true, true, true, true, true);
}
// Attack from Rook Straight Below
else if (x == 8 || x == 9 && (this.chessBoard[king2.row + i][king2.column]).equals(x) || (this.chessBoard[king2.row + i][king2.column]).equals(playerPieces[14])) {
kingMove(false, false, true, true, true, true, true, true);
}
// Attack from Rook Right
else if (x == 8 || x == 9 && (this.chessBoard[king2.row][king2.column + i]).equals(x) || (this.chessBoard[king2.row][king2.column + i]).equals(playerPieces[14])) {
kingMove(true, true, false, false, true, true, true, true);
}
// Attack from Rook Left
else if (x == 8 || x == 9 && (this.chessBoard[king2.row][king2.column - i]).equals(x) || (this.chessBoard[king2.row][king2.column + i]).equals(playerPieces[14])) {
kingMove(true, true, false, false, true, true, true, true);
}
// Attacks from above and to the left (Bishops & Queen)
else if ((this.chessBoard[king2.row - x][king2.column - x]).equals(playerPieces[10]) || (this.chessBoard[king2.row - x][king2.column - x]).equals(playerPieces[11]) || (this.chessBoard[king2.row - x][king2.column - x]).equals(playerPieces[14])) {
kingMove(true, true, true, true, false, true, true, false);
}
// Attack from the top Right (Bishops Only)
else if ((this.chessBoard[king2.row - x][king2.column + x]).equals(playerPieces[10]) || (this.chessBoard[king2.row - x][king2.column + x]).equals(playerPieces[11]) || (this.chessBoard[king2.row - x][king2.column + x]).equals(playerPieces[14])) {
kingMove(true, true, true, true, true, false, false, true);
}
// Attack from the Bottom Left (Bishop Only)
else if ((this.chessBoard[king2.row + x][king2.column - x]).equals(playerPieces[10]) || (this.chessBoard[king2.row + x][king2.column - x]).equals(playerPieces[11]) || (this.chessBoard[king2.row + x][king2.column - x]).equals(playerPieces[14])) {
kingMove(true, true, true, true, true, false, false, true);
}
// Attack from the Bottom Right (Bishop Only)
else if ((this.chessBoard[king2.row + x][king2.column + x]).equals(playerPieces[10]) || (this.chessBoard[king2.row + x][king2.column + x]).equals(playerPieces[11]) || (this.chessBoard[king2.row + x][king2.column + x]).equals(playerPieces[14])) {
kingMove(true, true, true, true, false, true, true, false);
}
// Attack from the Bottom Left (Knight Only (Sideways L))
else if ((this.chessBoard[king2.row + 1][king2.column - 2]).equals(playerPieces[12]) || (this.chessBoard[king2.row + 1][king2.column - 2]).equals(playerPieces[13])) {
kingMove(true, true, true, true, true, true, true, true);
}
// Attack from the Bottom Right (Knight Only (Sideways L))
else if ((this.chessBoard[king2.row + 1][king2.column + 2]).equals(playerPieces[12]) || (this.chessBoard[king2.row + 1][king2.column + 2]).equals(playerPieces[13])) {
kingMove(true, true, true, true, true, true, true, true);
}
// Attack from the Top Left (Knight Only (Sideways L))
else if ((this.chessBoard[king2.row - 1][king2.column - 2]).equals(playerPieces[12]) || (this.chessBoard[king2.row - 1][king2.column - 2]).equals(playerPieces[13])) {
kingMove(true, true, true, true, true, true, true, true);
}
// Attack from the Top Right (Knight Only (Sideways L))
else if ((this.chessBoard[king2.row - 1][king2.column + 2]).equals(playerPieces[12]) || (this.chessBoard[king2.row - 1][king2.column + 2]).equals(playerPieces[13])) {
kingMove(true, true, true, true, true, true, true, true);
}
// Attack from the Top Left (Knight Only (Standing L))
else if ((this.chessBoard[king2.row - 2][king2.column - 1]).equals(playerPieces[12]) || (this.chessBoard[king2.row - 2][king2.column - 1]).equals(playerPieces[13])) {
kingMove(true, true, true, true, true, true, true, true);
}
// Attack from the Top Right (Knight Only (Standing L))
else if ((this.chessBoard[king2.row - 2][king2.column + 1]).equals(playerPieces[12]) || (this.chessBoard[king2.row - 2][king2.column + 1]).equals(playerPieces[13])) {
kingMove(true, true, true, true, true, true, true, true);
}
// Attack from the Bottom Left (Knight Only (Standing L))
else if ((this.chessBoard[king2.row + 2][king2.column - 1]).equals(playerPieces[12]) || (this.chessBoard[king2.row + 2][king2.column - 1]).equals(playerPieces[13])) {
kingMove(true, true, true, true, true, true, true, true);
}
// Attack from the Bottom Right (Knight Only (Standing L))
else if ((this.chessBoard[king2.row + 2][king2.column + 1]).equals(playerPieces[12]) || (this.chessBoard[king2.row + 2][king2.column + 1]).equals(playerPieces[13])) {
kingMove(true, true, true, true, true, true, true, true);
}
}
}
this.kingSafe = true;
}
// Used in case there is no other better move
// It simply moves one of the pieces forward.
public void defaultMove() {
for (int i = 0; i < AIPieces.length; i++) {
// Pawns
if (i <= 7) {
for (int x = 0; x < pawnObjects.length; x++) {
if (pawnObjects[x].dead == false && pawnObjects[x].allowedMovesAI(pawnObjects[x].row - 1, pawnObjects[x].column) == true) {
chessBoard[pawnObjects[x].row][pawnObjects[x].column] = pawnObjects[x].chessPiece;
chessBoard[pawnObjects[x].row + 1][pawnObjects[x].column] = "|__|";
return;
}
}
}
// Rooks
else if (i == 8 || i == 9) {
for (int x = 0; x < rookObjects.length; x++) {
// Move Up
if (rookObjects[x].dead == false && rookObjects[x].row - 1 > 0 && rookObjects[x].allowedMovesAI(rookObjects[x].row - 1, rookObjects[x].column) == true) {
chessBoard[rookObjects[x].row][rookObjects[x].column] = rookObjects[x].chessPiece;
chessBoard[rookObjects[x].row + 1][rookObjects[x].column] = "|__|";
return;
} // Move Down
else if (rookObjects[x].dead == false && rookObjects[x].row + 1 < 9 && rookObjects[x].allowedMovesAI(rookObjects[x].row + 1, rookObjects[x].column) == true) {
chessBoard[rookObjects[x].row][rookObjects[x].column] = rookObjects[x].chessPiece;
chessBoard[rookObjects[x].row - 1][rookObjects[x].column] = "|__|";
return;
} // Move Left
else if (rookObjects[x].dead == false && rookObjects[x].column - 1 > 0 && rookObjects[x].allowedMovesAI(rookObjects[x].row, rookObjects[x].column - 1) == true) {
chessBoard[rookObjects[x].row][rookObjects[x].column] = rookObjects[x].chessPiece;
chessBoard[rookObjects[x].row][rookObjects[x].column + 1] = "|__|";
return;
} // Move Right
else if (rookObjects[x].dead == false && rookObjects[x].column + 1 < 9 && rookObjects[x].allowedMovesAI(rookObjects[x].row, rookObjects[x].column + 1) == true) {
chessBoard[rookObjects[x].row][rookObjects[x].column] = rookObjects[x].chessPiece;
chessBoard[rookObjects[x].row][rookObjects[x].column - 1] = "|__|";
return;
}
}
}
// Bishop
else if (i == 10 || i == 11) {
for (int x = 0; x < bishopObjects.length; x++) {
// Move Up and Left
if (bishopObjects[x].dead == false && bishopObjects[x].row - 1 > 0 && bishopObjects[x].column - 1 > 0 && bishopObjects[x].allowedMovesAI(bishopObjects[x].row - 1, bishopObjects[x].column - 1)) {
chessBoard[bishopObjects[x].row][bishopObjects[x].column] = bishopObjects[x].chessPiece;
chessBoard[bishopObjects[x].row + 1][bishopObjects[x].column + 1] = "|__|";
return;
}
// Move Up and Right
else if (bishopObjects[x].dead == false && bishopObjects[x].row - 1 > 0 && bishopObjects[x].column + 1 < 9 && bishopObjects[x].allowedMovesAI(bishopObjects[x].row - 1, bishopObjects[x].column + 1)) {
chessBoard[bishopObjects[x].row][bishopObjects[x].column] = bishopObjects[x].chessPiece;
chessBoard[bishopObjects[x].row + 1][bishopObjects[x].column - 1] = "|__|";
return;
}
// Move Down and Left
else if (bishopObjects[x].dead == false && bishopObjects[x].row + 1 < 9 && bishopObjects[x].column - 1 > 0 && bishopObjects[x].allowedMovesAI(bishopObjects[x].row + 1, bishopObjects[x].column - 1)) {
chessBoard[bishopObjects[x].row][bishopObjects[x].column] = bishopObjects[x].chessPiece;
chessBoard[bishopObjects[x].row - 1][bishopObjects[x].column + 1] = "|__|";
return;
}
// Move Down and Right
else if (bishopObjects[x].dead == false && bishopObjects[x].row + 1 < 9 && bishopObjects[x].column + 1 < 9 && bishopObjects[x].allowedMovesAI(bishopObjects[x].row + 1, bishopObjects[x].column + 1)) {
chessBoard[bishopObjects[x].row][bishopObjects[x].column] = bishopObjects[x].chessPiece;
chessBoard[bishopObjects[x].row - 1][bishopObjects[x].column - 1] = "|__|";
return;
}
}
}
// Knight
else if (i == 12 || i == 13) {
for (int x = 0; x < knightObjects.length; x++) {
// Up and to the Left (2 Up)
if (knightObjects[x].dead == false && knightObjects[x].row - 2 > 0 && knightObjects[x].column - 1 > 0 && knightObjects[x].allowedMovesAI(knightObjects[x].row - 2, knightObjects[x].column - 1)) {
chessBoard[knightObjects[x].row][knightObjects[x].column] = knightObjects[x].chessPiece;
chessBoard[knightObjects[x].row + 2][knightObjects[x].column + 1] = "|__|";
return;
}
// Up and to the Right (2 Up)
else if (knightObjects[x].dead == false && knightObjects[x].row - 2 > 0 && knightObjects[x].column + 1 < 9 && knightObjects[x].allowedMovesAI(knightObjects[x].row - 2, knightObjects[x].column + 1)) {
chessBoard[knightObjects[x].row][knightObjects[x].column] = knightObjects[x].chessPiece;
chessBoard[knightObjects[x].row + 2][knightObjects[x].column - 1] = "|__|";
return;
}
// Down and to the Left (2 Down)
else if (knightObjects[x].dead == false && knightObjects[x].row + 2 < 9 && knightObjects[x].column - 1 > 0 && knightObjects[x].allowedMovesAI(knightObjects[x].row + 2, knightObjects[x].column - 1)) {
chessBoard[knightObjects[x].row][knightObjects[x].column] = knightObjects[x].chessPiece;
chessBoard[knightObjects[x].row - 2][knightObjects[x].column + 1] = "|__|";
return;
}
// Down and to the Right (2 Down)
else if (knightObjects[x].dead == false && knightObjects[x].row + 2 < 9 && knightObjects[x].column + 1 < 9 && knightObjects[x].allowedMovesAI(knightObjects[x].row + 2, knightObjects[x].column + 1)) {
chessBoard[knightObjects[x].row][knightObjects[x].column] = knightObjects[x].chessPiece;
chessBoard[knightObjects[x].row - 2][knightObjects[x].column - 1] = "|__|";
return;
}
// Up and to the Left (1 Up)
else if (knightObjects[x].dead == false && knightObjects[x].row - 1 > 0 && knightObjects[x].column - 2 > 0 && knightObjects[x].allowedMovesAI(knightObjects[x].row - 1, knightObjects[x].column - 2)) {
chessBoard[knightObjects[x].row][knightObjects[x].column] = knightObjects[x].chessPiece;
chessBoard[knightObjects[x].row + 1][knightObjects[x].column + 2] = "|__|";
return;
}
// Up and to the Right (1 Up)
else if (knightObjects[x].dead == false && knightObjects[x].row - 1 > 0 && knightObjects[x].column + 2 < 9 && knightObjects[x].allowedMovesAI(knightObjects[x].row - 1, knightObjects[x].column + 2)) {
chessBoard[knightObjects[x].row][knightObjects[x].column] = knightObjects[x].chessPiece;
chessBoard[knightObjects[x].row + 1][knightObjects[x].column - 2] = "|__|";
return;
}
// Up and to the Left (1 Up)
else if (knightObjects[x].dead == false && knightObjects[x].row + 1 < 9 && knightObjects[x].column - 2 > 0 && knightObjects[x].allowedMovesAI(knightObjects[x].row + 1, knightObjects[x].column - 2)) {
chessBoard[knightObjects[x].row][knightObjects[x].column] = knightObjects[x].chessPiece;
chessBoard[knightObjects[x].row - 1][knightObjects[x].column + 2] = "|__|";
return;
}
// Up and to the Left (1 Up)
else if (knightObjects[x].dead == false && knightObjects[x].row + 1 < 9 && knightObjects[x].column + 2 < 9 && knightObjects[x].allowedMovesAI(knightObjects[x].row + 1, knightObjects[x].column + 2)) {
chessBoard[knightObjects[x].row][knightObjects[x].column] = knightObjects[x].chessPiece;
chessBoard[knightObjects[x].row - 1][knightObjects[x].column - 2] = "|__|";
return;
}
}
}
// Queen
else if (i == 14) {
// Move Up
if (queen2.dead == false && queen2.row - 1 > 0 && queen2.allowedMovesAI(queen2.row - 1, queen2.column) == true) {
chessBoard[queen2.row][queen2.column] = queen2.chessPiece;
chessBoard[queen2.row + 1][queen2.column] = "|__|";
return;
} // Move Down
else if (queen2.dead == false && queen2.row + 1 < 9 && queen2.allowedMovesAI(queen2.row + 1, queen2.column) == true) {
chessBoard[queen2.row][queen2.column] = queen2.chessPiece;
chessBoard[queen2.row - 1][queen2.column] = "|__|";
return;
} // Move Left
else if (queen2.dead == false && queen2.column - 1 > 0 && queen2.allowedMovesAI(queen2.row, queen2.column - 1) == true) {
chessBoard[queen2.row][queen2.column] = queen2.chessPiece;
chessBoard[queen2.row][queen2.column + 1] = "|__|";
return;
} // Move Right
else if (queen2.dead == false && queen2.column + 1 < 9 && queen2.allowedMovesAI(queen2.row, queen2.column + 1) == true) {
chessBoard[queen2.row][queen2.column] = queen2.chessPiece;
chessBoard[queen2.row][queen2.column - 1] = "|__|";
return;
}
// Move Up and Left
if (queen2.dead == false && queen2.row - 1 > 0 && queen2.column - 1 > 0 && queen2.allowedMovesAI(queen2.row - 1, queen2.column - 1)) {
chessBoard[queen2.row][queen2.column] = queen2.chessPiece;
chessBoard[queen2.row + 1][queen2.column + 1] = "|__|";
return;
}
// Move Up and Right
else if (queen2.dead == false && queen2.row - 1 > 0 && queen2.column + 1 < 9 && queen2.allowedMovesAI(queen2.row - 1, queen2.column + 1)) {
chessBoard[queen2.row][queen2.column] = queen2.chessPiece;
chessBoard[queen2.row + 1][queen2.column - 1] = "|__|";
return;
}
// Move Down and Left
else if (queen2.dead == false && queen2.row + 1 < 9 && queen2.column - 1 > 0 && queen2.allowedMovesAI(queen2.row + 1, queen2.column - 1)) {
chessBoard[queen2.row][queen2.column] = queen2.chessPiece;
chessBoard[queen2.row - 1][queen2.column + 1] = "|__|";
return;
}
// Move Down and Right
else if (queen2.dead == false && queen2.row + 1 < 8 && queen2.column + 1 < 9 && queen2.allowedMovesAI(queen2.row + 1, queen2.column + 1)) {
chessBoard[queen2.row][queen2.column] = queen2.chessPiece;
chessBoard[queen2.row - 1][queen2.column - 1] = "|__|";
return;
}
}
}
}
// The function is used to check all of the AI chess pieces and sees which of them will have the shortest path to taking the piece that the player just moved.
public boolean attackMove() {
for (int i = 0; i < AIPieces.length; i++) {
// AI for Pawns
if (i <= 7) {
for (int x = 0; x < pawnObjects.length; x++) {
//System.out.println(x);
if (pawnObjects[x].dead == false && pawnObjects[x].allowedMovesAI(pawnObjects[x].row - 1, pawnObjects[x].column - 1) == true && pawnObjects[x].dead == false) {
chessBoard[pawnObjects[x].row][pawnObjects[x].column] = pawnObjects[x].chessPiece;
chessBoard[pawnObjects[x].row + 1][pawnObjects[x].column + 1] = "|__|";
//System.out.println("true");
return true;
} else if (pawnObjects[x].dead == false && (pawnObjects[x].column + 1 < 9) && pawnObjects[x].allowedMovesAI(pawnObjects[x].row - 1, pawnObjects[x].column + 1) == true && pawnObjects[x].dead == false) {
chessBoard[pawnObjects[x].row][pawnObjects[x].column] = pawnObjects[x].chessPiece;
chessBoard[pawnObjects[x].row + 1][pawnObjects[x].column - 1] = "|__|";
return true;
}
}
}
// AI for Rooks
else if (i == 8 || i == 9) {
for (int x = 0; x < rookObjects.length; x++) {
for (int y = 1; y <= 7; y++) {
// Possible attack up
if (rookObjects[x].dead == false && rookObjects[x].row - y > 0 && rookObjects[x].allowedMovesAI(rookObjects[x].row - y, rookObjects[x].column) == true && rookObjects[x].dead == false) {
//System.out.println("up");
chessBoard[rookObjects[x].row][rookObjects[x].column] = rookObjects[x].chessPiece;
chessBoard[rookObjects[x].row + y][rookObjects[x].column] = "|__|";
return true;
} // Possible attack down
else if (rookObjects[x].dead == false && rookObjects[x].row + y < 9 && rookObjects[x].allowedMovesAI(rookObjects[x].row + y, rookObjects[x].column) == true && rookObjects[x].dead == false) {
//System.out.println("down");
chessBoard[rookObjects[x].row][rookObjects[x].column] = rookObjects[x].chessPiece;
chessBoard[rookObjects[x].row - y][rookObjects[x].column] = "|__|";
return true;
} // Possible attack left
else if (rookObjects[x].dead == false && rookObjects[x].column - y > 0 && rookObjects[x].allowedMovesAI(rookObjects[x].row, rookObjects[x].column - y) == true && rookObjects[x].dead == false) {
//System.out.println("left");
chessBoard[rookObjects[x].row][rookObjects[x].column] = rookObjects[x].chessPiece;
chessBoard[rookObjects[x].row][rookObjects[x].column + y] = "|__|";
return true;
} // Possible attack right
else if (rookObjects[x].dead == false && rookObjects[x].column + y < 9 && rookObjects[x].allowedMovesAI(rookObjects[x].row, rookObjects[x].column + y) == true && rookObjects[x].dead == false) {
//System.out.println("right");
chessBoard[rookObjects[x].row][rookObjects[x].column] = rookObjects[x].chessPiece;
chessBoard[rookObjects[x].row][rookObjects[x].column - y] = "|__|";
return true;
}
}
}
}
// AI for Bishops
else if (i == 10 || i == 11) {
for (int x = 0; x < bishopObjects.length; x++) {
for (int y = 1; y <= 7; y++) {
// Bishop Attack to Top Left
if (bishopObjects[x].dead == false && bishopObjects[x].row - y > 0 && bishopObjects[x].column - y > 0 && bishopObjects[x].allowedMovesAI(bishopObjects[x].row - y, bishopObjects[x].column - y) == true && bishopObjects[x].dead == false) {
chessBoard[bishopObjects[x].row][bishopObjects[x].column] = bishopObjects[x].chessPiece;
chessBoard[bishopObjects[x].row + y][bishopObjects[x].column + y] = "|__|";
return true;
} // Bishop Attack to Top Right
else if (bishopObjects[x].dead == false && bishopObjects[x].row - y > 0 && ((bishopObjects[x].column + y) < 9) && bishopObjects[x].allowedMovesAI(bishopObjects[x].row - y, bishopObjects[x].column + y) == true && bishopObjects[x].dead == false) {
//System.out.println("Bishop");
chessBoard[bishopObjects[x].row][bishopObjects[x].column] = bishopObjects[x].chessPiece;
chessBoard[bishopObjects[x].row + y][bishopObjects[x].column - y] = "|__|";
return true;
} // Bishop Attack to Bottom Right
else if (bishopObjects[x].dead == false && bishopObjects[x].row + y < 9 && bishopObjects[x].column - y > 0 && bishopObjects[x].allowedMovesAI(bishopObjects[x].row + y, bishopObjects[x].column - y) == true && bishopObjects[x].dead == false) {
chessBoard[bishopObjects[x].row][bishopObjects[x].column] = bishopObjects[x].chessPiece;
chessBoard[bishopObjects[x].row - y][bishopObjects[x].column + y] = "|__|";
return true;
} // Bishop Attack to Bottom Left
else if (bishopObjects[x].dead == false && bishopObjects[x].row + y < 9 && bishopObjects[x].column + y < 9 && bishopObjects[x].allowedMovesAI(bishopObjects[x].row + y, bishopObjects[x].column + y) == true && bishopObjects[x].dead == false) {
chessBoard[bishopObjects[x].row][bishopObjects[x].column] = bishopObjects[x].chessPiece;
chessBoard[bishopObjects[x].row - y][bishopObjects[x].column - y] = "|__|";
return true;
}
}
}
}
// AI for Knights
else if (i == 12 || i == 13) {
for (int x = 0; x < knightObjects.length; x++) {
// Attack Up and to the Right
if (knightObjects[x].dead == false && (knightObjects[x].row - 2) > 0 && (knightObjects[x].column + 1) < 9 && knightObjects[x].allowedMovesAI(knightObjects[x].row - 2, knightObjects[x].column + 1)) {
chessBoard[knightObjects[x].row][knightObjects[x].column] = knightObjects[x].chessPiece;
chessBoard[knightObjects[x].row + 2][knightObjects[x].column - 1] = "|__|";
return true;
} // Attack Up and to the Left
if (knightObjects[x].dead == false && (knightObjects[x].row - 2) > 0 && (knightObjects[x].column - 1) > 0 && knightObjects[x].allowedMovesAI(knightObjects[x].row - 2, knightObjects[x].column - 1)) {
chessBoard[knightObjects[x].row][knightObjects[x].column] = knightObjects[x].chessPiece;
chessBoard[knightObjects[x].row + 2][knightObjects[x].column + 1] = "|__|";
return true;
} // Attack Down and to the Left
if (knightObjects[x].dead == false && (knightObjects[x].row + 2) < 9 && (knightObjects[x].column - 1) > 0 && knightObjects[x].allowedMovesAI(knightObjects[x].row + 2, knightObjects[x].column - 1)) {
chessBoard[knightObjects[x].row][knightObjects[x].column] = knightObjects[x].chessPiece;
chessBoard[knightObjects[x].row - 2][knightObjects[x].column + 1] = "|__|";
return true;
} // Attack Down and to the Right
if (knightObjects[x].dead == false && (knightObjects[x].row + 2) < 9 && (knightObjects[x].column + 1) < 9 && knightObjects[x].allowedMovesAI(knightObjects[x].row + 2, knightObjects[x].column + 1)) {
chessBoard[knightObjects[x].row][knightObjects[x].column] = knightObjects[x].chessPiece;
chessBoard[knightObjects[x].row - 2][knightObjects[x].column - 1] = "|__|";
return true;
}
}
}
// AI for Queen
else if (i == 14) {
for (int y = 1; y <= 7; y++) {
// Possible attack up
if (queen2.dead == false && queen2.row - y > 0 && queen2.allowedMovesAI(queen2.row - y, queen2.column) == true && queen2.dead == false) {
//System.out.println("up");
chessBoard[queen2.row][queen2.column] = queen2.chessPiece;
chessBoard[queen2.row + y][queen2.column] = "|__|";
return true;
} // Possible attack down
else if (queen2.dead == false && queen2.row + y < 9 && queen2.allowedMovesAI(queen2.row + y, queen2.column) == true && queen2.dead == false) {
//System.out.println("down");
chessBoard[queen2.row][queen2.column] = queen2.chessPiece;
chessBoard[queen2.row - y][queen2.column] = "|__|";
return true;
} // Possible attack left
else if (queen2.dead == false && queen2.column - y > 0 && queen2.allowedMovesAI(queen2.row, queen2.column - y) == true && queen2.dead == false) {
//System.out.println("left");
chessBoard[queen2.row][queen2.column] = queen2.chessPiece;
chessBoard[queen2.row][queen2.column + y] = "|__|";
return true;
} // Possible attack right
else if (queen2.dead == false && queen2.column + y < 9 && queen2.allowedMovesAI(queen2.row, queen2.column + y) == true && queen2.dead == false) {
//System.out.println("right");
chessBoard[queen2.row][queen2.column] = queen2.chessPiece;
chessBoard[queen2.row][queen2.column - y] = "|__|";
return true;
} // Bishop Attack to Top Left
else if (queen2.dead == false && queen2.row - y > 0 && queen2.column - y > 0 && queen2.allowedMovesAI(queen2.row - y, queen2.column - y) == true && queen2.dead == false) {
chessBoard[queen2.row][queen2.column] = queen2.chessPiece;
chessBoard[queen2.row + y][queen2.column + y] = "|__|";
return true;
} // Bishop Attack to Top Right
else if (queen2.dead == false && queen2.row - y > 0 && ((queen2.column + y) < 9) && queen2.allowedMovesAI(queen2.row - y, queen2.column + y) == true && queen2.dead == false) {
chessBoard[queen2.row][queen2.column] = queen2.chessPiece;
chessBoard[queen2.row + y][queen2.column - y] = "|__|";
return true;
} // Bishop Attack to Bottom Right
else if (queen2.dead == false && queen2.row + y < 9 && queen2.column - y > 0 && queen2.allowedMovesAI(queen2.row + y, queen2.column - y) == true && queen2.dead == false) {
chessBoard[queen2.row][queen2.column] = queen2.chessPiece;
chessBoard[queen2.row - y][queen2.column + y] = "|__|";
return true;
} // Bishop Attack to Bottom Left
else if (queen2.dead == false && queen2.row + y < 9 && queen2.column + y < 9 && queen2.allowedMovesAI(queen2.row + y, queen2.column + y) == true && queen2.dead == false) {
chessBoard[queen2.row][queen2.column] = queen2.chessPiece;
chessBoard[queen2.row - y][queen2.column - y] = "|__|";
return true;
}
}
}
// AI For King
else if (i == 15) {
// Possible attack up
if (king2.row - 1 > 0 && king2.allowedMovesAI(king2.row - 1, king2.column) == true && king2.dead == false) {
//System.out.println("up");
chessBoard[king2.row][king2.column] = king2.chessPiece;
chessBoard[king2.row + 1][king2.column] = "|__|";
return true;
} // Possible attack down
else if (king2.row + 1 < 9 && king2.allowedMovesAI(king2.row + 1, king2.column) == true && king2.dead == false) {
//System.out.println("down");
chessBoard[king2.row][king2.column] = king2.chessPiece;
chessBoard[king2.row - 1][king2.column] = "|__|";
return true;
} // Possible attack left
else if (king2.column - 1 > 0 && king2.allowedMovesAI(king2.row, king2.column - 1) == true && king2.dead == false) {
//System.out.println("left");
chessBoard[king2.row][king2.column] = king2.chessPiece;
chessBoard[king2.row][king2.column + 1] = "|__|";
return true;
} // Possible attack right
else if (king2.column + 1 < 9 && king2.allowedMovesAI(king2.row, king2.column + 1) == true && king2.dead == false) {
//System.out.println("right");
chessBoard[king2.row][king2.column] = king2.chessPiece;
chessBoard[king2.row][king2.column - 1] = "|__|";
return true;
} // Bishop Attack to Top Left
else if (king2.row - 1 > 0 && king2.column - 1 > 0 && king2.allowedMovesAI(king2.row - 1, king2.column - 1) == true && king2.dead == false) {
chessBoard[king2.row][king2.column] = king2.chessPiece;
chessBoard[king2.row + 1][king2.column + 1] = "|__|";
return true;
} // Bishop Attack to Top Right
else if (king2.row - 1 > 0 && ((king2.column + 1) < 9) && king2.allowedMovesAI(king2.row - 1, king2.column + 1) == true && king2.dead == false) {
chessBoard[king2.row][king2.column] = king2.chessPiece;
chessBoard[king2.row + 1][king2.column - 1] = "|__|";
return true;
} // Bishop Attack to Bottom Right
else if (king2.row + 1 < 9 && king2.column - 1 > 0 && king2.allowedMovesAI(king2.row + 1, king2.column - 1) == true && king2.dead == false) {
chessBoard[king2.row][king2.column] = king2.chessPiece;
chessBoard[king2.row - 1][king2.column + 1] = "|__|";
return true;
} // Bishop Attack to Bottom Left
else if (king2.row + 1 < 9 && king2.column + 1 < 9 && king2.allowedMovesAI(king2.row + 1, king2.column + 1) == true && king2.dead == false) {
chessBoard[king2.row][king2.column] = king2.chessPiece;
chessBoard[king2.row - 1][king2.column - 1] = "|__|";
return true;
}
}
}
return false;
}
// The function below is used to defend against the players team's chess pieces.
// The for loops are used to iterate throughout the ches board and see which pieces are the computers and which are the player's.
public String[][] defendFunction(int movedRow, int movedColumn) {
if (attackMove() == true) {
return chessBoard;
} else {
defaultMove();
return chessBoard;
}
}
}