-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPiece.java
More file actions
250 lines (229 loc) · 6.49 KB
/
Copy pathPiece.java
File metadata and controls
250 lines (229 loc) · 6.49 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
import java.awt.*;
import java.util.*;
/**
* Represents
* a piece on the chess
* board.
*
* @author Linda Zeng
* @version 4.9.23
*/
public abstract class Piece
{
//the board this piece is on
private Board board;
//the location of this piece on the board
private Location location;
//the color of the piece
private Color color;
//the file used to display this piece
private String imageFileName;
//the approximate value of this piece in a game of chess
private int value;
/**
* Constructs a new Piece with the given color,
* value, and file name (image to be displayed).
*
* @param col color of piece
* @param fileName name of file with image of piece
* @param val value of piece in game
*/
public Piece(Color col, String fileName, int val)
{
color = col;
imageFileName = fileName;
value = val;
}
/**
* returns the board this piece is on
*
* @return the board of the piece
*/
public Board getBoard()
{
return board;
}
/**
* returns the location of this piece on the board
*
* @return location of piece on board
*/
public Location getLocation()
{
return location;
}
/**
* returns the color of this piece
*
* @return color of this pliece
*/
public Color getColor()
{
return color;
}
/**
* returns the name of the file used to display this piece
*
* @return file name of piece
*/
public String getImageFileName()
{
return imageFileName;
}
/**
* returns a number representing the relative value of this piece
*
* @return value of piece
*/
public int getValue()
{
return value;
}
/**
* Puts this piece into a board. If there is another piece at the given
* location, it is removed.
* @Precondition: (1) This piece is not contained in a grid (2)
* loc is valid in gr
* @param brd the board into which this piece should be placed
* @param loc the location into which the piece should be placed
*/
public void putSelfInGrid(Board brd, Location loc)
{
if (board != null)
throw new IllegalStateException(
"This piece is already contained in a board.");
Piece piece = brd.get(loc);
if (piece != null)
piece.removeSelfFromGrid();
brd.put(loc, this);
board = brd;
location = loc;
}
/**
* Removes this piece from its board.
* @Precondition: This piece is contained in a board
*/
public void removeSelfFromGrid()
{
if (board == null)
throw new IllegalStateException(
"This piece is not contained in a board.");
if (board.get(location) != this)
throw new IllegalStateException(
"The board contains a different piece at location "
+ location + ".");
board.remove(location);
board = null;
location = null;
}
/**
* Moves this piece to a new location. If there is another piece at the
* given location, it is removed.
* @Precondition: (1) This piece is contained in a grid (2)
* newLocation is valid in the grid of this piece
* @param newLocation the new location
*/
public void moveTo(Location newLocation)
{
if (board == null)
throw new IllegalStateException("This piece is not on a board.");
if (board.get(location) != this)
throw new IllegalStateException(
"The board contains a different piece at location "
+ location + ".");
if (!board.isValid(newLocation))
throw new IllegalArgumentException("Location " + newLocation
+ " is not valid.");
if (newLocation.equals(location))
return;
board.remove(location);
Piece other = board.get(newLocation);
if (other != null)
other.removeSelfFromGrid();
location = newLocation;
board.put(location, this);
}
/**
* Returns true if
* the given location
* is valid, and
* either empty or
* occupied by a Piece of a
* different color.
*
* @param dest the destination to be checked
* @return true if the dest
* is valid and either
* empty or occupied
* by a piece of diff color,
* false otherwise
*/
public boolean isValidDestination(Location dest)
{
if (board.isValid(dest))
{
return (board.get(dest) == null || board.get(dest).getColor() != color);
}
return false;
}
/**
* Indicates which locations
* this piece can move to.
*
* @return an ArrayList of
* locations this can
* move to
*/
public abstract ArrayList<Location> destinations();
/**
* Adds to a
* given list all the locations
* that are in
* a given direction from
* this piece until
* it reaches
* another piece or the edge of
* the board.
* If the piece reached is of opposing
* color, its location is also added
* to the list.
*
* @param dests the list of locations
* to add to
* @param direction the direction to check
* locations
*/
public void sweep(ArrayList<Location> dests, int direction)
{
Location dest = getLocation().getAdjacentLocation(direction);
while (board.isValid(dest) && board.get(dest) == null)
{
dests.add(dest);
dest = dest.getAdjacentLocation(direction);
}
if (board.isValid(dest) && board.get(dest).getColor() != color)
{
dests.add(dest);
}
}
/**
* Checks if two
* objects are equal.
*
* @param o other object to be checked
* @return true if the objects are
* equal, false otherwise
*/
/*public boolean equals(Object o)
{
if (o instanceof Piece)
{
Piece p = (Piece)o;
return (p.getColor().equals(getColor()) && p.getValue() == getValue() && p.getImageFileName().equals(p.getImageFileName()));
}
else
{
return false;
}
}*/
}