Repository navigation
Expand file tree
/
Copy pathCastleMove.java
More file actions
183 lines (176 loc) · 7.3 KB
/
Copy pathCastleMove.java
File metadata and controls
183 lines (176 loc) · 7.3 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
//Peter Schlueter, Interface defining a legal castle move
/**
*
* An interface defineing a legal castle move
* @author Peter Schleuter
*
*/
public interface CastleMove
{
/**
* Determines if a king move is a legal castle move
* @param toRow the row which the piece is intended to be moved
* @param toColumn the column which the piece is intended to be moved
* @param piece the piece which is intended to be moved
*/
default boolean isCastleMove(int toRow, int toColumn, ChessPiece piece)
{
//Before the rest of the method, check to see if this is a valid castle move
if ((toRow == piece.getRow()) && (Math.abs(piece.getColumn() - toColumn) == 2))
{
//1.) If the king has moved at all this game, immediately return false
if (piece.getHasMoved() == true)
{
return false;
}
//If the king is on the north side, check the following conditions
if (piece.getSide() == ChessGame.Side.NORTH)
{
//Next, check for if the king is moving east
if (toColumn > piece.getColumn())
{
//ChessPiece rook stores the chess piece in the left corner of the board (from north perspective)
ChessPiece rook = piece.getChessBoard().getPiece(0, piece.getChessBoard().getGameRules().getNumColumns() - 1);
//2.) If piece is not an instance of a rook, return false
if (!(rook instanceof RookPiece))
{
return false;
}
//3.) Given this piece is an instance of a rook, if it is on the opposing side, return false
if (!(piece.getSide().equals(rook.getSide())))
{
return false;
}
//4.) If the rook piece has moved before, return false
if (rook.getHasMoved() == true)
{
return false;
}
//5.)
//Loop-Goal: Determine if there are any pieces between the input piece and the rook or any squares between the two are threatened,
//and if so, return false
for (int c = rook.getColumn() - 1; c > piece.getColumn(); c--)
{
if ((piece.getChessBoard().getPiece(piece.getRow(), c) != null) ||
(piece.getChessBoard().squareThreatened(piece.getRow(), c, piece) == true))
{
return false;
}
}
//6.) If none of these return false, a castle move is legal for this side and direction and move the rook to its new position
return true;
}
//Check the following conditions if the king is moving west (on side north)
if (toColumn < piece.getColumn())
{
//ChessPiece rook stores the chesspiece in the right corner of the board (from north's perspective)
ChessPiece rook = piece.getChessBoard().getPiece(0, 0);
//2.) If piece is not an instance of a rook, return false
if (!(rook instanceof RookPiece))
{
return false;
}
//3.) Given this piece is an instance of a rook, if it is on the opposing side, return false
if (!(piece.getSide().equals(rook.getSide())))
{
return false;
}
//4.) If the rook piece has moved before, return false
if (rook.getHasMoved() == true)
{
return false;
}
//5.)
//Loop-Goal: Determine if there are any pieces between the input piece and the rook or any squares between the two are threatened,
//and if there are, return false
for (int c = rook.getColumn() + 1; c < piece.getColumn(); c++)
{
if ((piece.getChessBoard().getPiece(piece.getRow(), c) != null) ||
(piece.getChessBoard().squareThreatened(piece.getRow(), c, piece) == true))
{
return false;
}
}
//6.) If all of these conditions fail, this castle move is legal for this side and direction
//Set the new location for the rook at
return true;
}
}
//If the king is on the south side, check the following conditions
if (piece.getSide() == ChessGame.Side.SOUTH)
{
//Next, check for if the king is moving east
if (toColumn > piece.getColumn())
{
//ChessPiece rook stores the chess piece in the right corner of the board (from north)
ChessPiece rook = piece.getChessBoard().getPiece(piece.getChessBoard().getGameRules().getNumRows() - 1, piece.getChessBoard().getGameRules().getNumColumns() - 1);
//2.) If piece is not an instance of a rook, return false
if (!(rook instanceof RookPiece))
{
return false;
}
//3.) Given this piece is an instance of a rook, if it is on the opposing side, return false
if (!(piece.getSide().equals(rook.getSide())))
{
return false;
}
//4.) If the rook piece has moved before, return false
if (rook.getHasMoved() == true)
{
return false;
}
//5.)
//Loop-Goal: Determine if there are any pieces between the input piece and the rook or any squares between the two are threatened,
//and if so, return false
for (int c = rook.getColumn() - 1; c > piece.getColumn(); c--)
{
if ((piece.getChessBoard().getPiece(piece.getRow(), c) != null) ||
(piece.getChessBoard().squareThreatened(piece.getRow(), c, piece) == true))
{
return false;
}
}
//6.) If none of these return false, a castle move is legal for this side and direction
return true;
}
//Check if the piece is moving west
if (toColumn < piece.getColumn())
{
//ChessPiece rook stores the chesspiece in the left corner of the board (from north's perspective)
ChessPiece rook = piece.getChessBoard().getPiece(piece.getChessBoard().getGameRules().getNumRows() - 1, 0);
//2.) If piece is not an instance of a rook, return false
if (!(rook instanceof RookPiece))
{
return false;
}
//3.) Given this piece is an instance of a rook, if it is on the opposing side, return false
if (!(piece.getSide().equals(rook.getSide())))
{
return false;
}
//4.) If the rook piece has moved before, return false
if (rook.getHasMoved() == true)
{
return false;
}
//5.)
//Loop-Goal: Determine if there are any pieces between the input piece and the rook or any squares between the two are threatened,
//and if there are, return false
for (int c = rook.getColumn() + 1; c < piece.getColumn(); c++)
{
if ((piece.getChessBoard().getPiece(piece.getRow(), c) != null) ||
(piece.getChessBoard().squareThreatened(piece.getRow(), c, piece) == true))
{
return false;
}
}
//6.) If all of these conditions fail, this castle move is legal for this side and direction
return true;
}
}
//Return statements are in the if statement blocks
}
//If the initial conditions fails, return false
return false;
}
}