-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueen.java
More file actions
266 lines (223 loc) · 9.79 KB
/
Copy pathQueen.java
File metadata and controls
266 lines (223 loc) · 9.79 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
/**
* Queen
*
* @author Faiza Salami, 7941056
* <p>
* REMARKS: A queen class representing a queen piece on a chess board
*/
public class Queen extends Piece {
public Queen(char piece) {
super(piece);
}
/**
* checks if the move being made is valid for a player's queen
*
* @param move the move that was made
* @param gameBoard the current game board
* @return boolean-returns true if the move is valid and false otherwise
*/
public boolean validMove(Move move, Board gameBoard) {
boolean output = false;
boolean noPiecesBetween = true;
Piece newPiece = gameBoard.getPiece(move.getNewRow(), move.getNewCol());
//check they're moving in a straight path
if (move.getRow() == move.getNewRow()) {
for (int i = 1; i <= Math.abs(move.getNewCol() - move.getCol()) - 1; i++) {
if (gameBoard.getPiece(move.getNewRow(), Math.min(move.getCol(), move.getNewCol()) + i) != null) {
noPiecesBetween = false;
}
}
} else if (move.getCol() == move.getNewCol()) {
for (int i = 1; i <= Math.abs(move.getNewRow() - move.getRow()) - 1; i++) {
if (gameBoard.getPiece(Math.min(move.getRow(), move.getNewRow()) + i, move.getNewCol()) != null) {
noPiecesBetween = false;
}
}
} else if (Math.abs(move.getNewCol() - move.getCol()) == Math.abs(move.getNewRow() - move.getRow())) {
for (int i = 1; i <= Math.abs(move.getNewCol() - move.getCol()) - 1; i++) {
if (gameBoard.getPiece(Math.min(move.getRow(), move.getNewRow()) + i, Math.min(move.getCol(), move.getNewCol()) + i) != null) {
noPiecesBetween = false;
}
}
} else {
noPiecesBetween = false;
}
if (noPiecesBetween && (newPiece == null || newPiece.isAIPiece())) {
gameBoard.getPiece(move.getRow(), move.getCol()).setCaptured(newPiece);
output = true;
}
return output;
}
/**
* generate a move for the AI queen, easy mode. this method just moves the queen by one square
*
* @param move the move containing where the queen currently is
* @param gameBoard the current game board
* @return boolean-returns the move containing what location the queen is moving to or null if there's no available
* space for the queen to move to
*/
public Move generateMove(Move move, Board gameBoard) {
int[][] possibleMoves = {{-1, 1}, {-1, -1}, {1, 1}, {1, -1}, {1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int newRow;
int newCol;
Piece newPiece;
for (int i = 0; i < possibleMoves.length; i++) {
newRow = move.getRow() + possibleMoves[i][0];
newCol = move.getCol() + possibleMoves[i][1];
if (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8) {
newPiece = gameBoard.getPiece(newRow, newCol);
if (newPiece == null || newPiece.isPlayerPiece()) {
move.setNewRow(newRow);
move.setNewCol(newCol);
gameBoard.getPiece(move.getRow(), move.getCol()).setCaptured(gameBoard.getPiece(move.getNewRow(), move.getNewCol()));
return move;
}
}
}
return null;
}
/**
* checks if any of the player's piece is in the queen's vicinity to be captured
*
* @param move the move containing the queen's current location
* @param gameBoard the current game board
* @return Move-the move containing what location the queen is moving to or null if the queen cant capture any player
*/
public Move capturePlayerPiece(Move move, Board gameBoard) {
//checks all way the queen can move, horizontally, vertically, diagonally
int newRow = move.getRow() + 1;
int newCol = move.getCol() + 1;
Piece newPiece = null;
while (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8 && gameBoard.getPiece(newRow, newCol) == null) {
newPiece = gameBoard.getPiece(newRow, newCol);
newRow++;
newCol++;
}
if (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8) {
newPiece = gameBoard.getPiece(newRow, newCol);
}
if (newPiece != null && newPiece.isPlayerPiece()) {
move.setNewRow(newRow);
move.setNewCol(newCol);
gameBoard.getPiece(move.getRow(), move.getCol()).setCaptured(gameBoard.getPiece(move.getNewRow(), move.getNewCol()));
return move;
}
newRow = move.getRow() + 1;
newCol = move.getCol() - 1;
while (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8 && gameBoard.getPiece(newRow, newCol) == null) {
newPiece = gameBoard.getPiece(newRow, newCol);
newRow++;
newCol--;
}
if (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8) {
newPiece = gameBoard.getPiece(newRow, newCol);
}
if (newPiece != null && newPiece.isPlayerPiece()) {
move.setNewRow(newRow);
move.setNewCol(newCol);
gameBoard.getPiece(move.getRow(), move.getCol()).setCaptured(gameBoard.getPiece(move.getNewRow(), move.getNewCol()));
return move;
}
newRow = move.getRow() - 1;
newCol = move.getCol() + 1;
while (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8 && gameBoard.getPiece(newRow, newCol) == null) {
newPiece = gameBoard.getPiece(newRow, newCol);
newRow--;
newCol++;
}
if (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8) {
newPiece = gameBoard.getPiece(newRow, newCol);
}
if (newPiece != null && newPiece.isPlayerPiece()) {
move.setNewRow(newRow);
move.setNewCol(newCol);
gameBoard.getPiece(move.getRow(), move.getCol()).setCaptured(gameBoard.getPiece(move.getNewRow(), move.getNewCol()));
return move;
}
newRow = move.getRow() - 1;
newCol = move.getCol() - 1;
while (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8 && gameBoard.getPiece(newRow, newCol) == null) {
newPiece = gameBoard.getPiece(newRow, newCol);
newRow--;
newCol--;
}
if (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8) {
newPiece = gameBoard.getPiece(newRow, newCol);
}
if (newPiece != null && newPiece.isPlayerPiece()) {
move.setNewRow(newRow);
move.setNewCol(newCol);
gameBoard.getPiece(move.getRow(), move.getCol()).setCaptured(gameBoard.getPiece(move.getNewRow(), move.getNewCol()));
return move;
}
newRow = move.getRow() + 1;
newCol = move.getCol();
while (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8 && gameBoard.getPiece(newRow, newCol) == null) {
newPiece = gameBoard.getPiece(newRow, newCol);
newRow++;
}
if (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8) {
newPiece = gameBoard.getPiece(newRow, newCol);
}
if (newPiece != null && newPiece.isPlayerPiece()) {
move.setNewRow(newRow);
move.setNewCol(newCol);
gameBoard.getPiece(move.getRow(), move.getCol()).setCaptured(gameBoard.getPiece(move.getNewRow(), move.getNewCol()));
return move;
}
newRow = move.getRow();
newCol = move.getCol() + 1;
while (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8 && gameBoard.getPiece(newRow, newCol) == null) {
newPiece = gameBoard.getPiece(newRow, newCol);
newCol++;
}
if (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8) {
newPiece = gameBoard.getPiece(newRow, newCol);
}
if (newPiece != null && newPiece.isPlayerPiece()) {
move.setNewRow(newRow);
move.setNewCol(newCol);
gameBoard.getPiece(move.getRow(), move.getCol()).setCaptured(gameBoard.getPiece(move.getNewRow(), move.getNewCol()));
return move;
}
newRow = move.getRow();
newCol = move.getCol() - 1;
while (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8 && gameBoard.getPiece(newRow, newCol) == null) {
newPiece = gameBoard.getPiece(newRow, newCol);
newCol--;
}
if (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8) {
newPiece = gameBoard.getPiece(newRow, newCol);
}
if (newPiece != null && newPiece.isPlayerPiece()) {
move.setNewRow(newRow);
move.setNewCol(newCol);
gameBoard.getPiece(move.getRow(), move.getCol()).setCaptured(gameBoard.getPiece(move.getNewRow(), move.getNewCol()));
return move;
}
newRow = move.getRow() - 1;
newCol = move.getCol();
while (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8 && gameBoard.getPiece(newRow, newCol) == null) {
newPiece = gameBoard.getPiece(newRow, newCol);
newRow--;
}
if (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8) {
newPiece = gameBoard.getPiece(newRow, newCol);
}
if (newPiece != null && newPiece.isPlayerPiece()) {
move.setNewRow(newRow);
move.setNewCol(newCol);
gameBoard.getPiece(move.getRow(), move.getCol()).setCaptured(gameBoard.getPiece(move.getNewRow(), move.getNewCol()));
return move;
}
return null;
}
/**
* the name of the piece
*
* @return returns the piece name
*/
public String name() {
return "Queen";
}
}