-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSquare.java
More file actions
124 lines (103 loc) · 3.4 KB
/
Square.java
File metadata and controls
124 lines (103 loc) · 3.4 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
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JPanel;
//Squares are objects that contain
@SuppressWarnings("serial")
public class Square extends JPanel{
private Piece occupant;
private Shade clr;
private Coord location;
private boolean underBlackAttack;
private boolean underWhiteAttack;
/* The constructor verifies there is no piece occupying the square, sets the
* color of the square, and initializes the square as not under attack,
* though this will likely change when the pieces are placed.
*/
public Square(Shade shade, Coord place){
location = place;
occupant = null;
clr = shade;
underBlackAttack = false;
underWhiteAttack = false;
if(clr == Shade.BLACK) setBackground(Color.gray);
else setBackground(Color.white);
setPreferredSize(new Dimension(75,75));
}
//Method that returns the color of the square for the purpose of drawing
public Shade getShade(){
return clr;
}
/* Returns true if the square is empty, or if there is an occupant of the
* square not of the same color as the inquiring piece, allowing for a take
*/
public boolean canMove(Shade inquirer){
if (occupant == null || occupant.getShade() != inquirer) return true;
else return false;
}
/* A method that determines whether or not a square is occupied, preventing
* rooks, bishops, queens, and pawns from hopping over squares with opposing
* pieces.
*/
public boolean isOccupied(){
if (occupant instanceof PassantShadow) return false;
else if (!(occupant == null)) return true;
else return false;
}
public boolean hasPassant(){
if(occupant == null) return false;
else if (occupant instanceof PassantShadow) return true;
else return false;
}
public Piece getOccupant(){
return occupant;
}
public Coord getCoords(){
return location;
}
/* Attempts to move a piece to the square, but fails if the square is
* occupied by any piece. The main event loop of the program will clear
* the square before a piece is placed. This method involves the change
* of state of both the piece and the square.
*/
public boolean place(Piece p){
if (occupant == null || occupant instanceof PassantShadow){
occupant = p;
p.put(this.location);
return true;
}
else return false;
}
/* The method used to clear a square after a piece is moved from it or
* before a piece is moved to it once the piece occupying the square has
* been taken.
*/
public void clear(){
occupant = null;
}
/* Returns the color of the occupying piece, unsure if needed yet.
*/
public Shade occupantShade(){
if (occupant == null) return null;
else return occupant.getShade();
}
//A pair of methods for updating the state, to be used in conjunction with a yet-to-
//be-written function evaluating which will determine whether a square is under attack
public void setBlackAttack(boolean value){
underBlackAttack = value;
}
public void setWhiteAttack(boolean value){
underWhiteAttack = value;
}
/* A method that, given the color of the inquiring king, will return whether
*or not the opposite color is attacking the square. Necessary for
*determining the validity of king moves and castling.
*/
public boolean isAttacked(Shade king){
if (king == Shade.BLACK) return underWhiteAttack;
else return underBlackAttack;
}
public String toString(){
String returnable = "(" + location.getFile() + "," + location.getRank() + ")";
return returnable;
}
}