-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUserInterface.java
451 lines (406 loc) · 16 KB
/
UserInterface.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.RowConstraints;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.io.FileNotFoundException;
import java.util.ArrayList;
public class UserInterface implements UserInterfaceAPI {
BorderPane mainPane;
GridPane boardPane;
TextArea infoArea;
TextField commandField;
Button[][] displaySquares = new Button[Board.BOARD_SIZE][Board.BOARD_SIZE];
Scrabble scrabble;
boolean gameOver;
boolean opponentMadePlay;
Bots bots;
String latestInfo;
StringBuilder allInfo;
int gameOverDelayCount;
UserInterface(Scrabble scrabble) throws FileNotFoundException {
this.scrabble = scrabble;
gameOver = false;
opponentMadePlay = false;
latestInfo = "";
allInfo = new StringBuilder();
gameOverDelayCount = 0;
}
public void setBots(Bots bots) {
this.bots = bots;
}
// Stage display methods
public void displayStage(Stage primaryStage) throws InterruptedException {
primaryStage.setTitle("Scrabble");
mainPane = new BorderPane();
boardPane = new GridPane();
infoArea = new TextArea();
commandField = new TextField();
infoArea.setPrefRowCount(10);
infoArea.setPrefColumnCount(15);
infoArea.setWrapText(true);
commandField.setPromptText("Enter command...");
commandField.setPrefColumnCount(15);
Timeline timeline = new Timeline(
new KeyFrame(
Duration.seconds(Main.BOT_DELAY),
event -> {
if (!gameOver) {
String input = bots.getBot(scrabble.getCurrentPlayerId()).getCommand();
printLine("> " + input);
processInput(input);
}
if (!gameOver) {
printPrompt();
} else {
gameOverDelayCount++;
}
if (gameOverDelayCount >= 3) {
System.exit(0);
}
}
)
);
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
// rows are numbered from zero at the top
// columns are numbers from zero at the left
boardPane.setGridLinesVisible(true);
int squareSize = 50;
for (int r = 0; r < Board.BOARD_SIZE; r++) {
boardPane.getColumnConstraints().add(new ColumnConstraints(squareSize));
boardPane.getRowConstraints().add(new RowConstraints(squareSize));
for (int c = 0; c < Board.BOARD_SIZE; c++) {
Button button = new Button();
boardPane.add(button, c, r);
displaySquares[r][c] = button;
button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
GridPane.setFillHeight(button, true);
GridPane.setFillWidth(button, true);
}
}
refreshBoard();
mainPane.setCenter(boardPane);
mainPane.setBottom(commandField);
mainPane.setRight(infoArea);
primaryStage.setScene(new Scene(mainPane));
primaryStage.show();
printGameStart();
printPrompt();
}
private void pause() throws InterruptedException {
try {
Thread.sleep(Main.BOT_DELAY);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
void refreshBoard() {
for (int r = 0; r < Board.BOARD_SIZE; r++) {
for (int c = 0; c < Board.BOARD_SIZE; c++) {
displaySquare(r, c);
}
}
}
void displaySquare(int r, int c) {
Square square = scrabble.getBoard().getSquare(r, c);
Button button = displaySquares[r][c];
StringBuilder style = new StringBuilder();
style.append("-fx-background-radius: 0;");
String color;
if (square.isDoubleLetter()) {
color = "8080ff";
} else if (square.isTripleLetter()) {
color = "0000ff";
} else if (square.isDoubleWord()) {
color = "ff8080";
} else if (square.isTripleWord()) {
color = "ff0000";
} else {
color = "ffffff";
}
style.append("-fx-background-color: #").append(color).append(';');
if (square.isOccupied()) {
style.append("-fx-font-size: 14pt;");
style.append("-fx-font-weight: bold;");
button.setStyle(style.toString());
button.setText(square.getTile() + ""); // placed letter
} else {
style.append("-fx-font-size: 8pt;");
style.append("-fx-font-weight: lighter;");
button.setStyle(style.toString());
if (r==0) {
button.setText(((char) (((int) 'A') + c)) + ""); // column letters
} else if (c==0) {
button.setText((r + 1) + ""); // row numbers
} else {
button.setText(""); // empty
}
}
}
// Input methods
private void processInput(String input) {
// this deals with user commands
String command = input.trim().toUpperCase();
Player currentPlayer = scrabble.getCurrentPlayer();
if (!gameOver && (command.equals("PASS") || command.equals("P"))) {
scrabble.zeroScorePlay();
if (scrabble.isZeroScorePlaysOverLimit()) {
printZeroScorePlaysOverLimit();
gameOver = true;
} else {
opponentMadePlay = false;
scrabble.turnOver();
}
} else if (!gameOver && (command.equals("HELP") || command.equals("H"))) {
printHelp();
} else if (!gameOver && (command.equals("SCORE") || command.equals("S"))) {
printScores();
} else if (!gameOver && (command.equals("POOL") || command.equals("O"))) {
printPoolSize();
} else if (!gameOver &&
(command.matches("[A-O](\\d){1,2}( )+[A,D]( )+([A-Z]){1,15}") || // no blanks
(command.matches("[A-O](\\d){1,2}( )+[A,D]( )+([A-Z_]){1,17}( )+([A-Z]){1,2}") // with blanks
&& isValidPlayWithBlanks(command)) ) ) {
// no blanks
Word word = parsePlay(command);
if (!scrabble.getBoard().isLegalPlay(currentPlayer.getFrame(), word)) {
printPlayError(scrabble.getBoard().getErrorCode());
} else {
scrabble.play(word);
refreshBoard();
printPoints(scrabble.getLatestPoints());
if (scrabble.framesAreEmpty()) {
printAllTilesPlayed();
gameOver = true;
} else {
opponentMadePlay = true;
scrabble.turnOver();
}
}
} else if (!gameOver && (command.matches("EXCHANGE( )+([A-Z_]){1,7}") || command.matches("X( )+([A-Z_]){1,7}"))) {
String[] parts = command.split("( )+");
String letters = parts[1];
if (!currentPlayer.getFrame().isLegalExchange(scrabble.getPool(),letters)) {
printExchangeError(currentPlayer.getFrame().getErrorCode());
} else {
currentPlayer.getFrame().exchange(scrabble.getPool(),letters);
printTiles();
scrabble.zeroScorePlay();
if (scrabble.isZeroScorePlaysOverLimit()) {
printZeroScorePlaysOverLimit();
gameOver = true;
} else {
opponentMadePlay = false;
scrabble.turnOver();
}
}
} else if (!gameOver && (command.matches("NAME( )+[A-Z][A-Z0-9]*") || command.matches("N( )+[A-Z][A-Z0-9]*"))) {
String[] parts = input.split("( )+");
currentPlayer.setName(parts[1]);
printNewName();
} else if (!gameOver && opponentMadePlay && (command.equals("CHALLENGE") || command.equals("C"))) {
if (scrabble.getDictionary().areWords(scrabble.getLatestWords())) {
printChallengeFail();
scrabble.turnOver();
} else {
scrabble.undoPlay();
refreshBoard();
printChallengeSuccess();
}
} else {
printLine("Error: command syntax incorrect. See help.");
}
if (gameOver) {
scrabble.adjustScores();
printScores();
printWinner();
printGameOver();
}
}
private Word parsePlay(String command) {
// this converts the play command into a Word
String[] parts = command.split("( )+");
String gridText = parts[0];
int column = ((int) gridText.charAt(0)) - ((int) 'A');
String rowText = parts[0].substring(1);
int row = Integer.parseInt(rowText)-1;
String directionText = parts[1];
boolean isHorizontal = directionText.equals("A");
String letters = parts[2];
Word word;
if (parts.length == 3) {
word = new Word(row, column, isHorizontal, letters);
} else {
String designatedBlanks = parts[3];
word = new Word(row, column, isHorizontal, letters, designatedBlanks);
}
return word;
}
private boolean isValidPlayWithBlanks(String command) {
// this just checks that the number of blanks matches the number of designated blanks
String[] parts = command.split("( )+");
int blankCount = 0;
for (int i=0; i<parts[2].length(); i++) {
if (parts[2].charAt(i) == Tile.BLANK) {
blankCount++;
}
}
if (blankCount == 0 || parts[3].length() != blankCount) {
return false;
} else {
return true;
}
}
// Print methods
private void print(String text) {
infoArea.appendText(text);
latestInfo = text;
allInfo.append(text);
}
private void printLine(String text) {
print(text + "\n");
}
private void printGameStart() {
printLine("WELCOME TO SCRABBLE");
}
private void printNewName() {
printLine("Player " + scrabble.getCurrentPlayer().getPrintableId() + " is now named " + scrabble.getCurrentPlayer().getName() + ".");
}
private void printTiles() {
printLine(scrabble.getCurrentPlayer() + " has the following tiles:");
for (Tile tile : scrabble.getCurrentPlayer().getFrame().getTiles()) {
if (!Main.BOT_GAME) {
print(tile + " ");
} else {
print("? ");
}
}
printLine("");
}
private void printPrompt() {
printLine(scrabble.getCurrentPlayer() + "'s turn:");
printTiles(); // suppressed for the Bots
}
private void printPoints(int points) {
printLine(scrabble.getCurrentPlayer() + " scored " + points + " points.");
}
private void printPoolSize() {
printLine("Pool has " + scrabble.getPool().size() + " tiles");
}
private void printHelp() {
printLine("Command options: Q (quit), N (name), P (pass), X (exchange), C (challenge), S (scores), O (pool) or play");
printLine("Names must begin with a letter.");
printLine("For an exchange, enter the letters that you wish to exchange. E.g. X ABC");
printLine("For a play, enter the grid reference of the first letter, A (across) or D (down), " +
" and the word including any letters already on the board. E.g. A1 D HELLO");
printLine("For a play with a blank, enter the grid reference of the first letter, A (across) or D (down)," +
" the word with underscores for blanks including any letters already on the board," +
" and the designated letter(s) for the blanks. E.g. A1 D H_LLO E");
}
private void printPlayError(int errCode) {
String message = "";
switch (errCode) {
case Board.WORD_OUT_OF_BOUNDS:
message = "Error: Word does not fit on the board.";
break;
case Board.WORD_LETTER_NOT_IN_FRAME:
message = "Error: You do not have the necessary letters.";
break;
case Board.WORD_LETTER_CLASH:
message = "Error: The word entered does not fit with the letters on the board.";
break;
case Board.WORD_NO_LETTER_PLACED:
message = "Error: The word does not use any of your letters.";
break;
case Board.WORD_NO_CONNECTION:
message = "Error: The word is not connected with the words on the board. ";
break;
case Board.WORD_INCORRECT_FIRST_PLAY:
message = "Error: The first word must be in the centre of the board.";
break;
case Board.WORD_EXCLUDES_LETTERS:
message = "Error: The word places excludes letters already on the board";
break;
case Board.WORD_ONLY_ONE_LETTER:
message = "Error: Only one letter in the word.";
break;
}
printLine(message);
}
public void printExchangeError (int errCode) {
String message = "";
switch (errCode) {
case Frame.EXCHANGE_NOT_AVAILABLE:
message = "Error: Letter not available in the frame.";
break;
case Frame.EXCHANGE_NOT_ENOUGH_IN_POOL:
message = "Error: No enough tiles in the pool.";
break;
}
printLine(message);
}
private void printZeroScorePlaysOverLimit() {
printLine("The number of zero score plays is over the limit.");
}
private void printScores() {
for (Player player : scrabble.getPlayers()) {
printLine(player + " has " + player.getScore() + " points.");
}
}
private void printChallengeFail() {
printLine("Challenge fail - the words are in the dictionary.");
printLine("Turn over.");
}
private void printChallengeSuccess() {
printLine("Challenge success - the words are not in the dictionary.");
printLine(scrabble.getOpposingPlayer() + " loses " + scrabble.getLatestPoints() + ".");
}
private void printAllTilesPlayed() {
printLine("All tiles played.");
}
private void printWinner() {
int maxScore = -1000;
ArrayList<Player> winners = new ArrayList<>();
boolean draw = false;
for (Player player : scrabble.getPlayers()) {
if (player.getScore() > maxScore) {
draw = false;
winners.clear();
winners.add(player);
maxScore = player.getScore();
} else if (player.getScore() == maxScore) {
draw = true;
winners.add(player);
}
}
if (!draw) {
printLine(winners.get(0) + " wins!");
System.out.println(winners.get(0).getName());
System.out.println(winners.get(0).getScore());
} else {
printLine("The following players draw!");
for (Player winner : winners) {
printLine(winner + "");
}
}
}
private void printGameOver() {
printLine("GAME OVER");
}
// Methods for Bots
public String getLatestInfo() {
return latestInfo;
}
public String getAllInfo() {
return allInfo + "";
}
}