Repository navigation
Expand file tree
/
Copy pathChessPiece.java
More file actions
319 lines (280 loc) · 10.9 KB
/
Copy pathChessPiece.java
File metadata and controls
319 lines (280 loc) · 10.9 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
//Peter Schlueter, Creates and defines some properties of a chess piece
//Note: A chess piece is sometimes refered to as just a piece in comments of this code
//Note 2: Although there are implementations of all methods in this class, it is still abstract because I may add other implementations in the future
//Note 3: To the reader, I used comments in methods for both debugging purposes and readability,
//but, and this only applies for comments INSIDE methods, some of them may be outdated.
//Import package java.io.* for the purposes of debugging piece hierarchy through printing what is going on in problem areas
import java.io.*;
/**
*
* A class representing general properties of a Chess Piece
* @author Peter Schlueter
*
*/
public abstract class ChessPiece implements LMove
{
/* These are properties of a chess piece (class/instance fields) */
//Field storing side which a chess piece is on
private ChessGame.Side side;
//Field storing a chess piece label
private String label;
//Field storing a chess piece icon (graphical representation of a chess piece)
private Object icon;
//Field storing the row in which a chess piece is in
private int row;
//Field storing the collumn in which a chess piece is in
private int column;
//Field storing the chess board the chess piece is on
private ChessBoard board;
//Field storing whether or not a piece has moved this game
private boolean hasMoved = false;
//Field storing if the piece should be upgraded
private boolean toBeUpgraded = false;
//Field storing if the piece at an inputted spot should be removed
private boolean toBeRemoved = false;
/* Section containing constructor for a chess piece */
/**
* Creates a chess piece with the given user specifications
* @param side the side in which the chess piece is on
* @param label the chess piece label
* @param icon the chess piece icon
* @param row the row the chess piece is in
* @param column the column the chess piece is in
* @param board the board the chess piece is on
*/
public ChessPiece(ChessGame.Side side, String label, Object icon, int row, int column, ChessBoard board)
{
this.side = side;
this.label = label;
this.icon = icon;
this.row = row;
this.column = column;
this.board = board;
}
/* Section containing getter/setter methods for properties of a chess piece */
/**
* Gets whether or not this piece has moved this game
* @return true if the piece has moved this game, false otherwise
*/
public boolean getHasMoved()
{
return this.hasMoved;
}
/**
* Sets whether or not this piece has moved
* @param hasMoved whether or not this piece has moved
*/
public void setHasMoved(boolean hasMoved)
{
this.hasMoved = hasMoved;
}
/**
* Gets whether or not the piece should be upgraded this turn
* @return true if the piece should be upgraded this turn, false otherwise
*/
public boolean getToBeUpgraded()
{
return this.toBeUpgraded;
}
/**
* Sets whether or not the piece should be upgraded this turn
* @param toBeUpgraded whether or not the piece should be upgraded this turn
*/
public void setToBeUpgraded(boolean toBeUpgraded)
{
this.toBeUpgraded = toBeUpgraded;
}
/**
* Gets whether or not this piece should be removed from the board
* @return whether or not this piece should be removed from the game
*/
public boolean getToBeRemoved()
{
return this.toBeRemoved;
}
/**
* Sets whether or not this piece should be removed from the board
* @param toBeRemoved whether or not this piece should be removed from the board
*/
public void setToBeRemoved(boolean toBeRemoved)
{
this.toBeRemoved = toBeRemoved;
}
/**
* Gets the player side the chess piece is on
* @return side which chess piece is on
*/
public ChessGame.Side getSide()
{
return this.side;
}
/**
* Gets a non-visual representation of a chess piece
* @return label identifying the chess piece
*/
public String getLabel()
{
return this.label;
}
/**
* Gets a visual representation of a chess piece
* @return icon identifying the chess piece
*/
public Object getIcon()
{
return this.icon;
}
/**
* Sets a chess piece's location on the board
* @param row the row which the chess piece is in
* @param column the collumn which the chess piece is in
*/
public void setLocation(int row, int column)
{
this.row = row;
this.column = column;
}
/**
* Gets the row in which a chess piece is in
* @return row which chess piece resides in
*/
public int getRow()
{
return this.row;
}
/**
* Gets the column in which a chess piece is in
* @return column which chess piece resides in
*/
public int getColumn()
{
return this.column;
}
/**
* Gets the chess board which the chess piece is on
* @return chess board which chess piece resides on
*/
public ChessBoard getChessBoard()
{
return this.board;
}
/* isLegalMove is overriden in extended classes. A default implimentation is provided here */
/**
* Determines whether or not the chess piece move is legal
* @param toRow stores row which chess piece is intended to be moved to
* @param toColumn stores column which chess piece is intended to be moved to
* @return true if the piece doesn't go off the board, false if it does or input location is the same as the current one
*/
public boolean isLegalMove(int toRow, int toColumn)
{
//ChessBoard board is the current chessboard
ChessBoard board = getChessBoard();
//If the input location goes off the board, return false
if ((toRow > board.getGameRules().getNumRows()) || (toColumn > board.getGameRules().getNumColumns()) || (toRow < 0) || (toColumn < 0))
{
return false;
}
//A piece cannot move if the location to be moved to is the same as the current location
if ((this.getRow() == toRow) && (this.getColumn() == toColumn))
{
return false;
}
//If all conditions fail to return false, return true
return true;
}
/* This section contains methods that will be implimented in individual chess pieces (abstract methods) */
/**
* Determines whether or not a move is legal given the location to be moved to is unoccupied
* @param row stores the row which the chess piece is intended to be moved
* @param column stores the column which the chess piece is intended to be moved
* @return true if the chess piece can be moved to the given location, false if not
*/
public boolean isLegalNonCaptureMove(int row, int column)
{
//If the move is legal, continue with the method
if (isLegalMove(row, column))
{
//If there is not a piece on the square which the piece is intended to be moved to
//and the two pieces are not of the same side, set the piece's location to the new position and return true
if ((getChessBoard().getPiece(row, column) == null))
{
//ChessPiece piece stores the ChessPiece being moved
ChessPiece piece = getChessBoard().removePiece(getRow(), getColumn());
//If this piece is an instance of a king, check if it is castling
if (piece instanceof KingPiece)
{
//KingPiece castlingCheck stores a KingPiece which will check if the move is castling
KingPiece castlingCheck = (KingPiece)this;
//If the piece is castling, make sure to move the rook before the king
if (castlingCheck.isCastleMove(row, column, castlingCheck) && (castlingCheck.getColumn() < column))
{
//ChessPiece movePiece stores the rook which should be moved for castling
ChessPiece movePiece = getChessBoard().removePiece(row, getChessBoard().getGameRules().getNumColumns() - 1);
getChessBoard().addPiece(movePiece, row, column - 1);
}
if (castlingCheck.isCastleMove(row, column, castlingCheck) && (castlingCheck.getColumn() > column))
{
//ChessPiece movePiece stores the rook which should be moved for castling
ChessPiece movePiece = getChessBoard().removePiece(row, 0);
getChessBoard().addPiece(movePiece, row, column + 1);
}
}
//Sets that the piece at this spot has moved this game
piece.setHasMoved(true);
//Add the piece to the new position on the game board
getChessBoard().addPiece(piece, row, column);
return true;
}
}
return false;
}
/**
* Determines whether or not a move is legal given the location to be moved to is occupied
* @param row stores the row which the chess piece is intended to be moved
* @param column stores the column which the chess piece is intended to be moved
* @return true if the chess piece can be moved to the given location, false if not
*/
public boolean isLegalCaptureMove(int row, int column)
{
//If the move is legal, continue with the method
if (isLegalMove(row, column))
{
//If there is a piece on the square, continue with the method
if (getChessBoard().getPiece(row, column) != null)
{
//If this piece is on the opposing side (the side of the current piece
//(which has not moved yet) is not equal to the side of the piece in the space we want to move to),
//remove the piece at this position and set the piece's location to the new position and return true
if (!((getChessBoard().getPiece(getRow(), getColumn()).getSide()).equals(getChessBoard().getPiece(row, column).getSide())))
{
//Reset toBeRemoved
setToBeRemoved(false);
//First, make the opposing piece null
ChessPiece remove = getChessBoard().getPiece(row, column);
remove = null;
//ChessPiece piece stores the ChessPiece being moved
ChessPiece piece = getChessBoard().removePiece(getRow(), getColumn());
getChessBoard().addPiece(piece, row, column);
//Sets that the piece that just moved has moved this game
piece.setHasMoved(true);
return true;
}
}
}
return false;
}
/**
* Handles necessary processesing after the move is completed
* Note: This method currently does nothing since all end of move processing is done in the methods
* listed in the comments in the body of this method
*/
public void moveDone()
{
//In the current state of the project, all end of move processing is done in the following methods and classes
//1.) isLegalCaptureMove() (from ChessPiece)
//2.) isLegalNonCaptureMove() (from ChessPiece)
//3.) EuropeanChess class (handles setting that the piece has moved this game
//So, at this current time this method is only here because it was listed as necessary in the project directions
//Also, again since this method currently does nothing, I do not think that it is necessary to test it in the testing class
}
}