-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBoard.java
More file actions
1204 lines (1078 loc) · 37.4 KB
/
Board.java
File metadata and controls
1204 lines (1078 loc) · 37.4 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.awt.*;
import java.io.*;
import java.io.File;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Iterator;
import java.util.HashMap;
import java.util.Arrays;
/**
* <h1>Board</h1> This class handles the logic associated with a game of
* chess<br>
* <p>
*
* @author Mehul Pillai, John Tedesco, Steven Lum
* @version 1.0
* @since 2020-1-17
*/
public class Board {
// Properties
/**
* Boolean describing which colour the player is. True is white and false is
* black
*/
public boolean blnServer;
// used to check if game is in promotion state, user can choose a piece tp
// replace pawn
/**
* Boolean used to check if game is in promotion state, user can choose a piece
* to replace a pawn
*/
private boolean blnPromotion;
/**
* Boolean used to check if game intance has commenced a castling move
*/
private boolean hasCastled = false;
/**
* Boolean used to check if a player can commence a castling move
*/
private boolean canCastle = true;
/**
* Boolean used to check if a player is in tutorial mode
*/
private boolean blnTutorial = false;
/**
* Integer array representing a snapshot of the actual chessboard. This board
* will be used for checking checkmates without overwritting the actual
* chessboard
*/
private int[][] intTempBoard;
/**
* Boolean used to check if a player is in check state
*/
public boolean blnInCheck = false;
/**
* Integer array representing the actual chess board. Negative pieces are black
* while positive pieces are white
*/
private int[][] chessBoard = { { -1, -2, -3, -4, -5, -3, -2, -1 }, { -6, -6, -6, -6, -6, -6, -6, -6 },
{ 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0 }, { 6, 6, 6, 6, 6, 6, 6, 6 }, { 1, 2, 3, 4, 5, 3, 2, 1 } };
/**
* ArrayList of piece objects used in the game
*/
public ArrayList<Piece> pieces = new ArrayList<>();
/**
* HashMap holding key piece references to make accessing easier
*/
public HashMap<Integer, Piece> pieceLookup = new HashMap<>();
/**
* ArrayList of piece objects that have been captured
*/
public ArrayList<Piece> captured = new ArrayList<>();
/**
* ArrayList of piece objects representing white promotion choices
*/
private ArrayList<Piece> whitePromotion = new ArrayList<>();
/**
* ArrayList of piece objects representing black promotion choices
*/
private ArrayList<Piece> blackPromotion = new ArrayList<>();
/**
* Piece that is used to hold a reference to the pawn that will be promoted
*/
private Piece pieceToPromote = null;
// METHODS
/**
* Getter method for the pawn to be promoted
*
* @return a reference to the pawn to be promoted
*/
public Piece getPieceToPromote() {
return pieceToPromote;
}
/**
* Setter method to make the board a tutorial instance
*/
public void isTutorialMode() {
blnTutorial = true;
}
/**
* Getter method for the list of promotion choice
*
* @param blnServer boolean variable representing what instance of the game is
* running
* @return the list of promotion choice
*/
public ArrayList<Piece> getPromotionChoices(boolean blnServer) {
return blnServer ? whitePromotion : blackPromotion;
}
/**
* Getter method for the list of promotion choice
*
* @param blnColor boolean variable representing what instance of the game is
* running
* @param intPiece integer representing the piece type
* @return the number of specified captured pieces
*/
public int capturedPieceCount(boolean blnColor, int intPiece) {
int intPieceCount = 0;
for (Piece p : captured) {
if (p.blnColor != blnColor && p.intPiece == intPiece) {
intPieceCount++;
}
}
return intPieceCount;
}
/**
* Rounds down number to the specified place
*
* @param intNum the number to be rounded
* @param intRoundNum the place to round number to
* @return the list of promotion choice
*/
public int roundDown(int intNum, int intRoundNum) {
return intNum >= 0 ? (intNum / intRoundNum) * intRoundNum
: ((intNum - intRoundNum + 1) / intRoundNum) * intRoundNum;
}
/**
* Checks to see if the specified index is within chess board bounds
*
* @param intXIndex the column index
* @param intYIndex the row index
* @return true if in bounds and false if not
*/
private boolean inBounds(int intXIndex, int intYIndex) {
return intXIndex >= 0 && intXIndex < 8 && intYIndex >= 0 && intYIndex < 8;
}
/**
* Utility function that prints out the chess board
*/
public void printCharboard() {
for (int i = 0; i < chessBoard.length; i++) {
for (int j = 0; j < chessBoard[0].length; j++) {
System.out.print(chessBoard[i][j]);
}
System.out.println("");
}
}
/**
* Utility function that prints game status
*/
public void displayInformation() {
System.out.println("Incheck: " + blnInCheck);
System.out.println("Castled: " + hasCastled);
System.out.println("The white king is at: " + pieceLookup.get(5).intXPos + ", " + pieceLookup.get(5).intYPos);
}
/**
* Initalize piece arraylist
*/
private void initBoard() {
// the array is read flipped for the client so that their pieces appear at the
// bottom
for (int i = 0, i2 = 7; i < 8; i++, i2--) {
for (int j = 0, j2 = 7; j < 8; j++, j2--) {
Piece p;
int piece = chessBoard[i][j];
if (piece != 0) {
p = new Piece(blnServer ? (j * 90) : (j2 * 90), blnServer ? (i * 90) : (i2 * 90), piece > 0, piece);
} else {
continue;
}
pieces.add(p);
}
}
}
/**
* Initalize piece lookup HashMap
*/
public void initMap() {
Iterator<Piece> pieceIter = pieces.iterator();
// 0 - 5 will be white
// 6 - 10 will be black
while (pieceIter.hasNext()) {
Piece piece = pieceIter.next();
if (piece.intPiece != 6) {
if (piece.blnColor) {
pieceLookup.put(piece.intPiece, piece);
} else if (!piece.blnColor) {
pieceLookup.put(piece.intPiece + 5, piece);
}
}
}
}
/**
* Converts a pieces array location into a coordinate
*
* @param x1 Original x position
* @param y1 Original y position
* @param x2 Future x position
* @param y2 Future y position
* @return String of the coordinate move
*
*/
public String toCoord(int x1, int y1, int x2, int y2) {
String pos = "";
pos += (char) ((char) (x1) + 97);
int loc = 8 - y1;
pos += Integer.toString(loc);
pos += ',';
pos += (char) ((char) (x2) + 97);
loc = 8 - y2;
pos += loc;
System.out.println(pos);
return pos;
}
/**
* Updates the chessboard array
*
* @param strMove coordinates holding initial and final positons
* @param blnTemp boolean dictating whether to use the tempBoard or not
*/
public void move(String strMove, boolean blnTemp) {
System.out.println("****************************");
System.out.println(intTempBoard);
// Get's the current board being used
int[][] intCurrentBoard = blnTemp ? intTempBoard : chessBoard;
// Split the move into component parts
String[] strMoves = strMove.split(",");
boolean impedingKnightWhite = (blnServer && intCurrentBoard[7][6] != Piece.EMPTY) ? true : false;
boolean impedingKnightBlack = (blnServer && intCurrentBoard[0][1] != Piece.EMPTY) ? true : false;
System.out.println("Impeding white knight: " + impedingKnightWhite);
System.out.println("Impeding black knight: " + impedingKnightBlack);
Point p1 = coordToLoc(strMoves[0]);
System.out.println(p1.x + " " + p1.y);
Point p2 = coordToLoc(strMoves[1]);
System.out.println(p2.x + " " + p2.y);
// This is simply to prevent someone from castling twice.
if (!hasCastled && !blnInCheck) {
if (strMoves[0].equals("e1") && strMoves[1].equals("g1")) { // White castles short
System.out.println("Gets here: found move");
if (blnServer) {
castlesShort(p2.x * 90, p2.y * 90);
hasCastled = true;
} else {
// System.out.println("Get's here: looking for piece");
// System.out.println("Looking for: x" + (7 - p2.x) * 90);
// System.out.println("Looking for: y" + (7 - p2.y) * 90);
castlesShort((7 - p2.x) * 90, (7 - p2.y) * 90);
}
} else if (strMoves[0].equals("e1") && strMoves[1].equals("c1") && impedingKnightWhite == false) { // White
// casltes
// long
System.out.println("Gets here");
if (blnServer) {
castlesLong(p2.x * 90, p2.y * 90);
hasCastled = true;
} else {
castlesLong((7 - p2.x) * 90, (7 - p2.y) * 90);
}
} else if (strMoves[0].equals("e8") && strMoves[1].equals("g8")) { // Black castles short
System.out.println("Get's here");
if (!blnServer) {
castlesShortBlack((7 - p2.x) * 90, (7 - p2.y) * 90);
hasCastled = true;
} else {
castlesShortBlack(p2.x * 90, p2.y * 90);
}
} else if (strMoves[0].equals("e8") && strMoves[1].equals("c8") && impedingKnightBlack == false) { // Black
// casltes
// long
System.out.println("Gets here");
if (!blnServer) {
castlesLongBlack((7 - p2.x) * 90, (7 - p2.y) * 90);
hasCastled = true;
} else {
castlesLongBlack(p2.x * 90, p2.y * 90);
}
}
}
// Create a temp for the pieces original spot
int intTemp = intCurrentBoard[p1.y][p1.x];
System.out.println("intTemp = " + intTemp);
// Sets the old piece position to 0
intCurrentBoard[(int) (p1.y)][(int) (p1.x)] = Piece.EMPTY;
System.out.println(intCurrentBoard[(int) (p1.y)][(int) (p1.x)] = 0);
// Sets new king position to the old piece
intCurrentBoard[(int) (p2.y)][(int) (p2.x)] = intTemp;
System.out.println("Position 2: " + intCurrentBoard[(int) (p2.y)][(int) (p2.x)]);
System.out.println("****************************");
}
/**
* Short castles for white based on physical position
*
* @param intXPos pieces physical x-position on the screen
* @param intYPos pieces physcial y-position on the screen
*
*/
public void castlesShort(int intXPos, int intYPos) {
Iterator<Piece> pieceIterator = pieces.iterator();
System.out.println("Searching intXPos for black is: " + (intXPos - 90));
System.out.println("intYPos is: " + intYPos);
while (pieceIterator.hasNext()) {
Piece piece = pieceIterator.next();
if (blnServer && piece.intXPos == intXPos + 90 && piece.intYPos == intYPos) {
System.out.println("Gets here: checking the piece position");
System.out.println(piece.intPiece);
piece.setPosition(intXPos - 90, intYPos);
int intTempX = intXPos / 90;
int intTempY = intYPos / 90;
piece.setPosition(intXPos - 90, intYPos);
chessBoard[intTempY][intTempX + 1] = Piece.EMPTY;
chessBoard[intTempY][intTempX - 1] = Piece.ROOK;
} else if (!blnServer && piece.intXPos == intXPos - 90 && piece.intYPos == intYPos) {
System.out.println("Got here if server == false");
System.out.println("The piece is: " + piece.intPiece);
piece.setPosition(intXPos + 90, intYPos);
int intTempX = intXPos / 90;
int intTempY = intYPos / 90;
System.out.println("intTempX" + intTempX);
System.out.println("intTempY" + intTempY);
chessBoard[7 - intTempY][7 - intTempX + 1] = Piece.EMPTY;
chessBoard[7 - intTempY][7 - intTempX - 1] = Piece.ROOK;
}
}
}
/**
* Short castles for the black pieces based on the physical position
*
* @param intXPos physcial x-position of the piece
* @param intYPos physical y-position of the piece
*
*/
public void castlesShortBlack(int intXPos, int intYPos) {
Iterator<Piece> pieceIterator = pieces.iterator();
System.out.println("Searching intXPos for black is: " + (intXPos - 90));
System.out.println("intYPos is: " + intYPos);
while (pieceIterator.hasNext()) {
Piece piece = pieceIterator.next();
if (!blnServer && piece.intXPos == intXPos - 90 && piece.intYPos == intYPos) {
System.out.println(intXPos);
System.out.println(intYPos);
// Int yPos should be 0, int x Pos should be 90
piece.setPosition(intXPos + 90, intYPos);
int intTempX = intXPos / 90;
int intTempY = intYPos / 90;
System.out.println("tempX: " + intTempX);
System.out.println("tempY: " + intTempY);
chessBoard[7 - intTempY][7 - intTempX + 1] = Piece.EMPTY;
chessBoard[7 - intTempY][7 - intTempX - 1] = -Piece.ROOK;
} else if (blnServer && piece.intXPos == intXPos + 90 && piece.intYPos == intYPos) {
System.out.println(intXPos);
System.out.println(intYPos);
// Here int yPos should be 0 and intXPos should be 900(I think)
piece.setPosition(intXPos - 90, intYPos);
int intTempX = intXPos / 90;
int intTempY = intYPos / 90;
System.out.println("intTempX: " + intTempX);
System.out.println("intTempY: " + intTempY);
chessBoard[intTempY][intTempX + 1] = Piece.EMPTY;
chessBoard[intTempY][intTempX - 1] = -Piece.ROOK;
}
}
}
/**
* Castles long for white based on pieces physical position
*
* @param intXPos physical x-position of the piece
* @param intYPos physcial y-position of the piece
*/
public void castlesLong(int intXPos, int intYPos) {
Iterator<Piece> pieceIterator = pieces.iterator();
while (pieceIterator.hasNext()) {
Piece piece = pieceIterator.next();
if (blnServer && piece.intXPos == intXPos - 180 && piece.intYPos == intYPos) {
System.out.println("Get's here");
piece.setPosition(intXPos + 90, intYPos);
int intTempX = intXPos / 90;
int intTempY = intYPos / 90;
System.out.println("tempX: " + intTempX);
System.out.println("tempY: " + intTempY);
chessBoard[intTempY][intTempX - 2] = Piece.EMPTY;
chessBoard[intTempY][intTempX + 1] = Piece.ROOK;
} else if (!blnServer && piece.intXPos == intXPos + 180 && piece.intYPos == intYPos) {
System.out.println("Get's here if found piece and server == false");
piece.setPosition(intXPos - 90, intYPos);
int intTempX = intXPos / 90;
int intTempY = intYPos / 90;
System.out.println("tempX: " + intTempX);
System.out.println("tempY: " + intTempY);
chessBoard[7 - intTempY][7 - intTempX - 2] = Piece.EMPTY;
chessBoard[7 - intTempY][7 - intTempX + 1] = Piece.ROOK;
}
}
}
/**
* Castles long for white based on pieces physical position
*
* @param intXPos physical x-position of the piece
* @param intYPos physcial y-position of the piece
*/
public void castlesLongBlack(int intXPos, int intYPos) {
Iterator<Piece> pieceIterator = pieces.iterator();
while (pieceIterator.hasNext()) {
Piece piece = pieceIterator.next();
if (blnServer && piece.intXPos == intXPos - 180 && piece.intYPos == intYPos) {
System.out.println("Get's here");
piece.setPosition(intXPos + 90, intYPos);
int intTempX = intXPos / 90;
int intTempY = intYPos / 90;
System.out.println("tempX" + intTempX);
System.out.println("tempY" + intTempY);
chessBoard[intTempY][intTempX - 2] = Piece.EMPTY;
chessBoard[intTempY][intTempX + 1] = -Piece.ROOK;
} else if (!blnServer && piece.intXPos == intXPos + 180 && piece.intYPos == intYPos) {
System.out.println("Get's here if found piece and server == false");
piece.setPosition(intXPos - 90, intYPos);
int intTempX = intXPos / 90;
int intTempY = intYPos / 90;
System.out.println("tempX" + intTempX);
System.out.println("tempY" + intTempY);
chessBoard[7 - intTempY][7 - intTempX - 2] = Piece.EMPTY;
chessBoard[7 - intTempY][7 - intTempX + 1] = -Piece.ROOK;
}
}
}
/**
* Converts a coordinate into a physical location in the array
*
* @param coord String coordinate of the board
* @return A point with the x, and y location of the board
*
*/
public Point coordToLoc(String coord) {
// System.out.println(newCoord);
int x = (int) (coord.charAt(0) - 97);
int y = (int) (7 - coord.charAt(1) + 49);
// System.out.println(x + ", " + y);
Point retCoord = new Point(x, y);
return retCoord;
}
/**
* Checks specified index for a white piece
*
* @param intXIndex column index
* @param intYIndex row index
* @return true if there was a white piece
*/
public boolean isWhite(int intXIndex, int intYIndex) {
return chessBoard[intYIndex][intXIndex] > 0;
}
/**
* Getter method for the piece type at a specified index
*
* @param intXIndex column index
* @param intYIndex row index
* @return return the integer piece at the index
*/
public int getPiece(int intXIndex, int intYIndex) {
return chessBoard[intYIndex][intXIndex];
}
/**
* Setter method for setting a piece type at a specified index
*
* @param intXIndex column index
* @param intYIndex row index
* @param intPiece the piece type
*/
public void setPiece(int intXIndex, int intYIndex, int intPiece) {
chessBoard[intYIndex][intXIndex] = intPiece;
}
/**
* Setter method for changing the value of blnInCheck
*
* @param blnInCheck value to be set
*/
public void setCheck(boolean blnInCheck) {
this.blnInCheck = blnInCheck;
}
/**
* Checks to see if a move gives check to the opponent
*
* @return return true if there was a check
*/
public boolean givesCheck() {
if (blnServer) {
// If white get the black king
Piece king = pieceLookup.get(10);
int intXPos = (7 * 90) - king.intXPos;
int intYPos = (7 * 90) - king.intYPos;
System.out.println("intXPos: " + intXPos);
System.out.println("intYPos: " + intYPos);
// Check legal knight moves on king position to check for a knight
for (int[] p : ChessUtility.getLegalKnightMoves(intXPos, intYPos)) {
if (chessBoard[7 - p[1]][7 - p[0]] == Piece.KNIGHT) {
System.out.println("Knight Check weee");
return true;
}
}
// Check all diagonal moves to check for queen or bishop
for (int[] p : ChessUtility.getLegalBishopMoves(intXPos, intYPos, false)) {
System.out.println(p[0] + " " + p[1]);
if (chessBoard[7 - p[1]][7 - p[0]] == Piece.BISHOP || chessBoard[7 - p[1]][7 - p[0]] == Piece.QUEEN) {
System.out.println("diag check");
return true;
}
}
// Check all horizontal and vertical moves to check for rook or queen
for (int[] p : ChessUtility.getLegalRookMoves(intXPos, intYPos, false)) {
System.out.println(p[0] + " " + p[1]);
if (chessBoard[7 - p[1]][7 - p[0]] == Piece.QUEEN || chessBoard[7 - p[1]][7 - p[0]] == Piece.ROOK) {
System.out.println("file check");
return true;
}
}
intXPos = (7 * 90) - king.intXPos;
intYPos = (7 * 90) - king.intYPos;
// if white, then the pawn could be down left or down right
if ((inBounds((intXPos / 90) - 1, (intYPos / 90) + 1)
? chessBoard[(intYPos / 90) + 1][(intXPos / 90) - 1] == Piece.PAWN
: false)
|| (inBounds((intXPos / 90) + 1, (intYPos / 90) + 1)
? chessBoard[(intYPos / 90) + 1][(intXPos / 90) + 1] == Piece.PAWN
: false)) {
System.out.println("GET PWNED");
return true;
}
return false;
} else if (!blnServer) {
// If black get white king
Piece king = pieceLookup.get(5);
int intXPos = (7 * 90) - king.intXPos;
int intYPos = (7 * 90) - king.intYPos;
System.out.println("intXPos: " + intXPos);
System.out.println("intYPos: " + intYPos);
// System.out.println("white queen moves: " +
// ChessUtility.getLegalQueenMoves(intXPos, intYPos, true));
// System.out.println("black queen moves: " +
// ChessUtility.getLegalQueenMoves(intXPos, intYPos, false));
// Check legal knight moves on king for knights
for (int[] p : ChessUtility.getLegalKnightMoves(intXPos, intYPos)) {
System.out.println("Get's here: In knight check");
System.out.println(p[1] + " " + p[0]);
if (chessBoard[p[1]][p[0]] == -Piece.KNIGHT) {
System.out.println("Knight Check weee");
return true;
}
}
// Check diagonals for bishops and queens
for (int[] p : ChessUtility.getLegalBishopMoves(intXPos, intYPos, true)) {
System.out.println(p[0] + " " + p[1]);
if (chessBoard[p[1]][p[0]] == -Piece.BISHOP || chessBoard[p[1]][p[0]] == -Piece.QUEEN) {
System.out.println("diag check");
return true;
}
}
// Check horizontals and verticals for rooks and queens
for (int[] p : ChessUtility.getLegalRookMoves(intXPos, intYPos, true)) {
System.out.println(p[0] + " " + p[1]);
if (chessBoard[p[1]][p[0]] == -Piece.QUEEN || chessBoard[p[1]][p[0]] == -Piece.ROOK) {
System.out.println("Rook check");
return true;
}
}
if ((inBounds((intXPos / 90) - 1, (intYPos / 90) + 1)
? chessBoard[(intYPos / 90) + 1][(intXPos / 90) - 1] == -Piece.PAWN
: false)
|| (inBounds((intXPos / 90), (intYPos / 90) + 1)
? chessBoard[(intYPos / 90) + 1][(intXPos / 90)] == -Piece.PAWN
: false)) {
System.out.println("GET PWNED");
return true;
}
return false;
}
return false;
}
/**
* Get's the kings position while checking for checkmate
*
* @param blnColor color of the piece being moved
* @param intBoard the chess board
* @return Point object containing the kings x and y position
*/
private Point getKingPosition(boolean blnColor, int[][] intBoard) {
int intTarget = blnColor ? Piece.KING : -Piece.KING;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (intTarget == intBoard[i][j]) {
return new Point(j, i);
}
}
}
return null;
}
/**
* This will check if after making a move the king is still in check. If the
* function returns true than then king is still in check and that causes the
* move to be illegal
*
* @param blnTemp if you are using the temporary board or not.
* @return boolean true if the piece is in check false if it is not
*/
public boolean inCheck(boolean blnTemp) {
int[][] intCurrentBoard = blnTemp ? intTempBoard : chessBoard;
if (blnServer) {
// If white get the white king
Point kingPoint = getKingPosition(true, intCurrentBoard);
int intXPos = 0;
int intYPos = 0;
if (kingPoint == null) {
return true;
// Piece king = pieceLookup.get(5);
// intXPos = king.intXPos/90;
// intYPos = king.intYPos/90;
} else {
intXPos = kingPoint.x;
intYPos = kingPoint.y;
}
System.out.println("Blocking intXPos: " + intXPos);
System.out.println("Blocking intYPos: " + intYPos);
// check legal knight moves on king for knights
for (int[] p : ChessUtility.getLegalKnightMoves(intXPos * 90, intYPos * 90)) {
if (intCurrentBoard[p[1]][p[0]] == -Piece.KNIGHT) {
return true;
}
}
// check diagonals for bishops and queens
for (int[] p : ChessUtility.getLegalBishopMoves(intXPos * 90, intYPos * 90, true, intCurrentBoard)) {
if (intCurrentBoard[p[1]][p[0]] == -Piece.BISHOP || intCurrentBoard[p[1]][p[0]] == -Piece.QUEEN) {
System.out.println("Rook check");
return true;
}
}
// check horizontals and verticals for rooks and queens
for (int[] p : ChessUtility.getLegalRookMoves(intXPos * 90, intYPos * 90, true, intCurrentBoard)) {
if (intCurrentBoard[p[1]][p[0]] == -Piece.QUEEN || intCurrentBoard[p[1]][p[0]] == -Piece.ROOK) {
System.out.println("Rook check");
return true;
}
}
if ((inBounds(intXPos - 1, intYPos + 1) ? intCurrentBoard[intYPos + 1][intXPos - 1] == -Piece.PAWN : false)
|| (inBounds(intXPos + 1, intYPos + 1) ? intCurrentBoard[intYPos + 1][intXPos + 1] == -Piece.PAWN
: false)) {
System.out.println("GET PWNED");
return true;
}
return false;
} else if (!blnServer) {
// Piece king = pieceLookup.get(10);
// Get the black king
Point kingPoint = getKingPosition(false, intCurrentBoard);
int intXPos = 0;
int intYPos = 0;
if (kingPoint == null) {
return true;
// Piece king = pieceLookup.get(10);
// intXPos = 7-(king.intXPos/90);
// intYPos = 7-(king.intYPos/90);
} else {
intXPos = kingPoint.x;
intYPos = kingPoint.y;
}
// Check knight moves on king for knights
for (int[] p : ChessUtility.getLegalKnightMoves((7 - intXPos) * 90, (7 - intYPos) * 90)) {
if (intCurrentBoard[7 - p[1]][7 - p[0]] == Piece.KNIGHT) {
return true;
}
}
// Check for queens and bishops on diagonals
for (int[] p : ChessUtility.getLegalBishopMoves((7 - intXPos) * 90, (7 - intYPos) * 90, false,
intCurrentBoard)) {
if (intCurrentBoard[7 - p[1]][7 - p[0]] == Piece.BISHOP
|| intCurrentBoard[7 - p[1]][7 - p[0]] == Piece.QUEEN) {
System.out.println("Blocks check");
return true;
}
}
// Check for rooks and queens on horizontals and verticals
for (int[] p : ChessUtility.getLegalRookMoves((7 - intXPos) * 90, (7 - intYPos) * 90, false,
intCurrentBoard)) {
if (intCurrentBoard[7 - p[1]][7 - p[0]] == Piece.QUEEN
|| intCurrentBoard[7 - p[1]][7 - p[0]] == Piece.ROOK) {
System.out.println("Blocks check");
return true;
}
}
intXPos = 7 - intXPos;
intYPos = 7 - intYPos;
// Pawn checking
if ((inBounds(7 - intXPos - 1, 7 - intYPos + 1)
? intCurrentBoard[7 - intYPos + 1][7 - intXPos - 1] == Piece.PAWN
: false)
|| (inBounds(7 - intXPos + 1, 7 - intYPos + 1)
? intCurrentBoard[7 - intYPos + 1][7 - intXPos + 1] == Piece.PAWN
: false)) {
System.out.println("GET PWNED");
return true;
}
return false;
}
return false;
}
/**
* Creates a copoy of the contents of an array. Without sharing a refrence
*
* @param intBoard a two dimensinoal array
* @return A copy of the array that was passed in
*/
public int[][] deepCopy(int[][] intBoard) {
int[][] intCopy = new int[intBoard.length][];
for (int i = 0; i < intBoard.length; i++) {
intCopy[i] = Arrays.copyOf(intBoard[i], intBoard[i].length);
}
return intCopy;
}
// Or walks into check
/**
* Checks if a move puts the king in check
*
* @param strMove the move that is about to be made
* @return true if the move puts you in check. False if it does not
*
*/
public boolean stillInCheck(String strMove) {
intTempBoard = deepCopy(chessBoard);
// Set to true because of temp
move(strMove, true);
if (inCheck(true)) {
return true;
} else {
return false;
}
}
/**
* Checks if a move gives checkmate
*
* @return true if checkmate, false if king is able to escape
*/
public boolean checkmate() {
// intTempBoard = deepCopy(chessBoard);
System.out.println("In check: " + blnInCheck);
// Check all legal moves to see if anything will block check
if (!blnInCheck) {
return false;
} else if (blnInCheck && blnServer) {
for (Piece p : pieces) {
int intXIndex = p.intXPos / 90;
int intYIndex = p.intYPos / 90;
if (p.blnColor) {
LinkedList<int[]> legalList = new LinkedList<>();
if (p.intPiece == 5) {
int[] intTranlateX = { 1, -1, 0, 0, 1, -1, 1, -1 }; // Just for regular moves
int[] intTranlateY = { 0, 0, 1, -1, 1, -1, -1, 1 }; // Just for regular moves
// Regular moves
for (int i = 0; i < intTranlateX.length; i++) {
if (intYIndex + intTranlateY[i] >= 0 && intYIndex + intTranlateY[i] < 8
&& intXIndex + intTranlateX[i] >= 0 && intXIndex + intTranlateX[i] < 8) {
if (chessBoard[(intYIndex + intTranlateY[i])][(intXIndex + intTranlateX[i])] > 0) {
continue;
}
if (chessBoard[intYIndex + intTranlateY[i]][intXIndex + intTranlateX[i]] <= 0) {
legalList.add(
new int[] { intXIndex + intTranlateX[i], intYIndex + intTranlateY[i] });
}
}
}
} else {
legalList = getLegalMovesCheck(p);
}
for (int[] moves : legalList) {
System.out.println("Piece: " + p.intPiece);
System.out.println("intXIndexLast: " + intXIndex);
System.out.println("intYIndexLast: " + intYIndex);
if (!stillInCheck(toCoord(intXIndex, intYIndex, moves[0], moves[1]))) {
System.out.println("BRUH MOMENT");
return false;
}
}
}
}
} else if (blnInCheck && !blnServer) {
for (Piece p : pieces) {
int intXIndex = (p.intXPos / 90);
int intYIndex = (p.intYPos / 90);
if (!p.blnColor) {
LinkedList<int[]> legalList = new LinkedList<>();
if (p.intPiece == 5) {
int[] intTranlateX = { 1, -1, 0, 0, 1, -1, 1, -1 }; // Just for regular moves
int[] intTranlateY = { 0, 0, 1, -1, 1, -1, -1, 1 }; // Just for regular moves
// Regular moves
for (int i = 0; i < intTranlateX.length; i++) {
if (intYIndex + intTranlateY[i] >= 0 && intYIndex + intTranlateY[i] < 8
&& intXIndex + intTranlateX[i] >= 0 && intXIndex + intTranlateX[i] < 8) {
if (chessBoard[7 - (intYIndex + intTranlateY[i])][7
- (intXIndex + intTranlateX[i])] < 0) {
continue;
}
if (chessBoard[7 - (intYIndex + intTranlateY[i])][7
- (intXIndex + intTranlateX[i])] >= 0) {
legalList.add(
new int[] { intXIndex + intTranlateX[i], intYIndex + intTranlateY[i] });
}
}
}
} else {
legalList = getLegalMovesCheck(p);
}
for (int[] moves : legalList) {
System.out.println("Piece: " + p.intPiece);
System.out.println("intXIndexLast: " + intXIndex);
System.out.println("intYIndexLast: " + intYIndex);
if (!stillInCheck(toCoord(7 - intXIndex, 7 - intYIndex, 7 - moves[0], 7 - moves[1]))) {
System.out.println("BRUH MOMENT");
return false;
}
}
}
}
}
return true;
}
/**
* Get's all legal moves of any given piece
*
* @param p the piece that is being moved
* @return a linked list of piece positions
*/
private LinkedList<int[]> getLegalMovesCheck(Piece p) {
intTempBoard = deepCopy(chessBoard);
switch (p.intPiece) {
case Piece.ROOK:
return ChessUtility.getLegalRookMoves(p.intXPos, p.intYPos, p.blnColor, intTempBoard);
case Piece.KNIGHT:
return ChessUtility.getLegalKnightMoves(p.intXPos, p.intYPos);
case Piece.BISHOP:
return ChessUtility.getLegalBishopMoves(p.intXPos, p.intYPos, p.blnColor, intTempBoard);
case Piece.QUEEN:
LinkedList<int[]> legalQueenMoves = ChessUtility.getLegalRookMoves(p.intXPos, p.intYPos, p.blnColor,
intTempBoard);
legalQueenMoves.addAll(ChessUtility.getLegalBishopMoves(p.intXPos, p.intYPos, p.blnColor, intTempBoard));
return legalQueenMoves;
case Piece.PAWN:
LinkedList<int[]> legalPawnMoves = ChessUtility.getLegalPawnMoves(p.blnFirst, true, p.blnColor, p.intXPos,
p.intYPos);
legalPawnMoves.addAll(ChessUtility.getLegalPawnMoves(p.blnFirst, false, p.blnColor, p.intXPos, p.intYPos));
return legalPawnMoves;
}
return null;
}
// TODO: Clean this function up, the if statements have very similar code. Could
// probably be simplified
// return true if move was succesful
/**
* Executes a move based on the final x and y position of a mouse
*
* @param piece The piece being moved
* @param intXPos Physical x-position of the piece on the screen
* @param intYPos Physcial y-position of the piece on the screen
* @return true if the move is possible, false if it is not
*/
public boolean executeMove(Piece piece, int intXPos, int intYPos) {
SuperSocketMaster ssm = ChessGame.getNetwork();
piece.setPosition(intXPos, intYPos);
int intXIndexLast = blnServer ? piece.intLastX / 90 : 7 - (piece.intLastX / 90);
int intYIndexLast = blnServer ? piece.intLastY / 90 : 7 - (piece.intLastY / 90);
int intXIndex = blnServer ? intXPos / 90 : 7 - (intXPos / 90);
int intYIndex = blnServer ? intYPos / 90 : 7 - (intYPos / 90);
boolean blnLegalMove = false;
boolean blnStillInCheck = stillInCheck(toCoord(intXIndexLast, intYIndexLast, intXIndex, intYIndex));
blnInCheck = blnStillInCheck;
if (!blnTutorial) {
blnLegalMove = piece.isLegalMove(chessBoard[intYIndex][intXIndex] != 0) && !blnStillInCheck;
} else {
blnLegalMove = piece.isLegalMove(chessBoard[intYIndex][intXIndex] != 0);
}
// if player is white and the spot has a white piece
boolean blnSamePieceWhite = blnServer && isWhite(intXIndex, intYIndex) && chessBoard[intYIndex][intXIndex] != 0;
// if the player is black and the spot has a black piece
boolean blnSamePieceBlack = !blnServer && !isWhite(intXIndex, intYIndex)
&& chessBoard[intYIndex][intXIndex] != 0;