-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessModel
More file actions
1100 lines (1010 loc) · 37.9 KB
/
Copy pathChessModel
File metadata and controls
1100 lines (1010 loc) · 37.9 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
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.util.ArrayList;
import java.util.Collections;
/*****************************************************************************
* This class creates the chess game and is brains behind the GUI. every
* decision is made in this class.
*
* @author Nathan Kelderman and Matt Shampine
* @version 1.0
***************************************************************************/
public class ChessModel implements IChessModel {
/** a double array that stores all the pieces at their location on the
* board */
private IChessPiece[][] board;
/** stores the current player */
private Player player;
/** sets board size to 8 */
private final int BOARD_SIZE = 8;
/** stores if it is the first click or not */
private boolean firstClick;
/** stores the number of moves the AI has done for random moves */
private int numMoves;
/** stores 8 moves for the AI to use randomly */
private ArrayList<Move> randomMoves;
/** stores the best move for the AI to use for the next move */
private Move bestMove;
/** stores a possibly best move for the AI which is used to check against
* the current best move */
private Move possibleMove;
/** stores the last move the AI did so it doesn't move back to that
* spot */
private Move lastMove;
/** used to copy the current board so it can be manipulated without
* moving pieces on the actual board*/
private IChessPiece[][] tempBoard;
/** stores whether or not the AI is turned on or off */
private boolean AIEnabled;
/** stores all the dead white pieces */
private ArrayList<IChessPiece> whitePieces;
/** stores all the dead black pieces */
private ArrayList<IChessPiece> blackPieces;
/*************************************************************************
* The constructor resets the board to the default chess board and sets
* the AI to be on.
*
***********************************************************************/
public ChessModel() {
reset();
setAIEnabled(true);
}
/*************************************************************************
* The reset method sets the board to a default board with all the pieces
* positions set to the normal spots on the board according to chess
* rules. It also duplicates the board to the tempBoard in case a later
* method uses the tempBoard.
*
***********************************************************************/
public void reset() {
board = new IChessPiece[BOARD_SIZE][BOARD_SIZE];
numMoves = 0;
possibleMove = null;
tempBoard = new IChessPiece[BOARD_SIZE][BOARD_SIZE];
whitePieces = new ArrayList<IChessPiece>();
blackPieces = new ArrayList<IChessPiece>();
player = Player.BLACK; // setting the AI to black
board[0][0] = new Rook(player);
board[0][1] = new Knight(player);
board[0][2] = new Bishop(player);
board[0][3] = new Queen(player);
board[0][4] = new King(player);
board[0][5] = new Bishop(player);
board[0][6] = new Knight(player);
board[0][7] = new Rook(player);
for (int col = 0; col < 8; col++)
board[1][col] = new Pawn(player);
player = Player.WHITE;
board[7][0] = new Rook(player);
board[7][1] = new Knight(player);
board[7][2] = new Bishop(player);
board[7][3] = new Queen(player);
board[7][4] = new King(player);
board[7][5] = new Bishop(player);
board[7][6] = new Knight(player);
board[7][7] = new Rook(player);
for (int col = 0; col < 8; col++)
board[6][col] = new Pawn(player);
randomMoves = new ArrayList<Move>();
randomMoves.add(new Move(0, 1, 2, 0));
randomMoves.add(new Move(1, 1, 3, 1));
randomMoves.add(new Move(1, 3, 2, 3));
randomMoves.add(new Move(0, 6, 2, 5));
randomMoves.add(new Move(1, 2, 3, 2));
randomMoves.add(new Move(1, 4, 2, 4));
randomMoves.add(new Move(1, 6, 2, 6));
randomMoves.add(new Move(1, 7, 3, 7));
Collections.shuffle(randomMoves);
setFirstClick(true);
duplicateBoard(board);
}
/*************************************************************************
* Checks to see if the spot on the cell if occupied.
*
* @param temp stores a piece
* @return returns true if cell contains a piece and false if not
************************************************************************/
public boolean isCellOccupied(IChessPiece temp) {
if (temp != null)
return true;
return false;
}
/*************************************************************************
* Getter for checking if it is the first click.
*
* @return firstClick returns if it is the first click or not
************************************************************************/
public boolean isFirstClick() {
return firstClick;
}
/*************************************************************************
* Setter for declaring if it is the first click or not.
*
* @param firstClick
************************************************************************/
public void setFirstClick(boolean firstClick) {
this.firstClick = firstClick;
}
/*************************************************************************
* Getter for AIEnabled.
*
* @return returns if AI is turned on or not
************************************************************************/
public boolean isAIEnabled() {
return AIEnabled;
}
/*************************************************************************
* Setter for enabling or disabling the AI.
*
* @param aIEnabled sets the AI to be enabled or disabled
************************************************************************/
public void setAIEnabled(boolean AIEnabled) {
this.AIEnabled = AIEnabled;
}
/*************************************************************************
* Checks if the inputed move is a valid move.
*
* @param move receives a move to check if it is valid
* @see IChessPiece#isValidMove(Move move)
************************************************************************/
public boolean isValidMove(Move move) {
if (board[move.fromRow][move.fromColumn].player() != player) {
throw new IllegalPlayer();
}
if (!castling(move)) {
if (board[move.fromRow][move.fromColumn].isValidMove(move, board))
return true;
else
throw new InvalidMove();
} else
return false;
}
/*************************************************************************
* Promotes a pawn to the highest value piece that is dead and removes
* that piece from the dead pieces array.
*
* @param move takes in a move so it can check if the fromRow and
* fromColumn is a pawn and if it is White or Black
************************************************************************/
public void promotion(Move move) {
int y = 0;
int z = 0;
IChessPiece tempPiece = null;
if (tempBoard[move.fromRow][move.fromColumn].player() ==
Player.WHITE) {
//checks to see if piece reaching the end is a pawn
if (tempBoard[move.fromRow][move.fromColumn].type().
equals("Pawn")) {
//cycles through the dead pieces
for (int x = 0; x < whitePieces.size(); x++) {
if (whitePieces.get(x) != null &&
!whitePieces.get(x).type().equals("Pawn")) {
//sets temp piece to the next highest valued dead piece
if (whitePieces.get(x).getPieceWorth() >= y) {
y = whitePieces.get(x).getPieceWorth();
tempPiece = whitePieces.get(x);
z = x;
}
}
}
}
//sets pawn to the highest dead piece
board[move.toRow][move.toColumn] = tempPiece;
//removes that dead piece from the arrayList
if (whitePieces.size() != 0)
whitePieces.remove(z);
} else {
tempPiece = null;
if (tempBoard[move.fromRow][move.fromColumn].player() ==
Player.BLACK) {
//checks to see if piece reaching the end is a pawn
if (tempBoard[move.fromRow][move.fromColumn].type().
equals("Pawn")) {
//cycles through the dead pieces
for (int x = 0; x < blackPieces.size(); x++) {
if (blackPieces.get(x) != null && !blackPieces.get(x).
type().equals("Pawn")) {
//sets temp piece to the next highest valued dead piece
if (blackPieces.get(x).getPieceWorth() >= y) {
y = blackPieces.get(x).getPieceWorth();
tempPiece = blackPieces.get(x);
z = x;
}
}
}
}
}
//sets pawn to the highest dead piece
board[move.toRow][move.toColumn] = tempPiece;
//removes that dead piece from the arrayList
if (blackPieces.size() != 0)
blackPieces.remove(z);
}
}
/*************************************************************************
* Moves the piece on the board to the new spot if all the requirements
* have been met and promotes the pawn if necessary and stores the
* piece taken out (if a piece is taken out) into the arrayList of dead
* pieces.
*
* @param move is the move that is to be made
* @see IChessModel#move(Move)
************************************************************************/
public void move(Move move) {
IChessPiece temp1 = board[move.toRow][move.toColumn];
//adds killed piece to arrayList
if (temp1 != null) {
if (temp1.player() == Player.WHITE) {
whitePieces.add(temp1);
} else {
blackPieces.add(temp1);
}
}
//checks to see if the piece is a pawn and it is reaching the end
//and if it is then it promotes it
if (pieceAt(move.fromRow, move.fromColumn) != null
&& pieceAt(move.fromRow, move.fromColumn).type().
equals("Pawn") && ((move.toRow == 0 &&
board[move.fromRow][move.fromColumn].player() == Player.WHITE)
|| (move.toRow == 7 && board[move.fromRow][move.fromColumn].
player() == Player.BLACK))
&& player == pieceAt(move.fromRow, move.fromColumn).player()){
promotion(move);
board[move.fromRow][move.fromColumn] = null;
//if the pawn isn't set to anything or just to another pawn then
//it gets set to a queen
if (pieceAt(move.toRow, move.toColumn) == null
|| pieceAt(move.toRow, move.toColumn).
type().equals("Pawn")) {
board[move.toRow][move.toColumn] = new Queen(player);
player = player.next();
//checks if opposite player is in check after the promotion
if ((player == Player.WHITE && inCheck(Player.WHITE))
|| (player == Player.BLACK &&
inCheck(Player.BLACK))) {
if ((player == Player.WHITE && isComplete(Player.WHITE))
|| (player == Player.BLACK &&
isComplete(Player.BLACK))) {
throw new NoDeadPiecesInCheckMate();
} else
throw new NoDeadPiecesInCheck();
}
throw new NoDeadPieces();
}
player = player.next();
//checks if opposite player is in check after the promotion
if ((player == Player.WHITE && inCheck(Player.WHITE))
|| (player == Player.BLACK && inCheck(Player.BLACK))) {
if ((player == Player.WHITE && isComplete(Player.WHITE))
|| (player == Player.BLACK &&
isComplete(Player.BLACK))) {
throw new CheckMate();
} else
throw new Check();
}
} else {
//if there is no promotion the it moves the piece
IChessPiece temp = board[move.fromRow][move.fromColumn];
board[move.toRow][move.toColumn] =
board[move.fromRow][move.fromColumn];
board[move.fromRow][move.fromColumn] = null;
boolean black = inCheck(Player.BLACK);
boolean white = inCheck(Player.WHITE);
boolean blackCM = isComplete(Player.BLACK);
boolean whiteCM = isComplete(Player.WHITE);
//checks if either black or white is in checkmate after the move
if (whiteCM || blackCM) {
//if white is in checkmate and white is trying to move then
//don't allow the move
if (player == Player.WHITE && whiteCM) {
board[move.fromRow][move.fromColumn] = temp;
board[move.toRow][move.toColumn] = temp1;
}
//if black is in checkmate and black is trying to move then
//don't allow the move
if (player == Player.BLACK && blackCM) {
board[move.fromRow][move.fromColumn] = temp;
board[move.toRow][move.toColumn] = temp1;
}
//if white is moving and black is in checkmate then notify the
//players and allow the move
if (player == Player.WHITE && blackCM) {
board[move.fromRow][move.fromColumn] = null;
board[move.toRow][move.toColumn] = temp;
player = player.next();
throw new CheckMate();
}
//if white is in checkmate and black is moving then notify the
//players and allow the move
if (player == Player.BLACK && whiteCM) {
board[move.fromRow][move.fromColumn] = null;
board[move.toRow][move.toColumn] = temp;
player = player.next();
throw new CheckMate();
}
}
//if neither are in checkmate then check if they are in check
//after the move
else {
//if white is trying to move and white is in check then don't
//allow the move
if (player == Player.WHITE && white) {
board[move.fromRow][move.fromColumn] = temp;
board[move.toRow][move.toColumn] = temp1;
//if black is trying to move and black is in check then don't
//allow the move
} else if (player == Player.BLACK && black) {
board[move.fromRow][move.fromColumn] = temp;
board[move.toRow][move.toColumn] = temp1;
} else {
//if white is trying to move and black is in check then
//notify the players and allow the move
if (player == Player.WHITE && black) {
board[move.fromRow][move.fromColumn] = null;
board[move.toRow][move.toColumn] = temp;
player = player.next();
throw new Check();
}
//if black is trying to move and white is in check then
//notify the players and allow the move
if (player == Player.BLACK && white) {
board[move.fromRow][move.fromColumn] = null;
board[move.toRow][move.toColumn] = temp;
player = player.next();
throw new Check();
}
//if neither players is in check or checkmate after the
//move then just allow it
if (!black && !white) {
board[move.toRow][move.toColumn] = temp;
player = player.next();
}
}
}
}
}
/************************************************************************
* Checks to see if Player p is in check. Cycles through all the enemy
* pieces and if on of them can hit the friendly king then Player p is in
* check.
*
* @param p is the player that is being checked if they are in check
* @return returns true if in check and false if not
* @see IChessModel#inCheck(Player)
************************************************************************/
public boolean inCheck(Player p) {
duplicateBoard(board);
//finds a spot on the board that is the opposite player of player 'p'
for (int row = 0; row < BOARD_SIZE; row++)
for (int col = 0; col < BOARD_SIZE; col++) {
if (tempBoard[row][col] != null)
if (tempBoard[row][col].player() != p) {
//checks if that piece can move and hit the king of
//player 'p'
for (int r = 0; r < BOARD_SIZE; r++)
for (int c = 0; c < BOARD_SIZE; c++) {
Move temp = new Move(row, col, r, c);
if (isValidMoveAI(temp) && board[r][c] !=
null)
//if it can then it returns true for king
//being in check
if (tempBoard[r][c].player() == p &&
tempBoard[r][c].type().equals("King"))
return true;
}
}
}
return false;
}
/*************************************************************************
* This inCheck method is used by the isComplete method which moves pieces
* on the tempBoard so we don't want to duplicate the board again like the
* other inCheck method does.
*
* @param p is the player being checked if they are in check or not
* @return returns true if in check or false if not
************************************************************************/
public boolean tempBoardInCheck(Player p) {
//same as inCheck except it doesn't duplicate the board
for (int row = 0; row < BOARD_SIZE; row++)
for (int col = 0; col < BOARD_SIZE; col++) {
if (tempBoard[row][col] != null)
if (tempBoard[row][col].player() != p) {
for (int r = 0; r < BOARD_SIZE; r++)
for (int c = 0; c < BOARD_SIZE; c++) {
Move temp = new Move(row, col, r, c);
if (isValidMoveAI(temp) && tempBoard[r][c] !=
null)
if (tempBoard[r][c].player() == p &&
tempBoard[r][c].type().equals("King"))
return true;
}
}
}
return false;
}
/*************************************************************************
* This method cycles through all of the Player p pieces and moves all of
* them to all of its possible move locations and checks if the king is
* still in check. If the king is still in check after all pieces have
* been moved then the king is in checkmate otherwise if there is a
* piece that can be moved to take the king out of check then the king
* is not in checkmate.
*
* @param p is the player being checked if they are in check
* @return returns true if king is in checkmate and false if not
* @see IChessModel#isComplete(Player)
************************************************************************/
public boolean isComplete(Player p) {
duplicateBoard(board);
//finds a pieces on the board that is of type player 'p'
for (int row = 0; row < BOARD_SIZE; row++)
for (int col = 0; col < BOARD_SIZE; col++) {
if (tempBoard[row][col] != null)
if (tempBoard[row][col].player() == p) {
//tries every possible move of that piece
for (int r = 0; r < BOARD_SIZE; r++)
for (int c = 0; c < BOARD_SIZE; c++) {
Move temp = new Move(row, col, r, c);
IChessPiece tempPiece = tempBoard[row][col];
IChessPiece tempPiece1 = null;
if (tempBoard[r][c] != null)
tempPiece1 = tempBoard[r][c];
if (isValidMoveAI(temp)) {
tempBoard[row][col] = null;
tempBoard[r][c] = tempPiece;
//if that move takes the king out of
//check then that player is not in check
if (!tempBoardInCheck(p)) {
tempBoard[row][col] = tempPiece;
tempBoard[r][c] = tempPiece1;
return false;
}
}
tempBoard[row][col] = tempPiece;
tempBoard[r][c] = tempPiece1;
}
}
}
//if there are no pieces that can move and take the king out of check
//then that means the king is in checkmate so it returns true;
return true;
}
/*************************************************************************
* Creates a new board with the pieces at the same spot as the current
* board so the program can manipulate the tempBoard without changing
* anything on the current board.
*
* @param newBoard is the board that tempBoard is copying
************************************************************************/
public void duplicateBoard(IChessPiece[][] newBoard) {
tempBoard = new IChessPiece[BOARD_SIZE][BOARD_SIZE];
//cycles through the board and if there is a pieces there then it
//creates a new piece on the tempBoard at the same location as the
//one on board and sets the piece to be the exact same one as the one
//on board
for (int row = 0; row < BOARD_SIZE; row++)
for (int col = 0; col < BOARD_SIZE; col++) {
if (board[row][col] != null) {
if (newBoard[row][col].type().equals("Pawn") &&
newBoard[row][col].player() == Player.WHITE) {
tempBoard[row][col] = new Pawn(Player.WHITE);
} else if (newBoard[row][col].type().equals("Pawn"))
tempBoard[row][col] = new Pawn(Player.BLACK);
if (newBoard[row][col].type().equals("Rook") &&
newBoard[row][col].player() == Player.WHITE) {
tempBoard[row][col] = new Rook(Player.WHITE);
if (!((Rook) board[row][col]).isCastlingValid())
((Rook) tempBoard[row][col]).
setCastlingValid(false);
} else if (newBoard[row][col].type().equals("Rook")) {
tempBoard[row][col] = new Rook(Player.BLACK);
if (!((Rook) board[row][col]).isCastlingValid())
((Rook) tempBoard[row][col]).
setCastlingValid(false);
}
if (newBoard[row][col].type().equals("Bishop") &&
newBoard[row][col].player() == Player.WHITE)
tempBoard[row][col] = new Bishop(Player.WHITE);
else if (newBoard[row][col].type().equals("Bishop"))
tempBoard[row][col] = new Bishop(Player.BLACK);
if (newBoard[row][col].type().equals("Knight") &&
newBoard[row][col].player() == Player.WHITE)
tempBoard[row][col] = new Knight(Player.WHITE);
else if (newBoard[row][col].type().equals("Knight"))
tempBoard[row][col] = new Knight(Player.BLACK);
if (newBoard[row][col].type().equals("King") &&
newBoard[row][col].player() == Player.WHITE) {
tempBoard[row][col] = new King(Player.WHITE);
if (!((King) board[row][col]).isCastlingValid())
((King) tempBoard[row][col]).
setCastlingValid(false);
} else if (newBoard[row][col].type().equals("King")) {
tempBoard[row][col] = new King(Player.BLACK);
if (!((King) board[row][col]).isCastlingValid())
((King) tempBoard[row][col]).
setCastlingValid(false);
}
if (newBoard[row][col].type().equals("Queen") &&
newBoard[row][col].player() == Player.WHITE)
tempBoard[row][col] = new Queen(Player.WHITE);
else if (newBoard[row][col].type().equals("Queen"))
tempBoard[row][col] = new Queen(Player.BLACK);
}
}
}
/*************************************************************************
* @see IChessModel#currentPlayer()
************************************************************************/
public Player currentPlayer() {
return player;
}
/*************************************************************************
* Getter for the number of rows on the board.
*
* @return int returns the number of rows on the board
************************************************************************/
public int numRows() {
return BOARD_SIZE;
}
/*************************************************************************
* Getter for the number of columns on the board.
*
* @return int returns the number of columns on the board
************************************************************************/
public int numColumns() {
return BOARD_SIZE;
}
/*************************************************************************
* Returns the piece at any given location on the board.
* @param row number of rows on the board
* @param col number of columns on the board
* @return IChessPiece returns the piece at the given row and column
************************************************************************/
public IChessPiece pieceAt(int row, int col) {
return board[row][col];
}
/*************************************************************************
* Determines the best move for the AI to do without putting itself in
* check and if it is in check then it gets itself out of check. First
* four moves are random unless the opponent puts the king in check before
* the 4 moves are done.
*
* @return move returns the best move according to a point value that is
* given to each move
************************************************************************/
public Move chessAIMove() {
if (Player.BLACK == player) {
duplicateBoard(board);
//sets initial move value to 0
int moveValue = 0;
//if king is in check then find a move that takes it out of check
if (inCheck(Player.BLACK))
return getOutOfCheck();
//if it isn't in check then continue doing the random moves until
//the AI is 4 moves in
else {
if (numMoves < 4) {
for (int i = numMoves; i < randomMoves.size(); i++)
if (isValidMoveAI(randomMoves.get(i))) {
bestMove = randomMoves.get(i);
lastMove = bestMove;
numMoves++;
return bestMove;
}
}
//if the 4 random moves are done then it finds a black piece
//on the board and sends that piece to chessAI to find the
//best move for that piece and the compares that to the
//previous best move and if its greater than or equal to the
//previous best it assigns the new best move to the given move
else {
for (int row = 0; row < BOARD_SIZE; row++)
for (int col = 0; col < BOARD_SIZE; col++) {
if (tempBoard[row][col] != null)
if (tempBoard[row][col].player() ==
Player.BLACK) {
int temp = chessAI(tempBoard[row][col]);
if (temp >= moveValue) {
moveValue = temp;
//stores the last move and makes sure
//it doesn't keep moving back and
//forth
if ( lastMove != possibleMove)
bestMove = possibleMove;
}
}
}
lastMove = bestMove;
return bestMove;
}
}
}
return null;
}
/*************************************************************************
* This cycles through all of the board pieces and when it finds a black
* piece then it moves it to all of the possible spots on the board and
* gives that move a point value and if it is greater than the previous
* move's point value then it sets the new one the be the best up to this
* point.
*
* @param AIBoard a specific piece on the board to try moves on
* @return int returns the highest moves point value and stores the move
* into a temporary 'best' variable
************************************************************************/
public int chessAI(IChessPiece AIBoard) {
int moveValue = 0;
int tempValue = 0;
//finds the given black piece on the board
for (int row = 0; row < BOARD_SIZE; row++) {
for (int col = 0; col < BOARD_SIZE; col++) {
if (tempBoard[row][col] == AIBoard)
//cycles through each move that piece can move
for (int r = 0; r < BOARD_SIZE; r++) {
for (int c = 0; c < BOARD_SIZE; c++) {
//makes sure the move isn't back to the spot it
//was last turn
// if (r == lastMove.fromRow && c ==
// lastMove.fromColumn)
// break;
Move tempMove = new Move(row, col, r, c);
if (isValidMoveAI(tempMove)) {
IChessPiece temp = tempBoard[row][col];
IChessPiece temp1 = tempBoard[r][c];
//moves the piece if it is a valid move
tempBoard[r][c] = tempBoard[row][col];
tempBoard[row][col] = null;
//makes sure that move doesn't put it in
//check
if (!tempBoardInCheck(Player.BLACK)) {
tempBoard[r][c] = temp1;
tempBoard[row][col] = temp;
//if a pawn takes out a pawn the move
//value is set to 7
if (tempBoard[r][c] != null &&
tempBoard[r][c].type().
equals("Pawn") &&
tempBoard[row][col].type().
equals("Pawn"))
moveValue = 7;
//if a piece take out a piece of the same
//type then the piece worths are
//multiplied together
else if (tempBoard[r][c] != null
&& tempBoard[r][c].type().
equals(tempBoard[row][col].
type()))
moveValue = AIBoard.getPieceWorth()
* 2;
//adds piece worths together if it is 2
//random pieces taking e each other out
else if (tempBoard[r][c] != null)
moveValue = AIBoard.getPieceWorth() +
tempBoard[r][c].getPieceWorth();
else
//sets move value to 1 if moving to
//empty cell
moveValue = 1;
//compares move values
if (moveValue > tempValue)
{
tempValue = moveValue;
possibleMove =
tempMove;
}
//checks white moves
for (int rowW = 0; rowW < BOARD_SIZE;
rowW++) {
for (int colW = 0; colW < BOARD_SIZE;
colW++) {
if (tempBoard[rowW][colW] != null
&& tempBoard[rowW][colW].
player() == Player.WHITE){
Move tempW = new Move(rowW,
colW, r, c);
if (isValidMoveAI(tempW)) {
//if AI queen is taken out the divide by 2
if (tempBoard[row][col].
type().equals
("Queen"))
moveValue = moveValue
/2;
//if white takes out the same piece as black
//moved then subtract the AI's piece worth
else if (tempBoard[row]
[col].type().
equals(tempBoard
[rowW][colW].
type()))
moveValue = moveValue
- AIBoard.
getPieceWorth();
else if (tempBoard[row]
[col] != null)
//subtracts the AI's piece worth and the
//white's piece worth if none of the above
//happens
moveValue = moveValue
- (AIBoard.
getPieceWorth() +
tempBoard[rowW]
[colW].
getPieceWorth());
//compares move values
if (moveValue > tempValue)
{
tempValue = moveValue;
possibleMove =
tempMove;
}
}
}
}
}
}
//resets the board
tempBoard[r][c] = temp1;
tempBoard[row][col] = temp;
}
}
}
}
}
return tempValue;
}
/*************************************************************************
* Determines if castling is valid.
*
* @param move is the move that is a possible castle
* @return boolean returns true if castling is valid and false if it isn't
************************************************************************/
public boolean castling(Move move) {
duplicateBoard(board);
//king cannot be in check when castling
if (!tempBoardInCheck(player)) {
if (player == Player.WHITE) {
//makes sure they clicked on a king
if (tempBoard[move.fromRow][move.fromColumn].type().
equals("King"))
//makes sure king hasn't moved yet
if (((King) tempBoard[move.fromRow][move.fromColumn]).
isCastlingValid())
//checks if the spot they clicked on was 2 to the
//left and that the rook is still in the corner
if (move.toColumn == 2 && tempBoard[7][0]
instanceof Rook) {
//checks if rook hasn't moved yet
if (((Rook) tempBoard[7][0]).isCastlingValid())
//checks the spots between the rook and king
//to make sure they are empty
if (tempBoard[7][1] == null && tempBoard[7][2]
== null && tempBoard[7][3] == null) {
Move temp = new Move(7, 4, 7, 3);
if (isValidMoveAI(temp)) {
tempBoard[7][3] = tempBoard[7][4];
tempBoard[7][4] = null;
//makes sure the king isn't in check
//in the middle spot
if (!tempBoardInCheck(Player.WHITE)) {
Move temp1 = new Move(7, 3, 7, 2);
if (isValidMoveAI(temp1)) {
tempBoard[7][2] =
tempBoard[7][3];
tempBoard[7][3] = null;
//checks if the king is in
//check in the ending
//position
if (!tempBoardInCheck(Player.
WHITE)) {
board[7][3] =
tempBoard[7][0];
board[7][2] =
tempBoard[7][2];
board[7][0] = null;
board[7][4] = null;
player = player.next();
return true;
}
}
}
tempBoard[7][4] = tempBoard[7][2];
tempBoard[7][2] = null;
tempBoard[7][0] = tempBoard[7][3];
tempBoard[7][3] = null;
}
}
//same as previous except on left side of white
//player
} else if (move.toColumn == 6 && tempBoard[7][7]
instanceof Rook) {
if (((Rook) tempBoard[7][7]).isCastlingValid())
if (tempBoard[7][5] == null && tempBoard[7][6]
== null) {
Move temp = new Move(7, 4, 7, 5);
if (isValidMoveAI(temp)) {
tempBoard[7][5] = tempBoard[7][4];
tempBoard[7][4] = null;
if (!tempBoardInCheck(Player.WHITE)) {
Move temp1 = new Move(7, 5, 7, 6);
if (isValidMoveAI(temp1)) {
tempBoard[7][6] =
tempBoard[7][5];
tempBoard[7][5] = null;
if (!tempBoardInCheck(Player.
WHITE)) {
board[7][5] =
tempBoard[7][7];
board[7][6] =
tempBoard[7][6];
board[7][7] = null;
board[7][4] = null;
player = player.next();
return true;
}
}
}
tempBoard[7][4] = tempBoard[7][6];
tempBoard[7][6] = null;
tempBoard[7][7] = board[7][5];
tempBoard[7][5] = null;
}
}
}
//same as previous except for black player
} else if (tempBoard[move.fromRow][move.fromColumn].type().
equals("King"))
if (((King) tempBoard[move.fromRow][move.fromColumn]).
isCastlingValid())
if (move.toColumn == 2 && tempBoard[0][0] instanceof Rook)
{
if (((Rook) tempBoard[0][0]).isCastlingValid())
if (tempBoard[0][1] == null && tempBoard[0][2] ==
null && tempBoard[0][3] == null) {
Move temp = new Move(0, 4, 0, 3);
isValidMoveAI(temp);
tempBoard[0][3] = tempBoard[0][4];
tempBoard[0][4] = null;
if (!tempBoardInCheck(Player.BLACK)) {
Move temp1 = new Move(0, 3, 0, 2);
isValidMoveAI(temp1);
tempBoard[0][2] = tempBoard[0][3];
tempBoard[0][3] = null;
if (!tempBoardInCheck(Player.BLACK)) {
board[0][3] = tempBoard[0][0];
board[0][2] = tempBoard[0][2];
board[0][0] = null;
board[0][4] = null;
player = player.next();
return true;
}
}
}
tempBoard[0][4] = tempBoard[0][2];
tempBoard[0][2] = null;
tempBoard[0][0] = board[0][3];
tempBoard[0][3] = null;
} else if (move.toColumn == 6 && tempBoard[0][7]
instanceof Rook) {
if (((Rook) tempBoard[0][7]).isCastlingValid())
if (tempBoard[0][5] == null && tempBoard[0][6] ==
null) {
Move temp = new Move(0, 4, 0, 5);
isValidMoveAI(temp);
tempBoard[0][5] = tempBoard[0][4];
tempBoard[0][4] = null;
if (!tempBoardInCheck(Player.BLACK)) {
Move temp1 = new Move(0, 5, 0, 6);
isValidMoveAI(temp1);
tempBoard[0][6] = tempBoard[0][5];
tempBoard[0][5] = null;
if (!tempBoardInCheck(Player.BLACK)) {
board[0][5] = tempBoard[0][7];
board[0][6] = tempBoard[0][6];
board[0][7] = null;
board[0][4] = null;
player = player.next();
return true;
}
}
}
tempBoard[0][4] = tempBoard[0][6];
tempBoard[0][6] = null;
tempBoard[0][7] = board[0][5];
tempBoard[0][5] = null;
}
}
return false;
}
/*************************************************************************
* This method only checks if the move is valid for the AI so it doesn't
* throw exceptions for every false move because it will run into many
* of them.
*
* @param move takes in the move that the AI wants to do
* @return boolean returns true if the move is valid and false if it isn't