-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPiece.java
More file actions
222 lines (190 loc) · 5.91 KB
/
Copy pathPiece.java
File metadata and controls
222 lines (190 loc) · 5.91 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
package chess;
import java.awt.image.*;
public class Piece {
private int color, type, number;
private boolean white, inPlay;
private String name;
private BufferedImage image;
private int value;
private Square square;
private Piece pinner;
private boolean pinned, pinning;
private boolean moved, checking;
private int moveCounter, attackedSquareCounter;
private Square[] moves;
private Square[] attackedSquares;
public Piece(int color, int type, int number) {
// color, type, number
this.color = color;
this.type = type;
this.number = number;
// white
if (color == 0) {
white = true;
} else {
white = false;
}
// inPlay
if (((type == ChessSet.QUEEN || type == ChessSet.KING) && number == 1)
|| ((type == ChessSet.ROOK || type == ChessSet.BISHOP || type == ChessSet.KNIGHT)
&& (number == 1 || number == 2))
|| (type == ChessSet.PAWN && number < 9)) {
inPlay = true;
} else {
inPlay = false;
}
// name
char colorChar, typeChar;
if (white) {
colorChar = 'w';
} else {
colorChar = 'b';
}
switch (type) {
case ChessSet.QUEEN: typeChar = 'q'; break;
case ChessSet.ROOK: typeChar = 'r'; break;
case ChessSet.BISHOP: typeChar = 'b'; break;
case ChessSet.KNIGHT: typeChar = 'n'; break;
case ChessSet.PAWN: typeChar = 'p'; break;
case ChessSet.KING: typeChar = 'k'; break;
default:
typeChar = ' ';
System.out.println("Error: Piece.Piece()");
}
name = colorChar + "" + typeChar + "" + number;
// image
if (white) {
switch (type) {
case ChessSet.QUEEN: image = ChessSet.whiteQueen; break;
case ChessSet.ROOK: image = ChessSet.whiteRook; break;
case ChessSet.BISHOP: image = ChessSet.whiteBishop; break;
case ChessSet.KNIGHT: image = ChessSet.whiteKnight; break;
case ChessSet.PAWN: image = ChessSet.whitePawn; break;
case ChessSet.KING: image = ChessSet.whiteKing; break;
default:
System.out.println("Error: Piece.Piece()");
}
} else {
switch (type) {
case ChessSet.QUEEN: image = ChessSet.blackQueen; break;
case ChessSet.ROOK: image = ChessSet.blackRook; break;
case ChessSet.BISHOP: image = ChessSet.blackBishop; break;
case ChessSet.KNIGHT: image = ChessSet.blackKnight; break;
case ChessSet.PAWN: image = ChessSet.blackPawn; break;
case ChessSet.KING: image = ChessSet.blackKing; break;
default:
System.out.println("Error: Piece.Piece()");
}
}
// value
switch (type) {
case ChessSet.QUEEN: value = 9; break;
case ChessSet.ROOK: value = 5; break;
case ChessSet.BISHOP: value = 3; break;
case ChessSet.KNIGHT: value = 3; break;
case ChessSet.PAWN: value = 1; break;
case ChessSet.KING: value = 0; break;
default:
value = -1;
System.out.println("Error: Piece.Piece()");
}
// Other
square = null;
pinner = null;
pinned = pinning = false;
moved = checking = false;
moves = new Square[28];
attackedSquares = new Square[28];
moveCounter = attackedSquareCounter = 0;
}
public int getColor() {
return color;
}
public int getType() {
return type;
}
public int getNumber() {
return number;
}
public boolean isWhite() {
return white;
}
public boolean isInPlay() {
return inPlay;
}
public void setInPlay(boolean inPlay) {
this.inPlay = inPlay;
if (inPlay == false) {
if (square != null) {
square.setPiece(null);
square = null;
}
}
}
public String getName() {
return name;
}
public BufferedImage getImage() {
return image;
}
public int getValue() {
return value;
}
public Square getSquare() {
return square;
}
public void setSquare(Square square) {
this.square = square;
}
public Piece getPinner() {
return pinner;
}
public void setPinner(Piece pinner) {
this.pinner = pinner;
}
public boolean isPinned() {
return pinned;
}
public void setPinned (boolean pinned) {
this.pinned = pinned;
}
public boolean isPinning() {
return pinning;
}
public void setPinning(boolean pinning) {
this.pinning = pinning;
}
public boolean hasMoved() {
return moved;
}
public void setMoved() {
moved = true;
}
public boolean isChecking() {
return checking;
}
public void setChecking (boolean checking) {
this.checking = checking;
}
public void resetCounters() {
moveCounter = attackedSquareCounter = 0;
}
public Square[] getMoves() {
return moves;
}
public void addMove(Square move) {
moves[moveCounter++] = move;
}
public void setMoves(Square[] moves) {
this.moves = moves;
}
public Square[] getAttackedSquares() {
return attackedSquares;
}
public void addAttackedSquare(Square attackedSquare) {
attackedSquares[attackedSquareCounter++] = attackedSquare;
}
public void setAttackedSquares(Square[] attackedSquares) {
this.attackedSquares = attackedSquares;
}
}