-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTurn.java
More file actions
674 lines (584 loc) · 27.4 KB
/
Copy pathTurn.java
File metadata and controls
674 lines (584 loc) · 27.4 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
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
/**
* A class that represents the turns of each player
*
* @Team MAGA
* @Author Gajun Young - 16440714
* @Author Royal Thomas - 16326926
* @Author Richard Otroshchenko - 16353416
*/
public class Turn {
private CluedoUI ui;
private TileGrid grid = new TileGrid();
private Questions questions = new Questions();
private Weapons items;
private Players players;
private Players dummies;
private int numofPlayers;
ArrayList<Card> murderEnvelope = new ArrayList<>(); //Holds the murder envelope contents
public Turn(CluedoUI ui, Players players, Weapons weapons, Players dummies) {
this.ui = ui;
this.players = players;
this.items = weapons;
this.dummies = dummies;
}
//Mutator
public void setTurn(Players players, Weapons weapons, Players dummies) {
this.players = players;
this.items = weapons;
this.dummies = dummies;
this.numofPlayers = players.getCapacity();
}
/**
* Each player takes turns
*/
public void turns() {
String command; //Text that is entered
boolean valid; //Check if action is valid
Dice dice = new Dice();
do {
for (int i = 0; i < players.getCapacity(); i++) {
int selectedOption = -1;
ui.displayString("ROTATING PLAYER.......");
//Have window be still for 2 seconds
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
ui.clearContent(); //Clears info panel for next player
do {
//Pops up a JOptionPane and ensures turn is passed
selectedOption = JOptionPane.showConfirmDialog(null,
"Turn handed over to " + players.currPlayer(i) + "?", "TURN", JOptionPane.YES_NO_OPTION);
}while(selectedOption != JOptionPane.YES_NO_OPTION);
//Checks if player is alive
if (!players.getPlayer(i).isAlive()) {
continue;
}
if (numofPlayers == 1) {
finishGame(players.getPlayer(i));
}
valid = false;
//First prompt for first board action
ui.displayString("===" + players.currPlayer(i) + "'s TURN===");
ui.displayString("Type a command");
CommandPanel.updateUserImage(players.getPlayer(i).getImagePath());
//Available commands
CommandPanel.updateCommands(Commands.firstCommands);
CommandPanel.updateMovesReamining(-1);
do {
command = ui.getCommand();
ui.displayString(players.currPlayer(i) + ": " + command);
if (command.equalsIgnoreCase("roll")) {
//Rolls the dice and displays the result on-screen
dice.rollDice();
CommandPanel.updateCommands();
ui.drawDice(dice.getRoll1(), dice.getRoll2());
ui.displayString(players.currPlayer(i) + " rolled " + (dice.getRoll1()
+ dice.getRoll2()));
ui.display();
try { //The resulting dice roll is on-screen for 2 seconds
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
ui.drawDice(0, 0); //This hides the dice
ui.display();
valid = true;
} else if (command.equalsIgnoreCase("notes")) {
//Displays all cards apart from murder envelope ones, indicating cards
// they own or cards everyone can see
players.getPlayer(i).displayNote();
} else if (command.equalsIgnoreCase("cheat")) {
//Displays the murder envelope cards
cheat();
} else if (command.equalsIgnoreCase("log")){
log();
}
else if (command.equalsIgnoreCase("help")) {
//Displays various commands and an explanation on what they do
help();
} else {
ui.displayString(
"Whoops! Wrong command.\nType 'help' if you're unsure what to do.");
}
} while (!valid); //The first part of their turn ends only when they make a roll
//After rolling, the player decides where and how to move
movement(dice.getRoll1() + dice.getRoll2(), i);
//After all actions
ui.displayString(players.currPlayer(i) + "Out of moves! Type a command.");
if (players.getTile(i).getSlot() == 5) {
String[] extra;
if (players.getTile(i).getRoom() != 10) {
extra = Commands.questionCommands;
} else {
extra = Commands.accuseCommands;
}
CommandPanel.updateCommands(extra);
} else {
CommandPanel.updateCommands(Commands.endCommands);
}
boolean questionBarrier = false; //This turns to true if the user has asked a question this turn
do {
valid = false;
command = ui.getCommand();
ui.displayString(players.currPlayer(i) + ": " + command);
if (command.equalsIgnoreCase("done") && players.getTile(i).getRoom() != 10) {
valid = true;
} else if (command.equalsIgnoreCase("notes")) {
players.getPlayer(i).displayNote();
} else if (command.equalsIgnoreCase("cheat")) {
cheat();
} else if(command.equalsIgnoreCase("log")){
log();
} else if (command.equalsIgnoreCase("help")) {
help();
} else if (players.getTile(i).getSlot() == 5 && players.getTile(i).getRoom()
!= 10 && !questionBarrier) {
if (command.equalsIgnoreCase("question")) {
questionBarrier = questions.question(players, i, ui, this);
}
} else if (players.getTile(i).getRoom() == 10) {
if (command.split(" ")[0].equalsIgnoreCase("accuse")) {
Boolean done = accuse(command, players.getPlayer(i));
if (done) {
valid = true;
}
}
} else {
ui.displayString(
"Whoops! Wrong command.\nType 'help' if you're unsure what to do.");
}
if(!valid) {
ui.displayString("Type another command");
}
if(players.getTile(i).getRoom() == 10 && !valid) {
CommandPanel.updateCommands(Commands.accuseCommands);
}else if(players.getTile(i).getRoom() != 10 && !valid && !questionBarrier) {
CommandPanel.updateCommands(Commands.questionCommands);
}else {
CommandPanel.updateCommands(Commands.endCommands);
}
} while (!valid); //Their turn ends after they type the 'done' command
}
} while (true);
}
//The following method iterates through the contents of the murder envelope and displays it
// to the player
private void cheat() {
ui.displayString("======CHEATS======");
for (Card x : murderEnvelope) {
ui.displayString(x.toString());
}
}
//The following method displays info about each command to the player
private void help() {
ui.displayString("======HELP======");
ui.displayString("'roll' - to roll the dice and begin your turn."
+ "\nA roll ranges from 1 to 6 and you can move that many spaces on the board."
+ "\n\n'notes' - Type this to inspect your notes."
+ "\nThis lists all players, weapons and rooms,\n'X' mark for cards"
+ "\n'A' mark for cards everybody sees."
+ "\n'V' for cards that were 'seen' "
+ "\n\n'cheat' - Allows you to inspect the-\nmurder envelope."
+ "\n\n'u,r,d,l' - Type one of these to move up, right,-\ndown or left respectively."
+ "\n\n'Passage' - Type to move from one corner-\nof the board using a room to room"
+ " passageway"
+ "\n\n'log' - To show a list of questions asked-\nand people that answered"
+ "\n\n'done' - Finishes up your turn or a requirement"
+ "\n\n'question' - To question players about -\nyour suggestion"
+ "\n\n'accuse' - To accuse the murder, with the -\nweapon and place they killed the victum"
+ "\n\n'quit' - This ends the game immediately.");
}
//This method provides the user with a log of all questions asked, including who answered them.
private void log(){
List<String> log = questions.getLog();
if(log.size() == 0){
ui.displayString("No questions have been asked yet.");
}else {
for (int i = 0; i < log.size(); i++) {
ui.displayString(i + 1 + ". " + log.get(i));
}
}
}
//Method used in main to tell the turn class the contents of the murder envelope
public void setMurderEnvelope(ArrayList<Card> array) {
murderEnvelope = array;
}
/**
* Allows players to move depending on their dice roll
*/
public void movement(int dice, int currPlayer) {
String
direction; //Contains direction of where the
// user wants to go
boolean validDirection; //If direction is valid
int sentinel = 0; //Ensures right warning is displayed
Tile currTile = players.getTile(currPlayer);
ui.displayString(players.currPlayer(currPlayer) + "make your move!");
//Set of commands a player could possibly use
CommandPanel.updateCommands(Commands.movementCommands);
CommandPanel.updateMovesReamining(dice);
do {
validDirection = false; //Reset
sentinel = 0; //Reset
if (players.getTile(currPlayer).getSlot() == 5 || players.getTile(currPlayer).getSlot()
== 3 || players.getTile(currPlayer).getSlot() == 4) {
if (players.getTile(currPlayer).getSlot() == 3) {
ui.displayString("Choose another exit");
}
Boolean tookSecretPath = exitRoom(currPlayer,
players.getTile(currPlayer).getRoom());
ui.display();
if (!tookSecretPath) {
ui.displayString(players.currPlayer(currPlayer) + " now choose a direction");
CommandPanel.updateMovesReamining(dice);
} else {
dice = 0;
break;
}
}
direction = ui.getCommand();
ui.displayString(players.currPlayer(currPlayer) + ": " + direction);
//Catch if array is out of bounds
//Move character to another tile depending on direction choosen
try {
if (direction.equalsIgnoreCase("u")) {
currTile = grid.map[players.getTile(currPlayer).getRow() - 1][players.getTile(
currPlayer).getColumn()];
validDirection = true;
} else if (direction.equalsIgnoreCase("d")) {
currTile = grid.map[players.getTile(currPlayer).getRow() + 1][players.getTile(
currPlayer).getColumn()];
validDirection = true;
} else if (direction.equalsIgnoreCase("l")) {
currTile = grid.map[players.getTile(currPlayer).getRow()][players.getTile(
currPlayer).getColumn() - 1];
validDirection = true;
} else if (direction.equalsIgnoreCase("r")) {
currTile = grid.map[players.getTile(currPlayer).getRow()][players.getTile(
currPlayer).getColumn() + 1];
validDirection = true;
} else {
ui.displayString("'" + direction + "'" + " is not a valid direction");
sentinel = -1;
}
} catch (ArrayIndexOutOfBoundsException e) {
ui.displayString("Invalid direction...[Off the board]");
sentinel = -1;
}
//Ensures no two players are on the same slot
for (int i = 0; i < players.getCapacity(); i++) {
if (currTile == players.getTile(i) && validDirection && players.getPlayer(i)
!= players.getPlayer(currPlayer)) {
validDirection = false;
ui.displayString(
players.currPlayer(currPlayer) + " cannot move onto an occupied tile");
sentinel = -1;
break;
}
}
//If the moved tile is a path & the move is valid
if ((currTile.getSlot() == 1 || currTile.getSlot() == 7) && validDirection) {
if (players.getTile(currPlayer).getSlot() == 3 && currTile.getSlot() == 7) {
ui.displayString("There's a wall in your way...");
} else {
players.getPlayer(currPlayer).getToken().moveBy(currTile);
ui.display();
dice--;
CommandPanel.updateMovesReamining(dice);
}
} else if ((currTile.getSlot() == 3 && players.getTile(currPlayer).getSlot() != 7)
&& validDirection) {
//Enter a room
players.getPlayer(currPlayer).getToken().moveBy(currTile);
//Move into center of room and no more movement
roomCenter(currPlayer, players.getTile(currPlayer).getRoom());
ui.display();
dice = 0;
CommandPanel.updateMovesReamining(dice);
} else if (sentinel == 0 && (currTile.getSlot() != 3 || (currTile.getSlot() == 3
&& players.getTile(currPlayer).getSlot() == 7)
|| currTile.getSlot() == 7 && players.getTile(currPlayer).getSlot() == 3)) {
ui.displayString("There's a wall in your way...");
}
} while (dice > 0);
}
/**
* Reposition players onto the centre of the room
*/
public void roomCenter(int currPlayer, int room) {
Tile currTile = players.getTile(currPlayer);
boolean invalidRoom = true;
//Looks for the centre in which a character is positioned
for (int i = 0; i < grid.map.length; i++) {
//Quick escape from nested for loop if the value is found early.
if (!invalidRoom) {
break;
}
for (int j = 0; j < grid.map[i].length; j++) {
if (grid.map[i][j].getSlot() == 5 && grid.map[i][j].getRoom() == room
&& invalidRoom) {
currTile = grid.map[i][j];
invalidRoom = players.getSameTile(currTile);
//Might be in dummies array
if (!invalidRoom) {
invalidRoom = dummies.getSameTile(currTile);
}
}
}
}
players.getPlayer(currPlayer).getToken().moveBy(currTile);
}
/**
* Gives a list of exits to players if there are exits
*/
public Boolean exitRoom(int currPlayer, int room) {
ArrayList<Tile> exits = new ArrayList<Tile>();
//Searches for possible exits
for (int i = 0; i < grid.map.length; i++) {
for (int j = 0; j < grid.map[i].length; j++) {
if (room == grid.map[i][j].getRoom() && grid.map[i][j].getSlot() == 3) {
exits.add(grid.map[i][j]);
}
}
}
//Displays an array of exits for players
int numExits = 0;
String exitChoice;
ui.displayString("Available exits for " + players.currPlayer(currPlayer));
for (Tile t : exits) {
ui.displayString(++numExits + ". Exit location" + " " + t.showRoom());
}
if (room == 9 || room == 7 || room == 1 || room == 3) {
ui.displayString("Enter 'passage' to take the secret passage!");
}
CommandPanel.updateMovesReamining(-2);
//Player chooses an exit that isn't blocked
do {
do {
exitChoice = ui.getCommand();
ui.displayString(players.currPlayer(currPlayer) + ": " + exitChoice);
if (exitChoice.equalsIgnoreCase("passage") && (room == 9 || room == 7 || room == 1
|| room == 3)) {
break;
}
if (!StartUp.isNum(exitChoice)) {
ui.displayString("'" + exitChoice + "'" + " is not a valid choice");
}
} while (!StartUp.isNum(exitChoice));
if (exitChoice.equalsIgnoreCase("passage") && (room == 9 || room == 7 || room == 1
|| room == 3)) {
break;
}
if (Integer.parseInt(exitChoice) < 1 || Integer.parseInt(exitChoice) > exits.size()) {
ui.displayString(exitChoice + " is not a valid exit choice");
}
} while (Integer.parseInt(exitChoice) < 1 || Integer.parseInt(exitChoice) > exits.size());
if (exitChoice.equalsIgnoreCase("passage") && ((room == 9 || room == 7 || room == 1
|| room == 3))) {
switch (room) {
case 9:
roomCenter(currPlayer, 1);
break;
case 3:
roomCenter(currPlayer, 7);
break;
case 7:
roomCenter(currPlayer, 3);
break;
case 1:
roomCenter(currPlayer, 9);
default:
break;
}
return true;
} else {
players.getPlayer(currPlayer).getToken().moveBy(
exits.get(Integer.parseInt(exitChoice) - 1));
return false;
}
}
/**
* Teleports weapon token, based on player's suggestion
*/
public void weaponTeleport(String name, int room) {
Tile currTile = items.get(name).getPosition(); //Tile of current weapon
boolean invalidRoom = true;
//Ensures that two weapons don't land onto same tile
if (currTile.getRoom() != room) {
//Looks for the position in which a weapon is positioned
for (int i = 0; i < grid.map.length; i++) {
//Quick escape from nested for loop if the value is found early.
if (!invalidRoom) {
break;
}
for (int j = 0; j < grid.map[i].length; j++) {
if (grid.map[i][j].getSlot() == 6 && grid.map[i][j].getRoom() == room
&& invalidRoom) {
currTile = grid.map[i][j];
invalidRoom = items.getSameTile(currTile);
}
}
}
}
items.get(name).moveBy(currTile); //Move weapon to new position
}
/**
* Searches given string array for the given string(findMe)
* @param stringArray an array of strings to search through
* @param findMe string to find within the array of strings
* @return true if string is found
*/
public Boolean findInStringArray(String[] stringArray, String findMe) {
for (String currentString : stringArray) {
if (currentString.equalsIgnoreCase(findMe) || currentString.replaceAll("\\s+",
"").equalsIgnoreCase(findMe)) {
return true;
}
}
return false;
}
/**
* Used to end the game by accusing a token of committing a crime using a specific weapon
* inside a given room
* Will end the game if the accusation is correct but will remove the player from play if
* he/she is wrong.
*
* @param accuseString user's input string eg : "accuse white dagger hall"
* @param player the player that is accusing.
* @return A boolean value of true or false, false if the input is in an invalid format and
* true if the user is either right or wrong.
*/
public Boolean accuse(String accuseString, Player player) {
// Divide the input into multiple words by splitting them by ' ' (spaces)
String[] collection = accuseString.split(" ");
try {
// Divides the string into name, weapon and room and handles when the inputs have
// spaces in them.
int i = 1;
String name = collection[i++];
String weapon, room;
if (collection[i + 1].equalsIgnoreCase("stick") || collection[i + 1].equalsIgnoreCase(
"pipe")) {
weapon = collection[i++] + collection[i++];
} else {
weapon = collection[i++];
}
if (collection.length == i + 2) {
room = collection[i] + collection[i + 1];
} else {
room = collection[i];
}
// See if the inputs are valid
if (findInStringArray(Card.suspects, name)) {
if (findInStringArray(Card.weapons, weapon)) {
if (findInStringArray(Card.rooms, room)) {
// See if the user's accusation was right
if (murderEnvelope.get(0).toString().replaceAll("\\s+",
"").equalsIgnoreCase(name)
&& murderEnvelope.get(1).toString().replaceAll("\\s+",
"").equalsIgnoreCase(weapon)
&& murderEnvelope.get(2).toString().replaceAll("\\s+",
"").equalsIgnoreCase(room)) {
// Inputs are valid and the user's accusation is correct.
finishGame(player);
} else {
// User got it wrong, end his turns and remove him from the game.
player.killPlayer();
numofPlayers--;
JOptionPane.showMessageDialog(null,
"Oops, that was a wrong suggestion, you're now out of the "
+ "game, you are now only allowed to answer "
+ "questions!");
return true;
}
} else {
ui.displayString("Could not find the room:" + room
+ ", are you sure you spelled it correctly?");
}
} else {
ui.displayString("Could not find the weapon:" + weapon
+ ", are you sure you spelled it correctly?");
}
} else {
ui.displayString("Could not find the token:" + name
+ ", are you sure you spelled it correctly?");
}
} catch (Exception ex)
{
// Invalid format for input
ui.displayString(
"Unknown input format, make sure you it is in the following format: \n'accuse "
+ "(name) (weapon) (room) without()!");
}
return false;
}
public void playerTeleport(String token, int room) {
int side = 1; //1 = player's array, 2 = dummy's array
Tile currTile = null;
boolean invalidRoom = true;
//Check which array the token is in
if (players.hasTokenName(token)) {
side = 1;
currTile = players.getPlayer(token).getToken().getPosition();
} else if (dummies.hasTokenName(token)) {
side = 2;
currTile = dummies.getPlayer(token).getToken().getPosition();
}
//Ensures that two tokens are on the same tile
if (currTile.getRoom() != room) {
//Looks for the position in which a weapon is positioned
for (int i = 0; i < grid.map.length; i++) {
//Quick escape from nested for loop if the value is found early.
if (!invalidRoom) {
break;
}
for (int j = 0; j < grid.map[i].length; j++) {
if (grid.map[i][j].getSlot() == 5 && grid.map[i][j].getRoom() == room
&& invalidRoom) {
currTile = grid.map[i][j];
invalidRoom = players.getSameTile(currTile);
if (!invalidRoom) {
invalidRoom = dummies.getSameTile(currTile);
}
}
}
}
}
//Move according to what array token is in
if (side == 1) {
players.getPlayer(token).getToken().moveBy(currTile);
} else {
dummies.getPlayer(token).getToken().moveBy(currTile);
}
}
/**
* Method that calls the class that handles the frame that is shown when the game has ended.
* Shows messages signifying the importance of the person's victory and prevents inputs other
* than quit
*/
public void finishGame(Player player) {
// Clear information panel
ui.clearContent();
// Add celebratory contents to it
ui.displayString("YOU'VE WON!\nCongrats " + player.getName()+"[" +player.getToken().getTokenName() + "]" + "!\nYOU'VE WON!\nCongrats "
+ player.getName()+"[" +player.getToken().getTokenName() + "]" + "!\nYOU"
+ "'VE WON!\nCongrats " + player.getName()+"[" +player.getToken().getTokenName() + "]" + "!\nYOU'VE WON!\nCongrats "
+ player.getName() + "!\nYOU'VE "
+ "WON!\nCongrats!\n");
// Call Class that handles the celebration frame.
new Congrats(player.getName() +"[" +player.getToken().getTokenName() + "]");
CommandPanel.updateCommands();
CommandPanel.updateMovesReamining(-1);
// Ensure user can only exit from here on.
String input = ui.getCommand();
while (!input.equalsIgnoreCase("quit")) {
input = ui.getCommand();
}
System.exit(0);
}
}