-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameplay.java
452 lines (422 loc) · 17.4 KB
/
gameplay.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
450
451
452
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
import java.util.Scanner;
import java.util.*;
// This class handles move validation and score handling
// It also handles tileBag and tileScores
public class gameplay {
private HashMap<Character, Integer> tileBag;
private player[] players = new player[2];
private int turn;
scores scores;
public gameplay() {
this.initTileBag();
}
//Tile bag initialization and distribution
//Values taken from : 'thesprucecrafts.com/scrabble-tile-distribution-and-point-values-412402'
private void initTileBag() {
this.tileBag = new HashMap<Character, Integer>();
if (this.tileBag.isEmpty()) {
this.tileBag.put('A', 9);
this.tileBag.put('B', 2);
this.tileBag.put('C', 2);
this.tileBag.put('D', 4);
this.tileBag.put('E', 12);
this.tileBag.put('F', 2);
this.tileBag.put('G', 3);
this.tileBag.put('H', 2);
this.tileBag.put('I', 9);
this.tileBag.put('J', 1);
this.tileBag.put('K', 1);
this.tileBag.put('L', 4);
this.tileBag.put('M', 2);
this.tileBag.put('N', 6);
this.tileBag.put('O', 8);
this.tileBag.put('P', 2);
this.tileBag.put('Q', 1);
this.tileBag.put('R', 6);
this.tileBag.put('S', 4);
this.tileBag.put('T', 6);
this.tileBag.put('U', 4);
this.tileBag.put('V', 2);
this.tileBag.put('W', 2);
this.tileBag.put('X', 1);
this.tileBag.put('Y', 2);
this.tileBag.put('Z', 1);
}
}
/*
used when game starts and after each move
@param C tile to be removed
@return removed successfully
*/
public boolean removeTileFromBag(char C) {
if (this.tileBag.containsKey(C) && this.tileBag.get(C) > 0) {
int newValue = this.tileBag.get(C) - 1;
this.tileBag.put(C, newValue);
return true;
}
return false;
}
/*
used when players give up their turns
@param C tile added back to bag
@return added successfully
*/
public void addTileToBag(char C) {
if (this.tileBag.containsKey(C)) {
int newValue = this.tileBag.get(C) + 1;
this.tileBag.put(C, newValue);
}
}
public int totalNumberOfTilesInBag() {
int total = 0;
for (int i = 65; i <= 90; i++) {
char C = (char) i;
total += tileBag.get(C);
}
return total;
}
/*
used when players give up their turns
@param C Letter missing from the tray
*/
public Object getRandomLetterFromBag(char C) {
Random random = new Random();
Object[] letters = this.tileBag.keySet().toArray();
Object randomLetter = letters[random.nextInt(letters.length)];
if (this.tileBag.get(randomLetter) == 0) {
//Need to call function recursively
} else {
int newValue = this.tileBag.get(randomLetter) - 1;
this.tileBag.put((Character) randomLetter, newValue);
System.out.println(randomLetter);
}
return randomLetter;
}
public void createNewPlayers() {
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < players.length; i++) {
System.out.println("Enter the name of player "+ (i+1) +" : ");
players[i] = new player(scanner.next(), i, new String[7]);
}
this.scores = new scores(players[0], players[1]);
}
public void endGame() {
System.out.println("Game Over!");
System.out.println("Thank for playing " + players[0].getName() + " and " + players[1].getName() + "!");
}
public int switchTurn() {
if (turn == 1 || turn == 0) {
turn = 2;
} else if (turn == 2) {
turn = 1;
}
return turn;
}
public void gameOn(int turn, Game game) {
player thePlayer = null;
if (turn == 1) {
thePlayer = players[0];
} else if (turn == 2) {
thePlayer = players[1];
}
//Game game = new Game();
Scanner scanner = new Scanner(System.in);
view.printGame(game);
refillTray(thePlayer);
view.showTray(thePlayer);
System.out.println(players[0].getName() + ", ENTER A WORD (Skip: *, Quit: #, reset tiles: @, view scores: $");
String theWord = scanner.next();
if (theWord.equals("*")) {
System.out.println("You decided to skip you turn.");
gameOn(switchTurn(), game);
} else if (theWord.equals("@")) {
refreshTray(thePlayer);
gameOn(switchTurn(), game);
} else if (theWord.equals("$")){
view.printPlayerInformation(players[0], players[1], scores);
//gameOn(switchTurn(), game);
} else if (theWord.equals("#")) {
endGame();
} else {
System.out.println("Now a direction [right: 1 or down: 2]: ");
int theDirection = scanner.nextInt();
System.out.println("Start column: ");
int theCol = scanner.nextInt();
System.out.println("Start row: ");
int theRow = scanner.nextInt();
move theMove = new move(theWord, theDirection, theRow, theCol);
if (isMoveValid(thePlayer, theMove, game)) {
game.placeWordOnBoard(theMove);
refreshTray(thePlayer);
}
gameOn(switchTurn(), game);
// if (Game.validateWord(theWord)) {
// System.out.println(theWord + " is valid!");
// //place word on board
// //calculate score
// //add score to total score
// gameOn(switchTurn());
// } else {
// System.out.println("Sorry " + theWord + " is invalid");
// gameOn(switchTurn());
// }
}
}
public void refreshTray(player player) {
String[] lettersTray = player.getLetters();
for (int i = 0; i < lettersTray.length; i++) {
this.addTileToBag(lettersTray[i].charAt(0));
player.letters[i] = null;
}
}
public void refillTray(player player) {
String[] lettersTray = player.getLetters();
for (int i = 0; i < lettersTray.length; i++) {
if (lettersTray[i] == "" || lettersTray[i] == null) {
lettersTray[i] = String.valueOf(this.getRandomLetterFromBag('i'));
}
}
player.setLetters(lettersTray);
}
private boolean doesPlayerHaveTiles(player player, move move, Game game) {
return false;
}
/*
@param move move object played
@return boolean
*/
public boolean isMoveValid(player player, move move, Game game) {
String word = move.word.toUpperCase();
int row = move.startRow;
int col = move.startCol;
int dir = move.direction;
String[] tileCopy = player.getLetters();
Arrays.sort(tileCopy);
boolean tilesPresent = false;
System.out.println("Move details:");
System.out.println("Word: " + word);
System.out.println("Row: " + row);
System.out.println("Col: " + col);
System.out.println("Dir: " + dir);
System.out.println("tileCopy: " + Arrays.toString(tileCopy));
System.out.println("[Cool move validations begin]");
// System.out.println(player.getName());
// System.out.println("What are the players letters? " + player.getLetters()[0]);
//does board overflow?
if ((dir == move.RIGHT && (col + word.length() > 14)) ||
(dir == move.DOWN && (row + word.length() > 14))) {
System.out.println("isMoveValid err:: Board overflow.");
return false;
}
System.out.println("Board overflow validations................................done");
//is word valid using dictionary?
if (!Game.validateWord(word)) {
System.out.println("isMoveValid err:: This word doesn't exist!.");
return false;
}
System.out.println("Dictionary validations................................done");
//intended word should be the largest contiguous string in that direction
if (dir == move.RIGHT) {
if (game.getTileOnBoard(row, col + word.length() + 1) != ' ' ||
game.getTileOnBoard(row, col - 1) != ' ') {
System.out.println("Incomplete input word");
return false;
}
} else if (dir == move.DOWN) {
if (game.getTileOnBoard(row + word.length() + 1, col) != ' ' ||
game.getTileOnBoard(row - 1, col) != ' ') {
System.out.println("Incomplete input word");
return false;
}
}
System.out.println("Contiguous string validations................................done");
//does player have enough tiles to play
// if (!this.doesPlayerHaveTiles(player, move, game)) {
// System.out.println("Not enough tiles to play move!");
// return false;
// }
// if (move.totalNumberOfMoves == 1 && (row > 7 && col > 7)) {
// System.out.println("First move should touch the center of the board.");
// return false;
// }
//if it's the first move of the game does the work cross the centre X?
if (move.totalNumberOfMoves == 0) {
if ((dir == move.RIGHT && (col + word.length() < 7)) ||
(dir == move.DOWN && (row + word.length() < 7)) ||
(row > 7 && col > 7)) {
System.out.println("isMoveValid err: First move should touch the board center!");
return false;
}
}
//does the second (or greater) move touch an existing tile
if (move.totalNumberOfMoves >= 1) {
for (int i = 0; i < word.length(); i++) {
if (dir == move.RIGHT) {
if (game.getTileOnBoard(row, col + i) != ' ') { tilesPresent = true; }
} else if (dir == move.DOWN) {
if (game.getTileOnBoard(row + i, col) != ' ') { tilesPresent = true; }
}
}
if (!tilesPresent) {
System.out.println("isMoveValid err:: New word has to touch an existing word.");
return false;
}
}
//can the word be constructed on the board with existing pieces and player tiles
// for (int i = 0; i < word.length(); i++) {
// if (dir == move.RIGHT) {
// if (game.getTileOnBoard(row, col + i) == word.charAt(i)) {
// //don't do anything, this is expected
// } else if (game.getTileOnBoard(row, col + i) == ' ') {
// //empty cell - user should have it
// int pos = Arrays.binarySearch(tileCopy, String.valueOf(word.charAt(i)));
// if (pos >= 0) {
// tileCopy[pos] = null;
// } else {
// //neither empty cell nor unexpected char -- stepping over someone else's placed piece
// System.out.println("Unknown character! Unable to insert '" + word.charAt(i) + "'.");
// return tilesPresent;
// }
// } else if (dir == move.DOWN) {
// if (game.getTileOnBoard(row + i, col) == word.charAt(i)) {
// //expected for at most all-1 cases
// } else if (game.getTileOnBoard(row + i, col) == ' ') {
// //empty cell - find tile with player
// int pos = Arrays.binarySearch(tileCopy, String.valueOf(word.charAt(i)));
// if (pos >= 0) {
// tileCopy[pos] = null;
// } else {
// System.out.println("You are missing the letter '" + word.charAt(i) + "'.");
// return tilesPresent;
// }
// } else {
// //neither empty cell not expected char -- stepping over someone else's placed piece
// System.out.println("Unknown character! Unable to insert '" + word.charAt(i) + "'.");
// return tilesPresent;
// }
// }
// }
// //no letter was used from the tray
// if (tileCopy.length == player.getLetters().length) {
// System.out.println("The word already exists. Try again!");
// return tilesPresent;
// }
// }
System.out.println("Board placement validations................................done");
//are secondary words valid if existing
ArrayList<String> secList = this.getSecondaryWords(move, game);
if (!this.validateSecondaryWords(secList)) {
System.out.println("isMoveValid err:: The other words you created are invalid.");
return tilesPresent;
}
// 1. Set the move to valid
// 2. Secondary words created should be passed onto the move object
move.isValid = true;
move.registerCorrectMove(move);
move.secondaryWords = secList;
System.out.println("[Cool move validations complete]");
return tilesPresent;
}
private boolean validateSecondaryWords(ArrayList<String> list) {
for (String str: list) {
if (!Game.validateWord(str)) {
System.out.println(str+ " not valid secondary word.");
return false;
}
}
return true;
}
/*
private helper method that returns the other words formed due to a move
@param move current move
@param board board object
@return list containing secondary words
*/
// TODO: change to private once testing is complete
public ArrayList<String> getSecondaryWords (move move, Game game){
ArrayList<String> secWords = new ArrayList<String>();
String word = move.word.toUpperCase();
int row = move.startRow;
int col = move.startCol;
int dir = move.direction;
for (int i = 0; i < word.length(); i++) {
if (dir == move.RIGHT) {
if (game.getTileOnBoard(row, col + i) == ' ') {
//user is inserting a tile
if (game.getTileOnBoard(row - 1, col + i) != ' ' ||
game.getTileOnBoard(row + 1, col + i) != ' ') {
System.out.println("calling construct word for: " + word.charAt(i));
secWords.add(this.constructWord(row, col + i, word.charAt(i), move, game));
}
}
} else if (dir == move.DOWN) {
if (game.getTileOnBoard(row + i, col) == ' ') {
if (game.getTileOnBoard(row + i, col - 1) != ' ' ||
game.getTileOnBoard(row + i, col + 1) != ' ') {
System.out.println("calling construct word for: " + word.charAt(i));
secWords.add(this.constructWord(row + i, col, word.charAt(i), move, game));
}
}
}
}
return secWords;
}
/*
private helper method that returns the other words formed due to a move
@param row row index
@param col col index
@param C
@return constructed word/ null
*/
private String constructWord(int row, int col, char C, move move, Game game) {
StringBuilder newWord = new StringBuilder();
int start;
int end;
int i = 1;
int j = 1;
int dir = move.direction;
//String word = move.word;
C = Character.toUpperCase(C);
if (dir == move.DOWN) {
System.out.println("dir down");
while (col - j >= 0 && game.getTileOnBoard(row, col - j) != ' ') {
j++;
}
start = col - j + i; //determine start index of word to be formed
j = 1; //reset j
while (col + j <= 14 && game.getTileOnBoard(row, col + j) != ' ') {
j++;
}
end = col + j - 1; //determine end index of word
for (int newCol = start; newCol <= end; newCol++) {
if (newCol == col) {newWord.append(C);}
else { newWord.append(game.getTileOnBoard(row, newCol)); }
}
return newWord.toString().trim();
} else if (dir == move.RIGHT) {
System.out.println("dir right");
while (row - i >= 0 && game.getTileOnBoard(row - i, col) != ' ') {
i++;
}
start = row - i + 1;
i = 1;
while (row + i <= 14 && game.getTileOnBoard(row + i, col) != ' ') {
i++;
}
end = row + i - 1;
for (int newRow = start; newRow <= end; newRow++) {
if (newRow == row) {
newWord.append(C);
} else {
newWord.append(game.getTileOnBoard(newRow, col));
}
}
return newWord.toString().trim();
}
System.out.println("Constructing null");
return null;
}
}