-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChessModel.java
475 lines (415 loc) · 13.7 KB
/
ChessModel.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
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
import java.util.ArrayList;
/**************************************************************
* Functionality and interactions between ChessPieces
*
* @author Richard
* @author Steven
* @version 3/13/2022
**************************************************************/
public class ChessModel {
private ChessPiece[][] board;
private Player player;
private ArrayList<Move> moves = new ArrayList<Move>();
private ArrayList<ChessPiece> fromPieces = new ArrayList<ChessPiece>();
private ArrayList<ChessPiece> toPieces = new ArrayList<ChessPiece>();
// declare other instance variables as needed
/*************************************************************
* Constructor that fills the chess board with chess pieces
*************************************************************/
public ChessModel() {
board = new ChessPiece[8][8];
player = Player.WHITE;
// example of how to create and set a couple pieces on board initially
board[0][3] = new Queen(Player.BLACK);
board[0][4] = new King(Player.BLACK);
board[7][3] = new Queen(Player.WHITE);
board[7][4] = new King(Player.WHITE);
/** New Pieces */
board[0][0] = new Rook(Player.BLACK);
board[0][7] = new Rook(Player.BLACK);
board[7][0] = new Rook(Player.WHITE);
board[7][7] = new Rook(Player.WHITE);
board[0][1] = new Knight(Player.BLACK);
board[0][6] = new Knight(Player.BLACK);
board[7][1] = new Knight(Player.WHITE);
board[7][6] = new Knight(Player.WHITE);
board[0][2] = new Bishop(Player.BLACK);
board[0][5] = new Bishop(Player.BLACK);
board[7][2] = new Bishop(Player.WHITE);
board[7][5] = new Bishop(Player.WHITE);
for(int x = 0; x < numColumns(); x++) {
board[1][x] = new Pawn(Player.BLACK);
}
for(int x = 0; x < numColumns(); x++) {
board[6][x] = new Pawn(Player.WHITE);
}
}
/*************************************************************
* Determines if the game has ended due to checkmate
* or a lack of check free options
*
* @return boolean
*************************************************************/
public boolean isComplete() {
if(!inCheck(currentPlayer())) {
return false;
}
for(int row = 0; row < numRows(); row++) {
for(int col = 0; col < numColumns(); col++) {
if(pieceAt(row, col) != null) {
if(pieceAt(row, col).player() == currentPlayer()) {
for(int toRow = 0; toRow < numRows(); toRow++) {
for(int toCol = 0; toCol < numRows(); toCol++) {
Move move = new Move(row, col, toRow, toCol);
if(isValidMove(move)) {
move(move);
setNextPlayer();
if(inCheck(currentPlayer())) {
System.out.println("still in check");
System.out.println(currentPlayer());
undo();
} else {
System.out.println("out of check");
System.out.println(currentPlayer());
undo();
return false;
}
}
}
}
}
}
}
}
return true;
}
/*************************************************************
* Determines if a determined move is possible for the
* specific chesspiece
*
* @param move
* @return boolean
*************************************************************/
public boolean isValidMove(Move move) {
if(move != null) {
int fromRow = move.fromRow;
int fromCol = move.fromColumn;
if(fromRow < numRows() && fromRow >= 0) {
if(fromCol < numColumns() && fromCol >= 0) {
if(board[fromRow][fromCol] != null) {
if(board[fromRow][fromCol]
.isValidMove(move, board) == true) {
if(board[fromRow][fromCol]
.player() == this.player) {
moves.add(move);
fromPieces.add(board[fromRow][fromCol]);
toPieces.add(board[move.toRow][move.toColumn]);
setPiece(
move.toRow,
move.toColumn,
board[fromRow][fromCol]
);
board[fromRow][fromCol] = null;
if(!inCheck(currentPlayer())) {
setNextPlayer();
undo();
return true;
}
setNextPlayer();
undo();
return false;
}
}
}
}
}
}
return false;
}
/*************************************************************
* Moves the chesspiece when valid
*
* @param move
*************************************************************/
public void move(Move move) {
if(move != null) {
if(isValidMove(move) == true) {
if(pieceAt(move.fromRow, move.fromColumn) /** White player pawn promotion */
.type().equals("Pawn")) {
ChessPiece pawn = pieceAt(move.fromRow, move.fromColumn);
if(move.toRow == 0
&& pawn.player() == Player.WHITE) {
Queen queen = new Queen(Player.WHITE);
moves.add(move);
fromPieces.add(pawn);
toPieces.add(board[move.toRow][move.toColumn]);
setPiece(0, move.toColumn, queen);
board[move.fromRow][move.fromColumn] = null;
setNextPlayer();
return;
}
if(move.toRow == 7
&& pawn.player() == Player.BLACK) {/** Black player pawn promotion */
Queen queen = new Queen(Player.BLACK);
moves.add(move);
fromPieces.add(pawn);
toPieces.add(board[move.toRow][move.toColumn]);
setPiece(7, move.toColumn, queen);
board[move.fromRow][move.fromColumn] = null;
setNextPlayer();
return;
}
}
/**Adds the move to allow for undos */
moves.add(move);
fromPieces.add(board[move.fromRow][move.fromColumn]);
toPieces.add(board[move.toRow][move.toColumn]);
setPiece(
move.toRow,
move.toColumn,
board[move.fromRow][move.fromColumn]
); /**Move the piece */
board[move.fromRow][move.fromColumn] = null;
setNextPlayer();
return;
}
}
if(move == null) {
throw new NullPointerException();
}
}
/*************************************************************
* Determines if the selected player is in check or not
*
* @param p
* @return boolean
*************************************************************/
public boolean inCheck(Player p) {
if(p != null) {
int toRow = -1;
int toCol = -1;
/** This loop finds the king */
for (int row = 0; row < numRows(); row++) {
for(int col = 0; col < numColumns(); col++) {
if(board[row][col] != null) {
if(board[row][col].type() == "King"
&& board[row][col].player() == p) {
toRow = row;
toCol = col;
}
}
}
}
/** This loop determines if any move is capable of taking the king */
if(toRow != -1 && toCol != -1) {
for(int row = 0; row < numRows(); row++) {
for(int col = 0; col < numColumns(); col++) {
if(board[row][col] != null) {
if(board[row][col].player() != p) {
Move move = new Move(row, col, toRow, toCol);
System.out.println(board[row][col].player());
if(board[move.fromRow][move.fromColumn]
.isValidMove(move, board)) {
System.out.println(p.toString() + " in check");
return true;
}
}
}
}
}
} else {
System.out.println("error");
}
}
System.out.println(p.toString() + " NOT in check");
return false;
}
/*************************************************************
* Returns the current player
*
* @return Player
*************************************************************/
public Player currentPlayer() {
return player;
}
/*************************************************************
* Gives the amount of rows on the chessboard
*
* @return int
*************************************************************/
public int numRows() {
return 8;
}
/*************************************************************
* Gives the amount of columns on the chessboard
*
* @return int
*************************************************************/
public int numColumns() {
return 8;
}
/*************************************************************
* Returns a ChessPiece at an inputted space on the chessboard
*
* @param row
* @param column
* @return ChessPiece
*************************************************************/
public ChessPiece pieceAt(int row, int column) {
return board[row][column];
}
/*************************************************************
* Switches the current player
*************************************************************/
public void setNextPlayer() {
player = player.next();
}
/*************************************************************
* Sets a specific space on the board to a different ChessPiece
*
* @param row
* @param column
* @param piece
*************************************************************/
public void setPiece(int row, int column, ChessPiece piece) {
board[row][column] = piece;
}
/*************************************************************
* Undoes the last move made on the board
*************************************************************/
public void undo() {
if(moves.size() > 0) {
/**If more than one move has been made */
Move move = moves.get(moves.size() - 1);
ChessPiece fromPiece = fromPieces.get(fromPieces.size() - 1);
ChessPiece toPiece = toPieces.get(toPieces.size() - 1);
board[move.fromRow][move.fromColumn] = fromPiece;
board[move.toRow][move.toColumn] = toPiece;
moves.remove(moves.size() - 1);
fromPieces.remove(fromPieces.size() - 1);
toPieces.remove(toPieces.size() - 1);
setNextPlayer();
}
}
/*************************************************************
* Checks to see if any of the AI's pieces are in danger of
* being captured
*************************************************************/
public boolean AIDanger() {
int toRow = -1;
int toCol = -1;
for (int row1 = 0; row1 < numRows(); row1++) {
for(int col1 = 0; col1 < numColumns(); col1++) { /**Find the king */
if(board[row1][col1] != null) {
if(board[row1][col1].type() != "King"
&& board[row1][col1].player() == Player.BLACK) {
toRow = row1;
toCol = col1;
if(toRow != -1 && toCol != -1) {
for(int row = 0; row < numRows(); row++) {
for(int col = 0; col < numColumns(); col++) {
if(board[row][col] != null) {
if(board[row][col].player() != Player.WHITE) {
Move move = new Move(row, col, toRow, toCol);
if(board[move.fromRow][move.fromColumn]
.isValidMove(move, board)) {
System.out.println(Player.WHITE.toString() + " in check by AI");
move(move);
}
}
}
}
}
} else {
System.out.println("error");
}
}
}
}
}
return false;
}
/*************************************************************
* Functionality of an AI opponent
*************************************************************/
public void AI() {
/* TODO: implement this method (manually graded)
* Write a simple AI set of rules in the following order.
* a. Check to see if you are in check.
* i. If so, get out of check by moving the king or placing a piece to block the check
*
* b. Attempt to put opponent into check (or checkmate).
* i. Attempt to put opponent into check without losing your piece
* ii. Perhaps you have won the game.
*
*c. Determine if any of your pieces are in danger,
* i. Move them if you can.
* ii. Attempt to protect that piece.
*
*d. Move a piece (pawns first) forward toward opponent king
* i. check to see if that piece is in danger of being removed, if so, move a different piece.
*/
/** AI == BLACK */
if(currentPlayer() == Player.BLACK) {
if(inCheck(currentPlayer())) {
if(!isComplete()) {
for(int row = 0; row < numRows(); row++) {
for(int col = 0; col < numColumns(); col++) {
if(pieceAt(row, col) != null) {
if(pieceAt(row, col).player() == currentPlayer()) {
for(int toRow = 0; toRow < numRows(); toRow++) {
for(int toCol = 0; toCol < numRows(); toCol++) {
Move move = new Move(row, col, toRow, toCol);
if(isValidMove(move)) {
move(move);
setNextPlayer();
if(inCheck(currentPlayer())) {
System.out.println("AI still in check");
undo();
} else {
System.out.println("AI out of check");
}
}
}
}
}
}
}
}
}
}
if(!inCheck(Player.WHITE)) {/**need to add capture protection */
int toRow = -1;
int toCol = -1;
for (int row = 0; row < numRows(); row++) {
for(int col = 0; col < numColumns(); col++) {
if(board[row][col] != null) {
if(board[row][col].type() == "King" && board[row][col].player() == Player.WHITE) {
toRow = row;
toCol = col;
}
}
}
}
if(toRow != -1 && toCol != -1) {
for(int row = 0; row < numRows(); row++) {
for(int col = 0; col < numColumns(); col++) {
if(board[row][col] != null) {
if(board[row][col].player() != Player.WHITE) {
Move move = new Move(row, col, toRow, toCol);
if(board[move.fromRow][move.fromColumn].isValidMove(move, board)) {
System.out.println(Player.WHITE.toString() + " in check by AI");
move(move);
}
}
}
}
}
} else {
System.out.println("error");
}
} else {
}
}
}
}