-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.java
More file actions
239 lines (228 loc) · 4.98 KB
/
Copy pathBoard.java
File metadata and controls
239 lines (228 loc) · 4.98 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
import java.awt.*;
import java.util.*;
/**
* Represents a rectangular game board, containing Piece objects.
*
* @author Linda Zeng
* @version 4.9.23
*/
public class Board extends BoundedGrid<Piece>
{
/**
* Constructs a new Board with
* the standard dimensions
* 8 by 8.
*/
public Board()
{
super(8, 8);
}
/**
* Undos a given move.
*
* @precondition: move has already been made on the board
* @postcondition: piece has moved back to its source,
* and any captured piece is returned to its location
* @param move move to be undoed
*/
public void undoMove(Move move)
{
Piece piece = move.getPiece();
Location source = move.getSource();
Location dest = move.getDestination();
Piece victim = move.getVictim();
piece.moveTo(source);
if (victim != null)
victim.putSelfInGrid(piece.getBoard(), dest);
}
/**
* Returns a list
* of all possible moves
* for pieces of
* a given color.
*
* @param color the color
* of the pieces
* to be checked
* @return an ArrayList of
* all possible moves
*/
public ArrayList<Move> allMoves(Color color)
{
ArrayList<Move> res = new ArrayList<Move>();
ArrayList<Location> locs = getOccupiedLocations();
for (int i = 0; i < locs.size(); i++)
{
if (get(locs.get(i)).getColor() == color)
{
ArrayList<Location> dests = get(locs.get(i)).destinations();
for (int j = 0; j < dests.size(); j++)
{
res.add(new Move(get(locs.get(i)), dests.get(j)));
}
}
}
return res;
}
/**
* Executes a given move
* by removing and replacing
* a piece in a different
* location on the board.
*
* @param move the move to be executed
*/
public void executeMove(Move move)
{
move.getPiece().removeSelfFromGrid();
/*if (toBePromoted(move))
{
if (move.getPiece().getColor() == Color.WHITE)
{
Piece queen = new Queen(Color.WHITE, "white_queen.gif");
queen.putSelfInGrid(this, move.getDestination());
}
else
{
Piece queen = new Queen(Color.BLACK, "black_queen.gif");
queen.putSelfInGrid(this, move.getDestination());
}
}*/
move.getPiece().putSelfInGrid(this, move.getDestination());
}
/**
* Checks if the king
* in a given color is still
* on the board
*
* @param p the color to be checked
* @return true if the king is
* still there, false otherwise
*/
public boolean isKingInGrid(Color c)
{
ArrayList<Location> res = getOccupiedLocations();
for (Location r: res)
{
if (get(r).getColor().equals(c) && get(r).getValue() == 1000)
{
return true;
}
}
return false;
}
/**
* Checks if there
* are any pieces
* of a given color
* left on the board
*
* @param color color of pieces left
* to be checked
* @return true if there are still
* pieces of that color,
* false otherwise
*/
public boolean anyPiecesInGrid(Color color)
{
ArrayList<Location> res = getOccupiedLocations();
for (Location r: res)
{
if (get(r).getColor() == color)
{
return true;
}
}
return false;
}
/**
* Returns true
* if the given move
* is of a pawn that is
* to be promoted.
*
* @param move the move to be checked
* @return true if the move's piece
* can be promoted; false otherwise
*
private boolean toBePromoted(Move move)
{
Piece p = move.getPiece();
if (p instanceof Pawn)
{
if ((p.getColor() == Color.WHITE && move.getDestination().getRow() == 0) || ((p.getColor() == Color.BLACK && move.getDestination().getRow() == 7)))
{
return true;
}
}
return false;
}*/
/*//precondition: king must be on the board
public boolean inCheck(Color color)
{
Location kingLoc = null;
for (Location l: getOccupiedLocations())
{
if (get(l) instanceof King && get(l).getColor() == color)
{
kingLoc = l;
}
}
if (color == Color.BLACK)
{
ArrayList<Move> moves = allMovesHelper(Color.WHITE);
for (Move m: moves)
{
if (m.getDestination().equals(kingLoc))
{
return true;
}
}
return false;
}
else
{
ArrayList<Move> moves = allMovesHelper(Color.BLACK);
for (Move m: moves)
{
if (m.getDestination().equals(kingLoc))
{
return true;
}
}
return false;
}
}*/
/**
* Returns a list
* of all legal moves
* for pieces of
* a given color.
* When in check,
* only the king can move
* to locations
* where it won't die.
*
* @param color the color
* of the pieces
* to be checked
* @return an ArrayList of
* all possible moves
*
public ArrayList<Move> allMoves(Color color)
{
ArrayList<Move> res = allMovesHelper(color);
if (inCheck(color))
{
for (int i = 0; i < res.size(); i++)
{
if (!(res.get(i).getPiece() instanceof King))
{
res.remove(i);
i--;
}
}
}
return res;
}*/
}