-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKing.java
206 lines (192 loc) · 5.66 KB
/
King.java
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
/**
A class that represents the chess piece King.
@author Dyland Xue
@version 2011-05-18
*/
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
public class King implements Piece
{
private int color;
private ImageIcon ico;
private String name;
private ChessBoard myBoard;
/**
default constructor
@param c color of this piece
*/
public King(int c)
{
color = c;
if(color == ChessBoard.WHITE)
{
name = "K";
ico = new ImageIcon(Toolkit.getDefaultToolkit().getImage("white_king.png"));
}
else if(color == ChessBoard.BLACK)
{
name = "k";
ico = new ImageIcon(Toolkit.getDefaultToolkit().getImage("black_king.png"));
}
}
/**
constructor with reference to a chessboard
@param c color of this piece
@param cb reference to a chessboard
*/
public King(int c, ChessBoard cb)
{
this(c);
myBoard = cb;
}
/**
checks if the move from c1 to c2 is possible for this piece
@param c1 starting coord
@param c2 ending coord
@return true if possible, otherwise false
*/
public boolean soFarSoGood(Coord c1, Coord c2)
{
int ri = c1.getRow();
int ci = c1.getCol();
int rt = c2.getRow();
int ct = c2.getCol();
if(myBoard.get(c2) != null && myBoard.get(c2).getColor() == color)
//checks if the target square is occupied by a piece of its own color
{
return false;
}
//white castle kingside
else if(color == ChessBoard.WHITE &&
c1.equals(new Coord(8, 5)) &&
c2.equals(new Coord(8, 7)) &&
myBoard.get(new Coord(8, 6)) == null &&
myBoard.get(new Coord(8, 7)) == null &&
myBoard.get(new Coord(8, 8)).getColor() == ChessBoard.WHITE &&
myBoard.get(new Coord(8, 8)) instanceof Rook)
{
myBoard.getJudge().special_castleKingside = true;
return true;
}
//black castle kingside
else if(color == ChessBoard.BLACK &&
c1.equals(new Coord(1, 5)) &&
c2.equals(new Coord(1, 7)) &&
myBoard.get(new Coord(1, 6)) == null &&
myBoard.get(new Coord(1, 7)) == null &&
myBoard.get(new Coord(1, 8)).getColor() == ChessBoard.BLACK &&
myBoard.get(new Coord(1, 8)) instanceof Rook)
{
myBoard.getJudge().special_castleKingside = true;
return true;
}
//white castle queenside
else if(color == ChessBoard.WHITE &&
c1.equals(new Coord(8, 5)) &&
c2.equals(new Coord(8, 3)) &&
myBoard.get(new Coord(8, 4)) == null &&
myBoard.get(new Coord(8, 3)) == null &&
myBoard.get(new Coord(8, 2)) == null &&
myBoard.get(new Coord(8, 1)).getColor() == ChessBoard.WHITE &&
myBoard.get(new Coord(8, 1)) instanceof Rook)
{
myBoard.getJudge().special_castleQueenside = true;
return true;
}
//black castle queenside
else if(color == ChessBoard.BLACK &&
c1.equals(new Coord(1, 5)) &&
c2.equals(new Coord(1, 3)) &&
myBoard.get(new Coord(1, 4)) == null &&
myBoard.get(new Coord(1, 3)) == null &&
myBoard.get(new Coord(1, 2)) == null &&
myBoard.get(new Coord(1, 1)).getColor() == ChessBoard.BLACK &&
myBoard.get(new Coord(1, 1)) instanceof Rook)
{
myBoard.getJudge().special_castleQueenside = true;
return true;
}
else
{
double difC = Math.abs((double)(ci - ct));
double difR = Math.abs((double)(ri - rt));
return (difC < 2 && difR < 2);
}
}
/**
checks if the move from c1 to c2 satisfies chess rules or not
@param c1 starting coord
@param c2 ending coord
@return true if move is valid, otherwise false
*/
public boolean moveIsValid(Coord c1, Coord c2)
{
if(!soFarSoGood(c1, c2))
{
return false;
}
else
{
Piece temp = myBoard.get(c2);
myBoard.set(c2, this);
myBoard.set(c1, null);
myBoard.next();
if(myBoard.getJudge().isCheck())
{
myBoard.set(c2, temp);
myBoard.set(c1, this);
myBoard.next();
myBoard.getJudge().check = false;
return false;
}
else
{
myBoard.set(c2, temp);
myBoard.set(c1, this);
myBoard.next();
return true;
}
}
}
/**
sets a chessboard reference
@param cb the chessboard this piece is in
*/
public void setBoard(ChessBoard cb)
{
myBoard = cb;
}
/**
gets the color of this piece, either white or black
@return myColor
*/
public int getColor()
{
return color;
}
/**
gets the initial of this piece, used in notation
@return name
*/
public String getInitial()
{
return "K";
}
/**
gets the icon of this piece, used to represent itself in the GUI
@return ico
*/
public ImageIcon getIcon()
{
return ico;
}
/**
gets the value of this piece
@return VALUE_K
*/
public double getVal()
{
return XMQ.VALUE_K;
}
}