-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameLogic.java
410 lines (363 loc) · 15.2 KB
/
GameLogic.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import java.util.ArrayList;
import java.awt.Color;
import java.util.HashMap;
/**
* Contains the logic neccessary for a game of checkers
*
* @author Patrick McCarty
*/
public class GameLogic {
private static CheckersBoard cb; // Checkers board of this game session
public Square previouslyClicked = null; // The Square that was previously clicked
// list of moves from the previously pressed square
public HashMap<Square, ArrayList<Square>> movesList = null;
public int currPlayer; // The player whose turn it is. 1 for player1 and 2 for player2
public static final int ROWS = 8; // Total number of rows on this board
public static final int COLS = 8; // Total number of columns on this board
private static final Color HIGHLIGHTCOLOR = Color.green;
/**
* Constructs a GameLogic object
*
* @param the checkers board that will be played with
*/
public GameLogic(CheckersBoard board) {
this.currPlayer = 1;
cb = board;
}
/**
* Return a Square object at a specified row and column
*
* @param i row of desired Square
* @param j col of desired Square
* @return the desired Square
*/
private static Square getSquare(int i, int j) {
return cb.getSquares()[i][j];
}
/**
* Highlight a square by changing its background color
*
* @param the square that will be highlighted
*/
public void highlight(Square s) {
s.setBackground(HIGHLIGHTCOLOR);
cb.getHighlighted().add(s);
}
/**
* Unhighlight everything that is already highlighted by changing its color to
* black and removing it from the ArrayList
*/
public void clearHighlighted() {
ArrayList<Square> h = cb.getHighlighted();
for (int i = h.size() - 1; i >= 0; i--) {
h.remove(i).setBackground(Color.black);
}
}
/**
* Move a piece on a square to a different square
*
* @param curr the Square where the piece is currently on
* @param dest the destination Square where the piece should be moved to
*/
public void simpleMove(Square curr, Square dest) {
replacePlayerPiece(curr, dest);
dest.placePiece(curr.getPiece(), curr.getPlayer());
curr.removePiece();
this.currPlayer = this.currPlayer == 1 ? 2 : 1; // change player
}
/**
* Get the Square that is diagonally up and to the left.
*
* @param tile the Square whose diagonal we want
* @return the Square to the upper left or null if out of bounds
*/
private static Square getUpperLeft(Square tile) {
int[] coords = tile.getCoords();
int row = coords[0];
int col = coords[1];
return (row - 1 >= 0 && col - 1 >= 0) ? getSquare(row - 1, col - 1) : null;
}
/**
* Get the Square that is diagonally up and to the right.
*
* @param tile the Square whose diagonal we want
* @return the Square to the upper right or null if out of bounds
*/
private static Square getUpperRight(Square tile) {
int[] coords = tile.getCoords();
int row = coords[0];
int col = coords[1];
return (row - 1 >= 0 && col + 1 < COLS) ? getSquare(row - 1, col + 1) : null;
}
/**
* Get the Square that is diagonally down and to the left.
*
* @param tile the Square whose diagonal we want
* @return the Square to the lower left or null if out of bounds
*/
private static Square getLowerLeft(Square tile) {
int[] coords = tile.getCoords();
int row = coords[0];
int col = coords[1];
return (row + 1 < ROWS && col - 1 >= 0) ? getSquare(row + 1, col - 1) : null;
}
/**
* Get the Square that is diagonally down and to the right.
*
* @param tile the Square whose diagonal we want
* @return the Square to the lower right or null if out of bounds
*/
private static Square getLowerRight(Square tile) {
int[] coords = tile.getCoords();
int row = coords[0];
int col = coords[1];
return (row + 1 < ROWS && col + 1 < COLS) ? getSquare(row + 1, col + 1) : null;
}
/**
* Gets the available moves of a red pawn
*
* @param tile tile where the red pawn resides
* @return a HashMap where keys are final destination for moves and values are
* all the kills genereated by that move
*/
public static HashMap<Square, ArrayList<Square>> getRedPawnMoves(Square tile) {
HashMap<Square, ArrayList<Square>> map = new HashMap<Square, ArrayList<Square>>();
ArrayList<Square> queue = new ArrayList<Square>();
ArrayList<ArrayList<Square>> kills = new ArrayList<ArrayList<Square>>();
ArrayList<Character> origins = new ArrayList<Character>();
ArrayList<Boolean> enemyJumped = new ArrayList<Boolean>();
queue.add(getUpperLeft(tile));
kills.add(new ArrayList<Square>());
origins.add('L');
enemyJumped.add(false);
queue.add(getUpperRight(tile));
kills.add(new ArrayList<Square>());
origins.add('R');
enemyJumped.add(false);
while (queue.size() > 0) {
Square curr = queue.remove(0);
ArrayList<Square> kill = kills.remove(0);
char origin = origins.remove(0);
boolean jumped = enemyJumped.remove(0);
if (curr == null) {
continue;
}
if (curr.getPiece().equals("none")) { // square is empty. valid move
ArrayList<Square> neoKill = new ArrayList<Square>(); // make deep copy
neoKill.addAll(kill);
map.putIfAbsent(curr, neoKill);
if (jumped) { // reached this square by jumping over an enemy
if (getUpperLeft(curr) != null
&& !getUpperLeft(curr).getPiece().equals("none")) {
queue.add(getUpperLeft(curr));
origins.add('L');
enemyJumped.add(false);
ArrayList<Square> deepCopyLeft = new ArrayList<Square>(); // make deep copy
deepCopyLeft.addAll(neoKill);
kills.add(deepCopyLeft); // probably have to change this to neoKill
}
if (getUpperRight(curr) != null
&& !getUpperRight(curr).getPiece().equals("none")) {
queue.add(getUpperRight(curr));
origins.add('R');
enemyJumped.add(false);
ArrayList<Square> deepCopyRight = new ArrayList<Square>(); // make deep copy
deepCopyRight.addAll(neoKill);
kills.add(deepCopyRight);
}
}
} else if (!curr.getPlayer().equals(tile.getPlayer()) && !jumped) {
// square has enemy. could potentially kill enemy
ArrayList<Square> neoKill = new ArrayList<Square>(); // make deep copy
neoKill.addAll(kill);
neoKill.add(curr);
kills.add(neoKill);
if (origin == 'L') {
queue.add(getUpperLeft(curr));
origins.add('L');
enemyJumped.add(true);
} else if (origin == 'R') {
queue.add(getUpperRight(curr));
origins.add('R');
enemyJumped.add(true);
}
}
}
return map;
}
/**
* Gets the available moves of a lavander pawn
*
* @param tile tile where the lavander pawn resides
* @return a HashMap where keys are final destination for moves and values are
* all the kills genereated by that move
*/
public static HashMap<Square, ArrayList<Square>> getLavPawnMoves(Square tile) {
HashMap<Square, ArrayList<Square>> map = new HashMap<Square, ArrayList<Square>>();
ArrayList<Square> queue = new ArrayList<Square>();
ArrayList<ArrayList<Square>> kills = new ArrayList<ArrayList<Square>>();
ArrayList<Character> origins = new ArrayList<Character>();
ArrayList<Boolean> enemyJumped = new ArrayList<Boolean>();
queue.add(getLowerLeft(tile));
kills.add(new ArrayList<Square>());
origins.add('L');
enemyJumped.add(false);
queue.add(getLowerRight(tile));
kills.add(new ArrayList<Square>());
origins.add('R');
enemyJumped.add(false);
while (queue.size() > 0) {
Square curr = queue.remove(0);
ArrayList<Square> kill = kills.remove(0);
char origin = origins.remove(0);
boolean jumped = enemyJumped.remove(0);
if (curr == null) {
continue;
}
if (curr.getPiece().equals("none")) { // square is empty. valid move
ArrayList<Square> neoKill = new ArrayList<Square>();
neoKill.addAll(kill);
map.putIfAbsent(curr, neoKill);
if (jumped) { // reached this square by jumping over an enemy
if (getLowerLeft(curr) != null
&& !getLowerLeft(curr).getPiece().equals("none")) {
queue.add(getLowerLeft(curr));
origins.add('L');
enemyJumped.add(false);
ArrayList<Square> deepCopyLeft = new ArrayList<Square>(); // make deep copy
deepCopyLeft.addAll(neoKill);
kills.add(deepCopyLeft);
}
if (getLowerRight(curr) != null
&& !getLowerRight(curr).getPiece().equals("none")) {
queue.add(getLowerRight(curr));
origins.add('R');
enemyJumped.add(false);
ArrayList<Square> deepCopyRight = new ArrayList<Square>(); // make deep copy
deepCopyRight.addAll(neoKill);
kills.add(deepCopyRight);
}
}
} else if (!curr.getPlayer().equals(tile.getPlayer()) && !jumped) {
// square has enemy. could potentially kill enemy
ArrayList<Square> neoKill = new ArrayList<Square>(); // make deep copy
neoKill.addAll(kill);
neoKill.add(curr);
kills.add(neoKill);
if (origin == 'L') {
queue.add(getLowerLeft(curr));
origins.add('L');
enemyJumped.add(true);
} else if (origin == 'R') {
queue.add(getLowerRight(curr));
origins.add('R');
enemyJumped.add(true);
}
}
}
return map;
}
/**
* Promote a pawn to a queen. If pawn cannot be promoted do nothing
*
* @param the pawn of interest
*/
public void checkAndPromoteToQueen(Square tile) {
if (!tile.getPiece().equals("pawn")) { // only pawn can become queens
return;
}
if (tile.getPlayer().equals("player1") && tile.getCoords()[0] == 0) {
tile.promoteToQueen();
} else if (tile.getPlayer().equals("player2") && tile.getCoords()[0] == ROWS - 1) {
tile.promoteToQueen();
}
}
/**
* Merge the contents of m1 into m2
*
* @param m1 move map where moves will be moved from
* @param m2 move map where where entries will be moved into
* @return a hashmap with thr merged contents of the two arguments
*/
public static HashMap<Square, ArrayList<Square>> mergeTwoMoveMaps
(HashMap<Square, ArrayList<Square>> m1, HashMap<Square, ArrayList<Square>> m2) {
Square[] keys1 = m1.keySet().toArray(new Square[0]);
for (int i = 0; i < keys1.length; i++) {
m2.putIfAbsent(keys1[i], m1.get(keys1[i]));
}
return m2;
}
/**
* Gets the available moves of a queen
*
* @param tile tile where the red pawn resides
* @return a HashMap where keys are final destination for moves and values are
* all the kills genereated by that move
*/
public static HashMap<Square, ArrayList<Square>> getQueenMoves(Square tile) {
HashMap<Square, ArrayList<Square>> map1 = getRedPawnMoves(tile);
HashMap<Square, ArrayList<Square>> map2 = getLavPawnMoves(tile);
HashMap<Square, ArrayList<Square>> mergedMap = mergeTwoMoveMaps(map1, map2);
return mergedMap;
}
/**
* Determine whether a given player still has any available moves
*
* @param pieces ArrayList containing the squares controled by the given player
* @return true if the player still has available moves. false otherwise.
*/
public static boolean playerHasMoves(String p) {
ArrayList<Square> pieces = cb.getPlayerPieces(p);
if (pieces == null || pieces.size() == 0) { // player has no pieces remaining
return false;
}
for (int i = 0; i < pieces.size(); i++) {
Square tile = pieces.get(i);
HashMap<Square, ArrayList<Square>> map = null;
if (tile.getPiece().equals("pawn")) {
if (tile.getPlayer().equals("player1")) { // red pawn
map = getRedPawnMoves(tile);
} else { // lavander pawn
map = getLavPawnMoves(tile);
}
} else if (tile.getPiece().equals("queen")) {
map = getQueenMoves(tile);
} else {
System.out.println("Problem in playerHasMoves()");
}
if (map != null && map.size() > 0) {
return true;
}
}
return false;
}
/**
* Remove a given piece from the board and from the list of player pieces
*
* @param tile The square where the piece must be removed from
*/
public static void eliminate(Square tile) {
ArrayList<Square> l1 = cb.getPlayerPieces(tile.getPlayer());
l1.remove(tile);
tile.removePiece();
}
/**
* A piece has moved from one square to another. Remove the old square from the
* list
* of player pieces and add the new square
*
* @param former Square that currently has the piece that has moved
* @param latter Square that the piece will be moved to
*/
public static void replacePlayerPiece(Square former, Square latter) {
ArrayList<Square> piec = cb.getPlayerPieces(former.getPlayer());
piec.remove(former);
piec.add(latter);
}
/**
* Display a message to the screen that a player has won
*/
public static void displayWin(String message) {
cb.interruptMessage(message);
}
}