forked from TharinduThilakshana/CloudBot_v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BoardView.java
273 lines (201 loc) · 6.83 KB
/
BoardView.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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package chai;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javafx.animation.FadeTransition;
import javafx.animation.Timeline;
import javafx.animation.TranslateTransition;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.util.Duration;
import chesspresso.Chess;
import chesspresso.move.Move;
public class BoardView extends Group {
// the size of the font used to draw pieces,
// relative to the size of the square
private final static double FONT_SCALE = .75;
private int pixelsPerSquare;
private ChessGame game;
private int numCurrentAnimations;
private Label[] pieceLabels; // an array of Pieces sorted by square index
private static final Map<Short, String> unicodePiece;
static {
Map<Short, String> aMap = new HashMap<Short, String>();
aMap.put(Chess.WHITE_KING, "\u2654");
aMap.put(Chess.WHITE_QUEEN, "\u2655");
aMap.put(Chess.WHITE_ROOK, "\u2656");
aMap.put(Chess.WHITE_BISHOP, "\u2657");
aMap.put(Chess.WHITE_KNIGHT, "\u2658");
aMap.put(Chess.WHITE_PAWN, "\u2659");
aMap.put(Chess.BLACK_KING, "\u265A");
aMap.put(Chess.BLACK_QUEEN, "\u265B");
aMap.put(Chess.BLACK_ROOK, "\u265C");
aMap.put(Chess.BLACK_BISHOP, "\u265D");
aMap.put(Chess.BLACK_KNIGHT, "\u265E");
aMap.put(Chess.BLACK_PAWN, "\u265F");
unicodePiece = Collections.unmodifiableMap(aMap);
}
public BoardView(ChessGame game, int pixelsPerSquare) {
pieceLabels = new Label[64];
this.game = game;
this.pixelsPerSquare = pixelsPerSquare;
Color colors[] = { Color.LIGHTGRAY, Color.WHITE };
int color_index = 0; // alternating index to select tile color
for (int r = 0; r < game.rows; r++) {
for (int c = 0; c < game.columns; c++) {
int x = c * pixelsPerSquare;
int y = (game.rows - r - 1) * pixelsPerSquare;
Rectangle square = new Rectangle(x, y, pixelsPerSquare,
pixelsPerSquare);
square.setFill(colors[color_index]);
Text t = new Text(x, y + 12, "" + Chess.colToChar(c)
+ Chess.rowToChar(r));
this.getChildren().add(square);
this.getChildren().add(t);
// switch colors
color_index = (color_index + 1) % 2;
}
// switch color back for a new row
color_index = (color_index + 1) % 2;
}
// add pieces
//int pixelsPerPiece = (pixelsPerSquare * 3) / 4;
for (int r = 0; r < game.rows; r++) {
for (int c = 0; c < game.columns; c++) {
short stone = (short) game.getStone(c, r);
if (stone != Chess.NO_STONE) {
int x = c * pixelsPerSquare;
int y = (game.rows - r - 1) * pixelsPerSquare;
int sqi = Chess.coorToSqi(c, r);
// System.out.println(sqi);
pieceLabels[sqi] = new Label(unicodePiece.get(stone));
pieceLabels[sqi].setTranslateX(x);
pieceLabels[sqi].setTranslateY(y);
pieceLabels[sqi].setMinWidth(pixelsPerSquare);
pieceLabels[sqi].setPrefWidth(pixelsPerSquare);
pieceLabels[sqi].setMaxWidth(pixelsPerSquare);
pieceLabels[sqi].setFont(Font.font("Verdana",
(int) (pixelsPerSquare * FONT_SCALE)));
pieceLabels[sqi].setAlignment(Pos.CENTER);
this.getChildren().add(pieceLabels[sqi]);
}
// System.out.println(unicodePiece.get(stone));
// System.out.print(stone);
}
// System.out.println();
}
numCurrentAnimations = 0 ;
}
public boolean doMove(short move) {
// Castling bugs fixed:
// rook could not move after castle. Also,
// long castles animated improperly.
// Bugs found by Andrew Meier and Jon Preddy.
// Fix below by Jon Preddy (Jan 10, 2014).
// bail out if the move isn't legal
if (move == 0) {
System.out.println("Illegal move attempted.");
return false;
}
int fromSqi = Move.getFromSqi(move);
int toSqi = Move.getToSqi(move);
int r1 = Chess.sqiToRow(fromSqi);
int c1 = Chess.sqiToCol(fromSqi);
int r2 = Chess.sqiToRow(toSqi);
int c2 = Chess.sqiToCol(toSqi);
// System.out.println(fromsqi + " " + tosqi);
//System.out.println("rc " + r1 + " " + c1);
Label l = pieceLabels[fromSqi];
if (Move.isPromotion(move)) {
int piece = Move.getPromotionPiece(move);
//this is what the pawn to be promoted to
short stone = (short)Chess.pieceToStone(piece,
this.game.position.getToPlay());
l.setText(unicodePiece.get(stone));
}
// animate captured piece (regular)
if (Move.isCapturing(move)) {
animateCapture(pieceLabels[toSqi]);
//this.getChildren().removeAll(pieceLabels[toSqi]);
}
// animate captured En Passant piece
if (Move.isEPMove(move)) {
// capture column is c2, capture row is r1
int captureSqi = Chess.coorToSqi(c2, r1);
animateCapture(pieceLabels[captureSqi]);
}
// animate castle moves
if(Move.isShortCastle(move) || Move.isLongCastle(move)) {
int dx = -2; // short castle
int rookx = 7;
// NEW CODE
int toRookSqi = 5;
int fromRookSqi = 7;
if(Move.isLongCastle(move)) {
// NEW CODE
dx = 3;
rookx = 0;
// NEW CODE
toRookSqi = 3;
fromRookSqi = 0;
}
Label rook = pieceLabels[Chess.coorToSqi(rookx, r1)];
animateMove(rook, dx, 0);
// NEW CODE
pieceLabels[toRookSqi] = pieceLabels[fromRookSqi];
pieceLabels[fromRookSqi] = null;
}
// update the list of piece Labels
pieceLabels[toSqi] = pieceLabels[fromSqi];
pieceLabels[fromSqi] = null;
//System.out.println(l.getText());
Timeline timeline = new Timeline();
if (timeline != null) {
timeline.stop();
}
animateMove(l, c2 - c1, r2 - r1);
this.game.doMove(move);
return true;
}
public boolean doMove(int fromSqi, int toSqi) {
short move = game.findMove(fromSqi, toSqi);
return doMove(move);
}
// move the piece n by dx, dy cells
private void animateMove(Node n, int dx, int dy) {
numCurrentAnimations++;
TranslateTransition tt = new TranslateTransition(Duration.millis(1000),
n);
tt.setByX(pixelsPerSquare * dx);
tt.setByY(-pixelsPerSquare * dy);
tt.setCycleCount(1);
tt.setOnFinished(new animationFinished());
tt.play();
}
private void animateCapture(Node n) {
numCurrentAnimations++;
FadeTransition t = new FadeTransition(Duration.millis(1000), n);
t.setFromValue(1.0);
t.setToValue(0.0);
t.setOnFinished(new animationFinished());
t.play();
}
private class animationFinished implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent event) {
numCurrentAnimations--;
}
}
// BoardView is "ready" if not currently animating
public boolean ready() {
return numCurrentAnimations == 0;
}
}