-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPiece.java
More file actions
101 lines (85 loc) · 3.1 KB
/
Piece.java
File metadata and controls
101 lines (85 loc) · 3.1 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
import java.util.HashSet;
import javax.swing.JLabel;
//Piece is the superclass of each respective piece on the chess board, and is never itself instantiated
@SuppressWarnings("serial")
public class Piece extends JLabel{
protected Shade color;
protected Coord coords;
//A set containing the possible move vectors along which a piece can legally travel
protected boolean alive = true;
protected Chessboard c;
protected HashSet<Coord> attack;
//A method that returns the color of the piece in question
public Shade getShade(){
return color;
}
/* A method that returns the present numerical coordinates of the piece on
* the board. The return type is a Coord object.
*/
public Coord getCoord(){
return coords;
}
//A method for updating the coordinates of a piece
public void put(Coord cd){
coords = cd;
}
//A method that returns whether or not the piece is alive and still in play.
public boolean isAlive(){
return alive;
}
/* A method that, given a new coordinate for the piece, checks to see whether
* said new coordinate is a valid move. If the move is valid, the method
* returns true. Otherwise, it will return false.
*
* Valid moves involve placing a piece on a square not occupied by a piece
* of the same color. For rooks, queens, bishops, and sometimes pawns, this
* also means that no intervening squares between the initial and final
* squares may be occupied. For kings, the square in question must also
* not be under attack by any opposing pieces.
*/
public boolean validMove(Coord c){
//Stub to be overridden
return false;
}
//A method that kills the piece in question
public void kill(){
alive = false;
c.getSquare(coords).clear();
}
//A method for reviving pieces after the checks that occur in the main event loop
public void revive(){
alive = true;
c.getSquare(coords).place(this);
}
/*Constructor assigns color, start coordinates, and the chessboard context.
*It also attempts to place the piece, and raises an exception if the piece
*cannot be placed
*/
public Piece(Shade clr, int[] startCoords, Chessboard cb,String label) throws IllegalArgumentException{
super(clr + label);
color = clr;
coords = new Coord(startCoords[0],startCoords[1]);
c = cb;
if(!(c.getSquare(this.coords).place(this)))
throw new IllegalArgumentException("Piece " + this.toString() + " could not be placed.");
attack = new HashSet<Coord>();
/*try {
img = ImageIO.read(new File("strawberry.jpg"));
} catch (IOException e) {
}*/
}
//A method that returns a hashset containing all of the squares that a given piece is attacking
public HashSet<Coord> getAttack(){
return new HashSet<Coord>();
}
//A redundant method only in place to be overridden by the Pawn, whose moves are different than the squares it attacks
public HashSet<Coord> getMoves(){
return this.getAttack();
}
//A helper method for determining whether or not a given piece trajectory has left the board
protected boolean inRange(Coord cd){
if (cd.getFile() < 8 && cd.getFile() >= 0 &&
cd.getRank() < 8 && cd.getRank() >= 0) return true;
else return false;
}
}