-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessClient.java
More file actions
270 lines (210 loc) · 6.6 KB
/
Copy pathChessClient.java
File metadata and controls
270 lines (210 loc) · 6.6 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
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
package chai;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.concurrent.Worker;
import javafx.concurrent.Worker.State;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;
import chesspresso.Chess;
import chesspresso.position.Position;
public class ChessClient extends Application {
private static final int PIXELS_PER_SQUARE = 64;
private static final String welcomeMessage =
"Welcome to CS 76 chess. Moves can be made using algebraic notation;"
+ " for example the command c2c3 would move the piece at c2 to c3. \n";
Timeline timeline;
TextField commandField;
TextArea logArea;
BoardView boardView;
ChessGame game;
// RandomMoveSource[] playerMoveSources;
MoveMaker[] moveMaker;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("CS 76 Chess");
game = new ChessGame();
// build the board
boardView = new BoardView(game, PIXELS_PER_SQUARE);
// build the text area for giving log info to user
logArea = new TextArea();
//logArea.setPrefColumnCount(50);
logArea.setPrefRowCount(5);
logArea.setEditable(false);
logArea.setWrapText(true);
log(welcomeMessage);
// build the command entry text field
commandField = new TextField();
// request focus on the command field after the ui is built,
// to get a blinking cursor
Platform.runLater(new Runnable() {
public void run() {
commandField.requestFocus();
}
});
// set up the movemakers for black and white players.
// Movemakers handle getting input from an AI, from the keyboard, or
// from a server, depending on which type is used.
moveMaker = new MoveMaker[2];
moveMaker[Chess.BLACK] = new AIMoveMaker(new MinimaxAI(Chess.BLACK));
//moveMaker[Chess.WHITE] = new AIMoveMaker(new MinimaxAI(Chess.WHITE));
moveMaker[Chess.WHITE] = new TextFieldMoveMaker();
VBox vb = new VBox();
vb.getChildren().addAll(boardView, logArea, commandField);
vb.setSpacing(10);
vb.setPadding(new Insets(10, 10, 10, 10));
// add everything to a root stackpane, and then to the main window
StackPane root = new StackPane();
root.getChildren().add(vb);
primaryStage.setScene(new Scene(root)); //, boardView.getPreferredWidth(), 600));
primaryStage.show();
// sets the game world's game loop (Timeline)
timeline = new Timeline(1.0);
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.getKeyFrames().add(
new KeyFrame(Duration.seconds(.05), new GameHandler()));
timeline.playFromStart();
timeline.playFromStart();
// moveMaker = new AIMoveMaker(new RandomAI());
}
private void log(String logText) {
logArea.appendText(logText + "\n");
}
// As time passes, handle the state of the game
private class GameHandler implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent e) {
// System.out.println("timer fired");
// System.out.println(boardView.ready());
// setting activeMoveSource to null will cause a new one to be
// created:
MoveMaker mover = moveMaker[game.position.getToPlay()];
if (mover.getState() == Worker.State.READY) {
mover.start(game.position);
} else if (mover.getState() == Worker.State.SUCCEEDED
&& boardView.ready()) {
short move = mover.getMove();
boardView.doMove(move);
// check for check
if (game.position.isCheck()) {
if (game.position.getToPlay()==Chess.WHITE) {
log("Check for black.");
}
else {
log("Check for white.");
}
}
// check for endgame
if (game.position.isMate()) {
if (game.position.getToPlay()==Chess.WHITE) {
log("Checkmate. Black wins.");
timeline.stop();
}
else {
log("Checkmate. White wins.");
timeline.stop();
}
}
else if (game.position.isStaleMate()) {
log("Draw game.");
timeline.stop();
}
mover.reset();
}
// System.out.println("activeMoveSource state " +
// activeMoveSource.getState());
// short move =
// playerMoveSources[0].getMove(game.position.toString());
// System.out.println(move);
// boardView.doMove(move);
}
}
private class TextFieldMoveMaker implements MoveMaker,
EventHandler<ActionEvent> {
private Worker.State state;
short move;
public TextFieldMoveMaker() {
this.state = Worker.State.READY;
commandField.setOnAction(this);
move = 0;
}
@Override
public void start(Position position) {
//String[] players = {"WHITE", "BLACK"};
//commandField.setPromptText("Your move," + players[position.getToPlay()] + ".");
}
@Override
public void reset() {
commandField.setText("");
this.state = Worker.State.READY;
}
@Override
public State getState() {
return state;
}
@Override
public short getMove() {
return move;
}
@Override
public void handle(ActionEvent e) {
String text = commandField.getText();
if (text != null & text != "") {
int fromSqi = Chess.strToSqi(text.charAt(0), text.charAt(1));
int toSqi = Chess.strToSqi(text.charAt(2), text.charAt(3));
move = game.findMove(fromSqi, toSqi);
if (game.position.isMate()) {
int winner = 1-game.position.getToPlay();
System.out.println("The winner is player "+winner);
}
this.state = Worker.State.SUCCEEDED;
}
}
}
private class AIMoveMaker implements MoveMaker {
ChessAI ai;
AIMoveTask moveTask;
public AIMoveMaker(ChessAI ai) {
super();
this.ai = ai;
this.moveTask = null;
}
public void start(Position position) {
moveTask = new AIMoveTask(ai, new Position(position));
moveTask.setOnFailed(new EventHandler<WorkerStateEvent>() {
@Override public void handle(WorkerStateEvent event) {
event.getSource().getException().printStackTrace();
}
});
new Thread(moveTask).start();
}
public Worker.State getState() {
// short circuit if moveTask hasn't been initalized
// (threading bug fix by Yu-Han Lyu:)
if (moveTask == null)
return Worker.State.READY;
if (moveTask.getState() == Worker.State.SUCCEEDED)
return Worker.State.SUCCEEDED;
return Worker.State.RUNNING;
}
public short getMove() {
return moveTask.getValue();
}
public void reset() {
this.moveTask = null;
}
}
}