-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStartUp.java
More file actions
316 lines (256 loc) · 10.9 KB
/
Copy pathStartUp.java
File metadata and controls
316 lines (256 loc) · 10.9 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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
/**
* A class that takes in player information before the game play starts
*
* @Team MAGA
* @Author Gajun Young - 16440714
* @Author Royal Thomas - 16326926
* @Author Richard Otroshchenko - 16353416
*/
public class StartUp {
private CluedoUI ui;
private enum Token {PLUM, WHITE, SCARLET, GREEN, MUSTARD, PEACOCK}
private ArrayList<Card> murderEnvelope = new ArrayList<>();
public StartUp(CluedoUI ui) {
this.ui = ui;
}
/**
* A method that checks if a string contains a number
*
* @return true = string consists of numbers only, false = string consists of non-numbers
*/
public static boolean isNum(String str) {
if (str.equals("")) {
return false;
}
for (char c : str.toCharArray()) {
if (!Character.isDigit(c)) return false;
}
return true;
}
/**
* Asks the users, the number of players playing the game
*/
public int size() {
String size; //Holds the size of the players
ui.displayString("======GAME SETUP [CAPACITY]======");
ui.displayString("Enter the number of players: [min: 2, max: 6]");
//Ensures choice is within the [min, max] range
do {
//Checks if size contains numbers only
do {
size = ui.getCommand();
ui.displayString(size);
if (!isNum(size)) { //Error message
ui.displayString("\'" + size + "\'" + " is not an integer.....");
}
} while (!isNum(size));
if (Integer.parseInt(size) < 2 || Integer.parseInt(size) > 6) { //Error message
ui.displayString("Enter a valid integer between [2 - 6].....");
}
} while (Integer.parseInt(size) < 2 || Integer.parseInt(size) > 6);
return Integer.parseInt(size);
}
/**
* Finds name and choice of player and sets their token
*/
public void addPlayers(Players players) {
String name; //Name of player
String verifyName; //Y/N to the current name
String choice; //The token they want to choose
ui.displayString("=====GAME SETUP [Name & Token]=====");
//Iterates through each player and allows each one to choose their name and token
for (int i = 0; i < players.getCapacity(); i++) {
ui.displayString("======" + "PLAYER " + (i + 1) + "======");
ui.displayString("Player " + (i + 1) + "'s name: ");
//Acquire players name
do {
name = ui.getCommand();
} while (name.equals(""));
do {//Ensures if the name they choose, is the name they want
ui.displayString("\'" + name + "\'" + ", Are you sure about this name? [Y/N]");
verifyName = ui.getCommand();
if (verifyName.equalsIgnoreCase("N")) { //Allows users to change name
ui.displayString("Player " + (i + 1) + ", please choose a new name:");
name = ui.getCommand();
}
} while (!verifyName.equalsIgnoreCase("Y"));
//Character choice text
ui.displayString("Player " + (i + 1) + "(" + name + "), " + " Please choose a character");
int j = 0;
//Method to show only the character options available to the user.
for (Token p : Token.values()) {
j++;
boolean playerExists = false;
for (Player x : players) { //Ensures there's no two players having the same token
if (!x.hasChoice(j)) {
playerExists = true;
}
}
if (!playerExists) {
ui.displayString(j + ". " + p.toString());
}
}
boolean valid = true;
do { //Ensures valid choice
do { //Ensures choice is a number
choice = ui.getCommand();
ui.displayString("Player " + (i + 1) + ": " + choice);
if (!isNum(choice)) { //Error message for non numbers
ui.displayString("\'" + choice + "\'" + " is not a valid choice.");
}
} while (!isNum(choice));
//Ensures there is no two players having the same token
for (Player p : players) {
valid = p.hasChoice(Integer.parseInt(choice));
if (!valid) {
ui.displayString("Character unavailable, please retry.");
break;
}
}
if (Integer.parseInt(choice) > 6 || Integer.parseInt(choice)
< 1) { //Error message for out of bound
ui.displayString("Character unavailable, please retry.");
}
} while ((Integer.parseInt(choice) > 6 || Integer.parseInt(choice) < 1) || !valid);
//Create the players & give them tokens
players.createPlayers(name, Integer.parseInt(choice));
players.createTokens(i);
}
}
//Getter
public ArrayList<Card> getMurderEnvelope() {
return murderEnvelope;
}
/**
* Method to divide the stack of cards among the players and choose three for the murder
* envelope
*
* @param players an input of the current list of players of type Players
* @return nothing
*/
public void divideCards(Players players) {
ArrayList<Card> tokenList = new ArrayList<Card>(); // Arraylist to store the entire deck
Random rand = new Random(); // To pick out random cards from the deck
/* ArrayLists of characters, weapons and rooms to pick a random from each to store into
murder envelope */
ArrayList<String> characters = new ArrayList<>(
Arrays.asList("Mustard", "Plum", "Green", "Peacock",
"Scarlet", "White"));
ArrayList<String> weapons = new ArrayList<>(
Arrays.asList("Dagger", "Candle Stick", "Revolver", "Rope", "Lead Pipe",
"Spanner"));
ArrayList<String> rooms = new ArrayList<>(
Arrays.asList("Hall", "Lounge", "Dining Room", "Kitchen", "Ball Room",
"Conservatory",
"Billiard Room", "Library", "Study"));
// Pick out cards randomly for murder envelope
Card characterChosen = new Card(characters.get(rand.nextInt(characters.size())), 1);
Card weaponChosen = new Card(weapons.get(rand.nextInt(weapons.size())), 1);
Card roomChosen = new Card(rooms.get(rand.nextInt(rooms.size())), 1);
// Put them into the envelope
murderEnvelope.addAll(Arrays.asList(characterChosen, weaponChosen, roomChosen));
// Remove selected cards in the envelope from the main deck
characters.remove(characterChosen.getName());
weapons.remove(weaponChosen.getName());
rooms.remove(roomChosen.getName());
// Add the remaining card to the ArrayList to divide among players.
for (String x : characters) {
tokenList.add(new Card(x, 1));
}
for (String x : weapons) {
tokenList.add(new Card(x, 2));
}
for (String x : rooms) {
tokenList.add(new Card(x, 3));
}
// Divides cards evenly among players
while (tokenList.size() >= players.getCapacity()) {
for (int i = 0; i < players.getCapacity(); i++) {
Card chosenCard = tokenList.get(rand.nextInt(tokenList.size()));
players.getPlayer(i).giveCard(chosenCard);
tokenList.remove(chosenCard);
}
}
// Show the cards that didn't get divided to the user in the GUI
InformationPanel.updateRemainingCards(tokenList);
//Sets every players notebook
for(int i = 0; i < players.getCapacity(); i++) {
players.getPlayer(i).setNoteBook(tokenList);
}
}
/*
* Player with highest roll moves first
* The rest of the players are ordered in terms of who entered their names first
*/
public void position(Players players) {
Dice dice = new Dice();
boolean unique = false; //Unique highest roll
int[] pos = new int[players.getCapacity()]; //Saves the roll of each player
int position = 0; //memorises position of highest roller
int highest = 0; //memorises highest roll
int diff = 0; //Keeps track of highest after everyone rolls
ui.displayString("=====GAME SETUP [HIGHEST ROLL]=====");
while(!unique) {
//Rolling dice
for(int i = 0; i < players.getCapacity(); i++) {
//Only roll for players who hasn't rolled or with highest rolls
if(pos[i] == diff) {
dice.rollDice();
pos[i] = diff + dice.getRoll1() + dice.getRoll2(); //Sum of dice for the ith player
//Draw dice image onto the screen
ui.drawDice(dice.getRoll1(), dice.getRoll2());
ui.displayString(players.currPlayer(i) + " rolled " + (dice.getRoll1() + dice.getRoll2()));
ui.display();
try { //The resulting dice roll is onscreen for 2 seconds
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
ui.drawDice(0,0); //This hides the dice
ui.display();
//Records highest roll and position
if(highest < pos[i]) {
highest = pos[i];
position = i;
unique = true;
}else if(highest == pos[i]) {
//if two or more players have the same highest roll,
//there is no set highest position -> they have to reroll
unique = false;
}
}
}
if(!unique) {
ui.displayString("Players with the same highest roll value,\nhas to re-roll");
}
diff = highest; //ensures re-roll will not be the same as the lower numbers
}
//Reposition players into their new order
ui.displayString("===" + players.currPlayer(position) + " HIGHEST ROLL===");
players.addFirst(position, players.getPlayer(position));
}
/**
* Add dummies if the amount of players is less than 6
* @param players
* @param dummies
*/
public void addDummies(Players players, Players dummies) {
//Create a default set of tokens for dummies
for(int i = 0; i < dummies.getCapacity(); i++) {
dummies.createPlayers("dummy", i+1);
dummies.createTokens(i);
}
//Remove dummy tokens that players already have
for(int i = 0; i < players.getCapacity(); i++) {
for(int j = 0; j < dummies.getCapacity(); j++) {
if(players.getTokenName(i).equals(dummies.getTokenName(j))) {
dummies.remove(j);
break;
}
}
}
}
}