-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArimaa.java
More file actions
1470 lines (1340 loc) · 71.2 KB
/
Copy pathArimaa.java
File metadata and controls
1470 lines (1340 loc) · 71.2 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.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.*;
/*
* Implementation of Arimaa for two players via the GUI
* Author: Sitara and Aashvi
* Date: 6/1/2021
*/
public class Arimaa implements ActionListener {
//Global variables & objects
JFrame frame = new JFrame();
//Containers
Container north = new Container();
Container south = new Container();
Container east = new Container();
Container west = new Container();
Container center = new Container();
JButton[][] gridButtons = new JButton[8][8];
int[][] gridBoard = new int[8][8];
int[][] pieceStates = new int[8][8];
JButton currentButton = new JButton();
int currentRow;
int currentColumn;
int currentPieceValue;
JButton strongButton = new JButton();
int strongRow;
int strongColumn;
JButton weakButton = new JButton();
int weakRow;
int weakColumn;
JButton previousButton = new JButton();
int previousRow;
int previousColumn;
JButton initialButton = new JButton();
int initalRow;
int initialColumn;
final int G_ELEPHANT = 6;
final int G_CAMEL = 5;
final int G_HORSE = 4;
final int G_DOG = 3;
final int G_CAT = 2;
final int G_RABBIT = 1;
final int BLANK = 0;
final int S_ELEPHANT = 12;
final int S_CAMEL = 11;
final int S_HORSE = 10;
final int S_DOG = 9;
final int S_CAT = 8;
final int S_RABBIT = 7;
int aShift, bShift; //the gold pieces (valued 1-6) and silver pieces (valued 7-12), so we need a shift when comparing the pieces
ArrayList<Integer> goldPieceList = new ArrayList<Integer>(Arrays.asList(G_ELEPHANT, G_CAMEL, G_HORSE, G_HORSE, G_DOG, G_DOG, G_CAT, G_CAT, G_RABBIT, G_RABBIT, G_RABBIT, G_RABBIT, G_RABBIT, G_RABBIT, G_RABBIT, G_RABBIT));
ArrayList<Integer> silverPieceList = new ArrayList<Integer>(Arrays.asList(S_ELEPHANT, S_CAMEL, S_HORSE, S_HORSE, S_DOG, S_DOG, S_CAT, S_CAT, S_RABBIT, S_RABBIT, S_RABBIT, S_RABBIT, S_RABBIT, S_RABBIT, S_RABBIT, S_RABBIT));
JButton push = new JButton("Push");
JButton pull = new JButton("Pull");
JButton step = new JButton("Step");
JPanel moveButtons = new JPanel();
JButton turnDone = new JButton("Turn Done");
JButton reset = new JButton("Reset");
JButton start = new JButton("Start");
//Labels
JLabel goldLabel = new JLabel("GOLD", SwingConstants.CENTER);
JLabel silverLabel = new JLabel("SILVER", SwingConstants.CENTER);
JLabel piecesLabel = new JLabel("<html><p><center>PIECE</center></p></html>", SwingConstants.CENTER);
JLabel winsLabel = new JLabel("WINS", SwingConstants.CENTER);
JLabel goldWinsLabel = new JLabel("Gold: 0");
JLabel silverWinsLabel = new JLabel("Silver: 0");
JLabel blankLabel = new JLabel(" ", SwingConstants.CENTER);
JLabel setupLabel = new JLabel("<html><p><center>SET-UP</center></p></html>", SwingConstants.CENTER);
JLabel movesLabel = new JLabel("MOVES", SwingConstants.CENTER);
JLabel instructionsLabel = new JLabel("<html><p><center>Instructions:</center><br> "
+ " Click the grid square <br> "
+ " that you want to place <br> "
+ " your piece on once you <br> "
+ " have selected an animal <br></p></html>");
int goldWins = 0;
int silverWins = 0;
//Radio buttons
JRadioButton elephantButton = new JRadioButton("Elephant");
boolean elephant = false;
int eCounter = 1;
JRadioButton camelButton = new JRadioButton("Camel");
boolean camel = false;
int cCounter = 1;
JRadioButton horseButton = new JRadioButton("Horses");
boolean horse = false;
int hCounter = 2;
JRadioButton dogButton = new JRadioButton("Dogs");
boolean dog = false;
int dCounter = 2;
JRadioButton catButton = new JRadioButton("Cats");
boolean cat = false;
int catCounter = 2;
JRadioButton rabbitButton = new JRadioButton("Rabbits");
boolean rabbit = false;
int rCounter = 8;
ImageIcon[] imageGold = new ImageIcon[6];
ImageIcon[] imageSilver = new ImageIcon[6];
int turn;
final int GOLD_TURN = 0, SILVER_TURN = 1;
int gameState;
final int SET_UP = 0, PLAY = 1, FINISH = 2;
final int STEP = 1, PUSH = 2, PULL_ONE = 3, PULL_TWO = 4, TEMP = 5;
int moveState = TEMP;
final int PART_ONE = 0, PART_TWO = 1;
int pushPart = PART_ONE;
int stepCounter = 4;
// Constructor for Arimaa
public Arimaa() {
frame.setSize(1000, 600);
frame.setLayout(new BorderLayout());
// Icons for gold
imageGold[0] = new ImageIcon("./images/eGold.png");
imageGold[1] = new ImageIcon("./images/cGold.png");
imageGold[2] = new ImageIcon("./images/hGold.png");
imageGold[3] = new ImageIcon("./images/dGold.png");
imageGold[4] = new ImageIcon("./images/catGold.png");
imageGold[5] = new ImageIcon("./images/rGold.png");
// Icons for silver
imageSilver[0] = new ImageIcon("./images/eSilver.png");
imageSilver[1] = new ImageIcon("./images/cSilver.png");
imageSilver[2] = new ImageIcon("./images/hSilver.png");
imageSilver[3] = new ImageIcon("./images/dSilver.png");
imageSilver[4] = new ImageIcon("./images/catSilver.png");
imageSilver[5] = new ImageIcon("./images/rSilver.png");
//North container: silver
north.setLayout(new GridLayout(1, 1));
north.add(silverLabel);
frame.add(north, BorderLayout.NORTH);
//South container: gold
south.setLayout(new GridLayout(1, 1));
south.add(goldLabel);
frame.add(south, BorderLayout.SOUTH);
//Center container: grid
center.setLayout(new GridLayout(8, 8));
//creating the buttons in the grid
for (int a = 0; a < gridButtons.length; a++) {
for (int b = 0; b < gridButtons[0].length; b++) {
gridButtons[a][b] = new JButton();
gridButtons[a][b].addActionListener(this);
gridButtons[a][b].setEnabled(false);
center.add(gridButtons[a][b]);
gridBoard[a][b] = BLANK;
}
}
setBackgrounds();
frame.add(center, BorderLayout.CENTER);
//East container: move buttons
east.setLayout(new GridLayout(11,1));
east.add(movesLabel);
east.add(blankLabel);
moveButtons.setLayout(new GridLayout(1,3));
moveButtons.add(push);
push.setEnabled(false);
push.addActionListener(this);
moveButtons.add(pull);
pull.setEnabled(false);
pull.addActionListener(this);
moveButtons.add(step);
step.setEnabled(false);
step.addActionListener(this);
east.add(moveButtons);
east.add(turnDone);
turnDone.setEnabled(false);
turnDone.addActionListener(this);
east.add(blankLabel);
east.add(winsLabel);
east.add(goldWinsLabel);
east.add(silverWinsLabel);
east.add(blankLabel);
east.add(reset);
reset.setEnabled(false);
reset.addActionListener(this);
east.add(start);
start.addActionListener(this);
frame.add(east, BorderLayout.EAST);
//West container: setup GUI
BoxLayout boxlayout = new BoxLayout(west, BoxLayout.Y_AXIS);
west.setLayout(boxlayout);
west.add(setupLabel);
west.add(blankLabel);
west.add(piecesLabel);
west.add(blankLabel);
// Radiobuttons
elephantButton.setMnemonic(KeyEvent.VK_B);
elephantButton.setActionCommand("Elephant");
west.add(elephantButton);
elephantButton.addActionListener(this);
elephantButton.setEnabled(false);
camelButton.setMnemonic(KeyEvent.VK_C);
camelButton.setActionCommand("Camel");
west.add(camelButton);
camelButton.addActionListener(this);
camelButton.setEnabled(false);
horseButton.setMnemonic(KeyEvent.VK_C);
horseButton.setActionCommand("Horse");
west.add(horseButton);
horseButton.addActionListener(this);
horseButton.setEnabled(false);
dogButton.setMnemonic(KeyEvent.VK_C);
dogButton.setActionCommand("Dog");
west.add(dogButton);
dogButton.addActionListener(this);
dogButton.setEnabled(false);
catButton.setMnemonic(KeyEvent.VK_C);
catButton.setActionCommand("Cat");
west.add(catButton);
catButton.addActionListener(this);
catButton.setEnabled(false);
rabbitButton.setMnemonic(KeyEvent.VK_C);
rabbitButton.setActionCommand("Rabbit");
west.add(rabbitButton);
rabbitButton.addActionListener(this);
rabbitButton.setEnabled(false);
west.add(blankLabel);
west.add(instructionsLabel);
frame.add(west, BorderLayout.WEST);
//Group the radio buttons
ButtonGroup group = new ButtonGroup();
group.add(elephantButton);
group.add(camelButton);
group.add(horseButton);
group.add(dogButton);
group.add(catButton);
group.add(rabbitButton);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
// Main method to start the program
public static void main(String[] args) {
/*
* I copied this code from stackoverflow.com
* URL: https://stackoverflow.com/questions/1065691/how-to-set-the-background-color-of-a-jbutton-on-the-mac-os#:~:text=Normally%20with%20Java%20Swing%20you,RED)%3B
* This section makes Apple Mac's GUI components match Windows
*/
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
new Arimaa();
}
//Method to check if someone has won (by goal, elimination, or immobilization)
public void checkWin() {
boolean gWin = false;
boolean sWin = false;
//checks if win is by elimination
if (!goldPieceList.contains(G_RABBIT)) {
sWin = true;
} else if (!silverPieceList.contains(S_RABBIT)) {
gWin = true;
}
//checks if win is by immobilization
int gCount = 0;
int sCount = 0;
for (int a = 0; a < gridButtons.length; a++) {
for (int b = 0; b < gridButtons[0].length; b++) {
currentRow = a;
currentColumn = b;
if (gridBoard[a][b] >= 1 && gridBoard[a][b] <= 6 && checkFrozen()) {
gCount++;
if (gCount == goldPieceList.size()) {
sWin = true;
}
} else if (gridBoard[a][b] >= 7 && gridBoard[a][b] <= 12 && checkFrozen()) {
sCount++;
if (sCount == silverPieceList.size()) {
sWin = true;
}
}
}
}
//checks if win is by goal
for (int i = 0; i < gridButtons.length; i++) {
if (gridBoard[0][i] == G_RABBIT) {
gWin = true;
} else if (gridBoard[7][i] == S_RABBIT) {
sWin = true;
}
}
//If there is a win: update label and only enable reset button
if (gWin) {
goldWins++;
goldWinsLabel.setText("Gold: " + goldWins);
JOptionPane.showMessageDialog(frame, "Gold wins!");
for (int a = 0; a < gridButtons.length; a++) {
for (int b = 0; b < gridButtons[0].length; b++) {
gridButtons[a][b].setEnabled(false);
}
}
reset.setEnabled(true);
start.setEnabled(false);
push.setEnabled(false);
pull.setEnabled(false);
step.setEnabled(false);
turnDone.setEnabled(false);
} else if (sWin) {
silverWins++;
silverWinsLabel.setText("Silver: " + silverWins);
JOptionPane.showMessageDialog(frame, "Silver wins!");
for (int a = 0; a < gridButtons.length; a++) {
for (int b = 0; b < gridButtons[0].length; b++) {
gridButtons[a][b].setEnabled(false);
}
}
reset.setEnabled(true);
start.setEnabled(false);
push.setEnabled(false);
pull.setEnabled(false);
step.setEnabled(false);
turnDone.setEnabled(false);
}
}
//Method to completely reset the GUI to start again
public void resetBoard() {
turn = GOLD_TURN;
gameState = SET_UP;
setBackgrounds();
for (int a = 0; a < gridButtons.length; a++) {
for (int b = 0; b < gridButtons[0].length; b++) {
gridButtons[a][b].setEnabled(false);
gridButtons[a][b].setIcon(null);
gridBoard[a][b] = BLANK;
}
}
push.setEnabled(false);
pull.setEnabled(false);
step.setEnabled(false);
turnDone.setEnabled(false);
start.setEnabled(true);
reset.setEnabled(false);
eCounter = 1;
cCounter = 1;
hCounter = 2;
dCounter = 2;
catCounter = 2;
rCounter = 8;
stepCounter = 4;
}
//Method to check if a piece on the board is frozen
public boolean checkFrozen() {
//Shift for comparing gold pieces (valued 1-6) and silver pieces (valued 7-12) hierarchy
if(currentPieceValue <= G_ELEPHANT) {
aShift = 0;
bShift = 6;
} else {
aShift = 6;
bShift = 0;
}
//Checking the current piece's above, left, right, and down squares for if they can freeze the current piece
if((currentRow > 0) && (gridBoard[currentRow-1][currentColumn] != BLANK)) { //above
if((gridBoard[currentRow][currentColumn] <= G_ELEPHANT && gridBoard[currentRow][currentColumn] >= G_RABBIT)
&& gridBoard[currentRow-1][currentColumn] <= G_ELEPHANT) {//if there is a friendly piece
return false;
} else if((gridBoard[currentRow][currentColumn] <= S_ELEPHANT && gridBoard[currentRow][currentColumn] >= S_RABBIT)
&& gridBoard[currentRow-1][currentColumn] >= S_RABBIT){//if there is a friendly piece
return false;
}
}
if((currentColumn > 0) && (gridBoard[currentRow][currentColumn-1] != BLANK)) { //left
if((gridBoard[currentRow][currentColumn] <= G_ELEPHANT && gridBoard[currentRow][currentColumn] >= G_RABBIT)
&& gridBoard[currentRow][currentColumn-1] <= G_ELEPHANT) {//if there is a friendly piece
return false;
} else if((gridBoard[currentRow][currentColumn] <= S_ELEPHANT && gridBoard[currentRow][currentColumn] >= S_RABBIT)
&& gridBoard[currentRow][currentColumn-1] >= S_RABBIT){//if there is a friendly piece
return false;
}
}
if((currentColumn < gridButtons[0].length - 1) && (gridBoard[currentRow][currentColumn+1] != BLANK)) { //right
if((gridBoard[currentRow][currentColumn] <= G_ELEPHANT && gridBoard[currentRow][currentColumn] >= G_RABBIT)
&& gridBoard[currentRow][currentColumn+1] <= G_ELEPHANT) {//if there is a friendly piece
return false;
} else if((gridBoard[currentRow][currentColumn] <= S_ELEPHANT && gridBoard[currentRow][currentColumn] >= S_RABBIT)
&& gridBoard[currentRow][currentColumn+1] >= S_RABBIT){//if there is a friendly piece
return false;
}
}
if((currentRow < gridButtons.length - 1) && (gridBoard[currentRow+1][currentColumn] != BLANK)) { //down
if((gridBoard[currentRow][currentColumn] <= G_ELEPHANT && gridBoard[currentRow][currentColumn] >= G_RABBIT)
&& gridBoard[currentRow+1][currentColumn] <= G_ELEPHANT) {//if there is a friendly piece
return false;
} else if((gridBoard[currentRow][currentColumn] <= S_ELEPHANT && gridBoard[currentRow][currentColumn] >= S_RABBIT)
&& gridBoard[currentRow+1][currentColumn] >= S_RABBIT){//if there is a friendly piece
return false;
}
}
if((currentRow > 0) && (gridBoard[currentRow-1][currentColumn] != BLANK)) { //above
if((gridBoard[currentRow-1][currentColumn]-bShift) > currentPieceValue-aShift) {//if there's a stronger piece
if((gridBoard[currentRow][currentColumn] <= G_ELEPHANT && gridBoard[currentRow][currentColumn] >= G_RABBIT)
&& gridBoard[currentRow-1][currentColumn] >= S_RABBIT) {//if it's an opposing piece
return true;
} else if ((gridBoard[currentRow][currentColumn] <= S_ELEPHANT && gridBoard[currentRow][currentColumn] >= S_RABBIT)
&& gridBoard[currentRow-1][currentColumn] <= G_ELEPHANT) {//if it's an opposing piece
return true;
} else {
return false;
}
}
}
if((currentColumn > 0) && (gridBoard[currentRow][currentColumn-1] != BLANK)) { //left
if((gridBoard[currentRow][currentColumn-1]-bShift) > currentPieceValue-aShift) {//if there's a stronger piece
if((gridBoard[currentRow][currentColumn] <= G_ELEPHANT && gridBoard[currentRow][currentColumn] >= G_RABBIT)
&& gridBoard[currentRow][currentColumn-1] >= S_RABBIT) {//if it's an opposing piece
return true;
} else if ((gridBoard[currentRow][currentColumn] <= S_ELEPHANT && gridBoard[currentRow][currentColumn] >= S_RABBIT)
&& gridBoard[currentRow][currentColumn-1] <= G_ELEPHANT) {//if it's an opposing piece
return true;
} else {
return false;
}
}
}
if((currentColumn < gridButtons[0].length - 1) && (gridBoard[currentRow][currentColumn+1] != BLANK)) { //right
if((gridBoard[currentRow][currentColumn+1]-bShift) > currentPieceValue-aShift) {//if there's a stronger piece
if((gridBoard[currentRow][currentColumn] <= G_ELEPHANT && gridBoard[currentRow][currentColumn] >= G_RABBIT)
&& gridBoard[currentRow][currentColumn+1] >= S_RABBIT) {//if it's an opposing piece
return true;
} else if ((gridBoard[currentRow][currentColumn] <= S_ELEPHANT && gridBoard[currentRow][currentColumn] >= S_RABBIT)
&& gridBoard[currentRow][currentColumn+1] <= G_ELEPHANT) {//if it's an opposing piece
return true;
} else {
return false;
}
}
}
if((currentRow < gridButtons.length - 1) && (gridBoard[currentRow+1][currentColumn] != BLANK)) { //down
if((gridBoard[currentRow+1][currentColumn]-bShift) > currentPieceValue-aShift) {//if there's a stronger piece
if((gridBoard[currentRow][currentColumn] <= G_ELEPHANT && gridBoard[currentRow][currentColumn] >= G_RABBIT)
&& gridBoard[currentRow+1][currentColumn] >= S_RABBIT) {//if it's an opposing piece
return true;
} else if ((gridBoard[currentRow][currentColumn] <= S_ELEPHANT && gridBoard[currentRow][currentColumn] >= S_RABBIT)
&& gridBoard[currentRow+1][currentColumn] <= G_ELEPHANT) {//if it's an opposing piece
return true;
} else {
return false;
}
}
}
return false;
}
//Method to check if a piece in a trap square has been 'captured' (no adjacent friendly pieces)
public void checkCaptured() {
setBackgrounds();
//for loop to check each of the four trap squares
for(int i = 0; i < 4; i++) {
if(i == 0) {
currentRow = 2;
currentColumn = 2;
} else if(i == 1) {
currentRow = 2;
currentColumn = 5;
} else if (i == 2) {
currentRow = 5;
currentColumn = 2;
} else if (i == 3){
currentRow = 5;
currentColumn = 5;
}
//Checking if the silver or gold piece has no adjacent friendly pieces, and if so, removing it from the game
if (gridBoard[currentRow][currentColumn] <= G_ELEPHANT && gridBoard[currentRow][currentColumn] >= G_RABBIT) {
if (!(gridBoard[currentRow - 1][currentColumn] <= G_ELEPHANT && gridBoard[currentRow - 1][currentColumn] >= G_RABBIT)
&& !(gridBoard[currentRow + 1][currentColumn] <= G_ELEPHANT && gridBoard[currentRow + 1][currentColumn] >= G_RABBIT)
&& !(gridBoard[currentRow][currentColumn - 1] <= G_ELEPHANT && gridBoard[currentRow][currentColumn - 1] >= G_RABBIT)
&& !(gridBoard[currentRow][currentColumn + 1] <= G_ELEPHANT && gridBoard[currentRow][currentColumn + 1] >= G_RABBIT)) {
goldPieceList.remove(goldPieceList.indexOf(gridBoard[currentRow][currentColumn]));
gridButtons[currentRow][currentColumn].setIcon(null);
gridBoard[currentRow][currentColumn] = BLANK;
}
} else if (gridBoard[currentRow][currentColumn] <= S_ELEPHANT && gridBoard[currentRow][currentColumn] >= S_RABBIT) {
if (!(gridBoard[currentRow - 1][currentColumn] >= S_RABBIT)
&& !(gridBoard[currentRow + 1][currentColumn] >= S_RABBIT)
&& !(gridBoard[currentRow][currentColumn - 1] >= S_RABBIT)
&& !(gridBoard[currentRow][currentColumn + 1] >= S_RABBIT)) {
silverPieceList.remove(silverPieceList.indexOf(gridBoard[currentRow][currentColumn]));
gridButtons[currentRow][currentColumn].setIcon(null);
gridBoard[currentRow][currentColumn] = BLANK;
}
}
}
}
//Method for checking if a piece can push another adjacent piece
public boolean checkPush() {
//shifting down silver to compare with gold depending on if it's silver pushing OR getting pushed
if(currentPieceValue <= G_ELEPHANT) {
aShift = 0;
bShift = 6;
} else {
aShift = 6;
bShift = 0;
}
if(stepCounter < 2) {
return false;
} else {
//A push is possible if an adjacent square houses an opponenent's weaker piece
if((currentRow > 0) && (gridBoard[currentRow-1][currentColumn] != BLANK) && ((gridBoard[currentRow-1][currentColumn]-bShift) < currentPieceValue-aShift)) { //above
if(turn == GOLD_TURN && gridBoard[currentRow-1][currentColumn] >= S_RABBIT) {
return true;
} else if (turn == SILVER_TURN && gridBoard[currentRow-1][currentColumn] <= G_ELEPHANT) {
return true;
} else {
return false;
}
}
if((currentColumn > 0) && (gridBoard[currentRow][currentColumn-1] != BLANK) && ((gridBoard[currentRow][currentColumn-1]-bShift) < currentPieceValue-aShift)) { //left
if(turn == GOLD_TURN && gridBoard[currentRow][currentColumn-1] >= S_RABBIT) {
return true;
} else if (turn == SILVER_TURN && gridBoard[currentRow][currentColumn-1] <= G_ELEPHANT) {
return true;
} else {
return false;
}
}
if((currentColumn < gridButtons[0].length - 1) && (gridBoard[currentRow][currentColumn+1] != BLANK) && ((gridBoard[currentRow][currentColumn+1]-bShift) < currentPieceValue-aShift)) { //right
if(turn == GOLD_TURN && gridBoard[currentRow][currentColumn+1] >= S_RABBIT) {
return true;
} else if (turn == SILVER_TURN && gridBoard[currentRow][currentColumn+1] <= G_ELEPHANT) {
return true;
} else {
return false;
}
}
if((currentRow < gridButtons.length - 1) && (gridBoard[currentRow+1][currentColumn] != BLANK) && ((gridBoard[currentRow+1][currentColumn]-bShift) < currentPieceValue-aShift)) { //down
if(turn == GOLD_TURN && gridBoard[currentRow+1][currentColumn] >= S_RABBIT) {
return true;
} else if (turn == SILVER_TURN && gridBoard[currentRow+1][currentColumn] <= G_ELEPHANT) {
return true;
} else {
return false;
}
}
}
return false;
}
//method for checking if a piece can pull
public boolean checkPull() {
//Shift for comparing gold pieces (valued 1-6) and silver pieces (valued 7-12) hierarchy
if (currentPieceValue <= G_ELEPHANT) {
aShift = 0;
bShift = 6;
} else {
aShift = 6;
bShift = 0;
}
boolean conditionOne = false;
boolean conditionTwo = false;
//Checking if a pull is possible if an adjacent square houses an opponenent's weaker piece
if ((currentRow > 0) && (gridBoard[currentRow - 1][currentColumn] - bShift) < currentPieceValue - aShift) { //above
if (gridBoard[currentRow - 1][currentColumn] == BLANK) {
conditionOne = true;
} else if (turn == GOLD_TURN && gridBoard[currentRow - 1][currentColumn] >= S_RABBIT) {
conditionTwo = true;
} else if (turn == SILVER_TURN && gridBoard[currentRow - 1][currentColumn] <= G_ELEPHANT) {
conditionTwo = true;
}
}
if ((currentColumn > 0) && (gridBoard[currentRow][currentColumn - 1] - bShift) < currentPieceValue - aShift) { //left
if (gridBoard[currentRow][currentColumn - 1] == BLANK) {
conditionOne = true;
} else if (turn == GOLD_TURN && gridBoard[currentRow][currentColumn - 1] >= S_RABBIT) {
conditionTwo = true;
} else if (turn == SILVER_TURN && gridBoard[currentRow][currentColumn - 1] <= G_ELEPHANT) {
conditionTwo = true;
}
}
if ((currentColumn < gridButtons[0].length - 1) && (gridBoard[currentRow][currentColumn + 1] - bShift) < currentPieceValue - aShift) { //right
if (gridBoard[currentRow][currentColumn + 1] == BLANK) {
conditionOne = true;
} else if (turn == GOLD_TURN && gridBoard[currentRow][currentColumn + 1] >= S_RABBIT) {
conditionTwo = true;
} else if (turn == SILVER_TURN && gridBoard[currentRow][currentColumn + 1] <= G_ELEPHANT) {
conditionTwo = true;
}
}
if ((currentRow < gridButtons.length - 1) && (gridBoard[currentRow + 1][currentColumn] - bShift) < currentPieceValue - aShift) { //down
if (gridBoard[currentRow + 1][currentColumn] == BLANK) {
conditionOne = true;
} else if (turn == GOLD_TURN && gridBoard[currentRow + 1][currentColumn] >= S_RABBIT) {
conditionTwo = true;
} else if (turn == SILVER_TURN && gridBoard[currentRow + 1][currentColumn] <= G_ELEPHANT) {
conditionTwo = true;
}
}
if (conditionOne && conditionTwo) {
return true;
}
return false;
}
//method for letting a piece push or pull
public void pushOrPull() {
//disabling all grid buttons first so they can't be selected for a move
for (int a = 0; a < gridButtons.length; a++) {
for (int b = 0; b < gridButtons[0].length; b++) {
gridButtons[a][b].setEnabled(false);
}
}
//shifting down silver to compare with gold depending on if it's silver pushing OR getting pushed
if(currentPieceValue <= G_ELEPHANT) {
aShift = 0;
bShift = 6;
} else {
aShift = 6;
bShift = 0;
}
//enabling and highlighting the spaces with opponent's weaker pieces to push or pull
if((currentRow > 0) && (gridBoard[currentRow-1][currentColumn] != BLANK) && ((gridBoard[currentRow-1][currentColumn]-bShift) < currentPieceValue-aShift)) { //above
if(turn == GOLD_TURN && gridBoard[currentRow-1][currentColumn] >= S_RABBIT) {
gridButtons[currentRow-1][currentColumn].setEnabled(true);
gridButtons[currentRow-1][currentColumn].setBackground(Color.GREEN);
} else if (turn == SILVER_TURN && gridBoard[currentRow-1][currentColumn] <= G_ELEPHANT) {
gridButtons[currentRow-1][currentColumn].setEnabled(true);
gridButtons[currentRow-1][currentColumn].setBackground(Color.GREEN);
}
}
if((currentColumn > 0) && (gridBoard[currentRow][currentColumn-1] != BLANK) && ((gridBoard[currentRow][currentColumn-1]-bShift) < currentPieceValue-aShift)) { //left
if(turn == GOLD_TURN && gridBoard[currentRow][currentColumn-1] >= S_RABBIT) {
gridButtons[currentRow][currentColumn-1].setEnabled(true);
gridButtons[currentRow][currentColumn-1].setBackground(Color.GREEN);
} else if (turn == SILVER_TURN && gridBoard[currentRow][currentColumn-1] <= G_ELEPHANT) {
gridButtons[currentRow][currentColumn-1].setEnabled(true);
gridButtons[currentRow][currentColumn-1].setBackground(Color.GREEN);
}
}
if((currentColumn < gridBoard.length - 1) && (gridBoard[currentRow][currentColumn+1] != BLANK) && ((gridBoard[currentRow][currentColumn+1]-bShift) < currentPieceValue-aShift)) { //right
if(turn == GOLD_TURN && gridBoard[currentRow][currentColumn+1] >= S_RABBIT) {
gridButtons[currentRow][currentColumn+1].setEnabled(true);
gridButtons[currentRow][currentColumn+1].setBackground(Color.GREEN);
} else if (turn == SILVER_TURN && gridBoard[currentRow][currentColumn+1] <= G_ELEPHANT) {
gridButtons[currentRow][currentColumn+1].setEnabled(true);
gridButtons[currentRow][currentColumn+1].setBackground(Color.GREEN);
}
}
if((currentRow < gridBoard.length - 1) && (gridBoard[currentRow+1][currentColumn] != BLANK) && ((gridBoard[currentRow+1][currentColumn]-bShift) < currentPieceValue-aShift)) { //down
if(turn == GOLD_TURN && gridBoard[currentRow+1][currentColumn] >= S_RABBIT) {
gridButtons[currentRow+1][currentColumn].setEnabled(true);
gridButtons[currentRow+1][currentColumn].setBackground(Color.GREEN);
} else if (turn == SILVER_TURN && gridBoard[currentRow+1][currentColumn] <= G_ELEPHANT) {
gridButtons[currentRow+1][currentColumn].setEnabled(true);
gridButtons[currentRow+1][currentColumn].setBackground(Color.GREEN);
}
}
}
//method for checking if a piece can step
public boolean checkStep() {
if (currentRow > 0 && gridBoard[currentRow - 1][currentColumn] == BLANK) { //above
return true;
}
if (currentColumn > 0 && gridBoard[currentRow][currentColumn - 1] == BLANK) { //left
return true;
}
if (currentColumn < gridBoard[0].length - 1 && gridBoard[currentRow][currentColumn + 1] == BLANK) { //right
return true;
}
if (currentRow < gridBoard.length - 1 && gridBoard[currentRow + 1][currentColumn] == BLANK) { //down
return true;
}
return false;
}
//method for letting a piece step
public void step() {
for (int a = 0; a < gridButtons.length; a++) {
for (int b = 0; b < gridButtons[0].length; b++) {
gridButtons[a][b].setEnabled(false);
}
}
if (currentRow > 0 && gridBoard[currentRow - 1][currentColumn] == BLANK
&& gridBoard[currentRow][currentColumn] != S_RABBIT) { //above
gridButtons[currentRow - 1][currentColumn].setEnabled(true);
gridButtons[currentRow - 1][currentColumn].setBackground(Color.GREEN);
}
if (currentColumn > 0 && gridBoard[currentRow][currentColumn - 1] == BLANK) { //left
gridButtons[currentRow][currentColumn - 1].setEnabled(true);
gridButtons[currentRow][currentColumn - 1].setBackground(Color.GREEN);
}
if (currentColumn < gridBoard[0].length - 1 && gridBoard[currentRow][currentColumn + 1] == BLANK) { //right
gridButtons[currentRow][currentColumn + 1].setEnabled(true);
gridButtons[currentRow][currentColumn + 1].setBackground(Color.GREEN);
}
if (currentRow < gridBoard.length - 1 && gridBoard[currentRow + 1][currentColumn] == BLANK
&& gridBoard[currentRow][currentColumn] != G_RABBIT) { //down
gridButtons[currentRow + 1][currentColumn].setEnabled(true);
gridButtons[currentRow + 1][currentColumn].setBackground(Color.GREEN);
}
}
//enable pieces for making next move
public void enablePieces() {
if (turn == GOLD_TURN) {
for (int c = 0; c < gridButtons.length; c++) {
for (int d = 0; d < gridButtons[0].length; d++) {
if (gridBoard[c][d] >= 1 && gridBoard[c][d] <= 6) {
gridButtons[c][d].setEnabled(true);
} else {
gridButtons[c][d].setEnabled(false);
}
}
}
} else {
for (int c = 0; c < gridButtons.length; c++) {
for (int d = 0; d < gridButtons[0].length; d++) {
if (gridBoard[c][d] >= 7 && gridBoard[c][d] <= 12) {
gridButtons[c][d].setEnabled(true);
} else {
gridButtons[c][d].setEnabled(false);
}
}
}
}
}
// makes all backgrounds black and trap squares red
public void setBackgrounds() {
for (int i = 0; i < gridButtons.length; i++) {
for (int j = 0; j < gridButtons[0].length; j++) {
gridButtons[i][j].setBackground(Color.BLACK);
}
}
gridButtons[2][2].setBackground(Color.RED);
gridButtons[2][5].setBackground(Color.RED);
gridButtons[5][2].setBackground(Color.RED);
gridButtons[5][5].setBackground(Color.RED);
}
// Creates an action event for user interaction
@Override
public void actionPerformed(ActionEvent event) {
// when reset is pressed, call resetBoard
if (event.getSource().equals(reset)) {
resetBoard();
}
// when turn done is pressed, switch turn
if (event.getSource().equals(turnDone)) {
turnDone.setEnabled(false);
setBackgrounds();
push.setEnabled(false);
pull.setEnabled(false);
step.setEnabled(false);
stepCounter = 4;
if (turn == SILVER_TURN) {
turn = GOLD_TURN;
for (int c = 0; c < gridButtons.length; c++) {
for (int d = 0; d < gridButtons[0].length; d++) {
if (gridBoard[c][d] >= 1 && gridBoard[c][d] <= 6) {
gridButtons[c][d].setEnabled(true);
} else {
gridButtons[c][d].setEnabled(false);
}
}
}
} else {
turn = SILVER_TURN;
for (int c = 0; c < gridButtons.length; c++) {
for (int d = 0; d < gridButtons[0].length; d++) {
if (gridBoard[c][d] >= 7 && gridBoard[c][d] <= 12) {
gridButtons[c][d].setEnabled(true);
} else {
gridButtons[c][d].setEnabled(false);
}
}
}
}
}
//when game state is play allow move buttons to be used
if (gameState == PLAY) {
if(stepCounter < 4) {
turnDone.setEnabled(true);
}
if (stepCounter > 0) {
//highlighting the piece a user wants to move and checking if they can step, push or pull
for (int a = 0; a < gridButtons.length; a++) {
for (int b = 0; b < gridButtons[0].length; b++) {
if (moveState == TEMP) {
if (event.getSource().equals(gridButtons[a][b])) {
gridButtons[a][b].setBackground(Color.BLUE);
currentButton = gridButtons[a][b];
currentRow = a;
currentColumn = b;
currentPieceValue = gridBoard[currentRow][currentColumn];
turnDone.setEnabled(false);
for (int c = 0; c < gridButtons.length; c++) {
for (int d = 0; d < gridButtons[0].length; d++) {
gridButtons[c][d].setEnabled(false);
}
}
gridButtons[a][b].setEnabled(true);
// Enabled buttons if piece isn't frozen and if action is possible
if (!checkFrozen()) {
if (checkPush() && stepCounter >= 2) {
push.setEnabled(true);
}
if (checkPull() && stepCounter >= 2) {
pull.setEnabled(true);
}
if (checkStep()) {
step.setEnabled(true);
}
} else {
JOptionPane.showMessageDialog(frame, "Piece is frozen, choose another!");
if (stepCounter <= 3) {
turnDone.setEnabled(true);
}
setBackgrounds();
enablePieces();
}
}
}
}
}
// when step is pressed, call step
if (event.getSource().equals(step)) {
step.setEnabled(false);
pull.setEnabled(false);
push.setEnabled(false);
turnDone.setEnabled(false);
moveState = STEP;
step();
}
boolean stepDone = false;
//if the user made a step move to another square, change the current square data to that square for the 'step'
if (moveState == STEP) {
for (int a = 0; a < gridButtons.length; a++) {
for (int b = 0; b < gridButtons[0].length; b++) {
if (event.getSource().equals(gridButtons[a][b])) {
gridButtons[a][b].setIcon(currentButton.getIcon());
gridBoard[a][b] = gridBoard[currentRow][currentColumn];
currentButton.setIcon(null);
currentButton.setBackground(Color.BLACK);
setBackgrounds();
gridBoard[currentRow][currentColumn] = BLANK;
stepDone = true;
}
}
}
if (stepDone) {
enablePieces();
checkCaptured();
checkWin();
turnDone.setEnabled(true);
stepCounter--;
if (stepCounter == 0) {
for (int c = 0; c < gridButtons.length; c++) {
for (int d = 0; d < gridButtons[0].length; d++) {
gridButtons[c][d].setEnabled(false);
}
}
if (turn == GOLD_TURN) {
JOptionPane.showMessageDialog(frame, "It's silver's turn! Click turn done.");
} else {
JOptionPane.showMessageDialog(frame, "It's gold's turn! Click turn done.");
}
}
moveState = TEMP;
}
}
// when push is pressed, call push
if (event.getSource().equals(push)) {
push.setEnabled(false);
pull.setEnabled(false);
step.setEnabled(false);
turnDone.setEnabled(false);
moveState = PUSH;
pushOrPull();
}
boolean pushDone = false;
boolean pushAllow = false;
//if the user made a step move to another square, change the current square data to that square for the 'step'
if (moveState == PUSH) {
if (pushPart == PART_ONE) {
for (int a = 0; a < gridButtons.length; a++) {
for (int b = 0; b < gridButtons[0].length; b++) {
if (event.getSource().equals(gridButtons[a][b])) {
initalRow = currentRow;
initialColumn = currentColumn;
initialButton = gridButtons[currentRow][currentColumn];
//user clicked green weaker piece, which turns blue, and all other green returns to black
gridButtons[a][b].setBackground(Color.BLUE);
gridButtons[currentRow][currentColumn].setBackground(Color.BLACK);
if (currentRow > 0 && gridButtons[currentRow - 1][currentColumn] != gridButtons[a][b]) { //above
gridButtons[currentRow - 1][currentColumn].setBackground(Color.BLACK);
}
if (currentColumn > 0 && gridButtons[currentRow][currentColumn - 1] != gridButtons[a][b]) { //left
gridButtons[currentRow][currentColumn - 1].setBackground(Color.BLACK);
}
if (currentColumn < gridBoard.length - 1 && gridButtons[currentRow][currentColumn + 1] != gridButtons[a][b]) { //right
gridButtons[currentRow][currentColumn + 1].setBackground(Color.BLACK);
}
if (currentRow < gridBoard.length - 1 && gridButtons[currentRow + 1][currentColumn] != gridButtons[a][b]) { //down
gridButtons[currentRow + 1][currentColumn].setBackground(Color.BLACK);
}
//options for where the blue weaker piece could be pushed to highlighted green/enabled and all other grid buttons disabled
currentRow = a;
currentColumn = b;
currentButton = gridButtons[a][b];
for (int c = 0; c < gridButtons.length; c++) {
for (int d = 0; d < gridButtons[0].length; d++) {
gridButtons[c][d].setEnabled(false);
}
}
JOptionPane.showMessageDialog(frame, "Choose where you would like to push to");
if (currentRow > 0 && gridBoard[currentRow - 1][currentColumn] == BLANK) { //above
gridButtons[currentRow - 1][currentColumn].setEnabled(true);
gridButtons[currentRow - 1][currentColumn].setBackground(Color.GREEN);
pushAllow = true;
}
if (currentColumn > 0 && gridBoard[currentRow][currentColumn - 1] == BLANK) { //left
gridButtons[currentRow][currentColumn - 1].setEnabled(true);
gridButtons[currentRow][currentColumn - 1].setBackground(Color.GREEN);