-
Notifications
You must be signed in to change notification settings - Fork 0
/
Woo.java
401 lines (351 loc) · 13 KB
/
Woo.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
import cs1.Keyboard;
public class Woo
{
private int countMoves;
private boolean gameRunning;
// list of animals that exist
public static final String[] ANIMALS = {"Bear", "Elephant", "Giraffe", "Lion", "RedPanda"};
public Woo()
{
countMoves = 4;
gameRunning = true;
}
public static void printWelcome()
{
System.out.println("\nWelcome to the Animal Circus!\n");
System.out.println("You are the owner of the circus.\nUse your time and money wisely to buy and train animals.\n" +
"Position your animals in the circus so that you can earn money and fame.\n\nHAVE FUN!!!\n");
}
public void printMoves()
{
System.out.println("You have " + countMoves + " actions left.");
}
public static String listAnimals()
{
String retStr = "";
for (int i = 0; i < ANIMALS.length; i += 1)
{
retStr += "\t" + (i + 1) + ") " + ANIMALS[i] + "\n";
}
return retStr;
}
public static void attemptPurchase(Woo someGame, Player somePlayer, Animal someAnimal, GameBoard someBoard)
{
System.out.println();
System.out.println(someAnimal);
System.out.println();
System.out.println("Confirm purchase:");
System.out.println("\t" + "1) Yes.");
System.out.println("\t" + "2) No.");
// user input
System.out.print("Selection: ");
int confirmation = Keyboard.readInt();
// while invalid number is chosen
while (confirmation != 1 && confirmation != 2) {
System.out.println ("Invalid number selected. Please enter a valid number for confirmation.");
System.out.print("Selection: ");
confirmation = Keyboard.readInt();
}
// if when user confirms purchase
if (confirmation == 1)
{
System.out.println(prettySpacing(100));
// buy animal
if (somePlayer.buyAnimal(someAnimal))
{
// if purchase was successfully, decrement countMoves
someGame.countMoves -= 1;
// add animal and its shape to each ArrayList
someBoard.getRoster().add(someAnimal);
someBoard.getRosterShapes().add(someAnimal.getShape());
}
}
// if when user does not confirm purchase
else
{
System.out.println(prettySpacing(100));
System.out.println("No purchase made.");
}
}
// used to make terminal display nicer
public static String prettySpacing(int x)
{
String retStr = "";
for (int i = 0; i < x; i += 1)
{
retStr += "\n";
}
return retStr;
}
public static void main(String[] args)
{
String spacing = prettySpacing(100);
System.out.println(spacing);
printWelcome();
// creates new instances of each class, and takes user input for name
Woo newGame = new Woo();
System.out.print("What is your name?" + "\n\t> ");
Player newPlayer = new Player(Keyboard.readString());
Circus newCircus = new Circus();
GameBoard newBoard = new GameBoard();
System.out.println(spacing);
System.out.println("Hello " + newPlayer.getName() + "!\n");
System.out.println(newPlayer);
while (newGame.gameRunning)
{
while (newGame.countMoves > 0)
{
System.out.println();
newGame.printMoves();
System.out.println();
System.out.println("What would you like to do?");
System.out.println("\t" + "1) Buy and train animals.");
System.out.println("\t" + "2) Skip preparation and start the Animal Circus!");
// user input
System.out.print("Selection: ");
int prepOrCircus = Keyboard.readInt();
// while invalid input is chosen
while (prepOrCircus != 1 && prepOrCircus != 2) {
System.out.println ("Invalid input. Please enter a valid number for selection.");
System.out.print("Selection: ");
prepOrCircus = Keyboard.readInt();
}
// if when user wants to buy or train animals
if (prepOrCircus == 1)
{
System.out.println(spacing);
System.out.println("Would you like to buy or train an animal?");
System.out.println("\t" + "1) Buy!");
System.out.println("\t" + "2) Train!");
// user input
System.out.print("Selection: ");
int buyOrTrain = Keyboard.readInt();
// while invalid input is chosen
while (buyOrTrain != 1 && buyOrTrain != 2) {
System.out.println ("Invalid input. Please enter a valid number for selection.");
System.out.print("Selection: ");
buyOrTrain = Keyboard.readInt();
}
// if when user wants to buy an animal
if (buyOrTrain == 1)
{
System.out.println(spacing);
System.out.println("Which animal would you like to buy?");
System.out.print(listAnimals());
// user input
System.out.print("Selection: ");
int animalNum = Keyboard.readInt();
// while invalid input is chosen
while (animalNum != 1 && animalNum != 2 && animalNum != 3 && animalNum != 4 && animalNum != 5) {
System.out.println ("Invalid input. Please enter a valid number for selection.");
System.out.print("Selection: ");
animalNum = Keyboard.readInt();
}
System.out.println(spacing);
// when specific animal is chosen, attempts purchase
if (animalNum == 1)
{
attemptPurchase(newGame, newPlayer, new Bear(), newBoard);
}
else if (animalNum == 2)
{
attemptPurchase(newGame, newPlayer, new Elephant(), newBoard);
}
else if (animalNum == 3)
{
attemptPurchase(newGame, newPlayer, new Giraffe(), newBoard);
}
else if (animalNum == 4)
{
attemptPurchase(newGame, newPlayer, new Lion(), newBoard);
}
else
{
attemptPurchase(newGame, newPlayer, new RedPanda(), newBoard);
}
}
// if when user wants to train the animals
else
{
// if the user owns 1+ animals
if (newPlayer.getOwnedAnimals().size() > 0)
{
System.out.println(spacing);
System.out.println("Which animal would you like to train?");
System.out.println(newPlayer.listOwnedAnimals());
// user input
System.out.print("Selection: ");
int trainNum = Keyboard.readInt();
// while invalid input is chosen
while (trainNum > newPlayer.getOwnedAnimals().size() || trainNum < 1) {
System.out.println ("Invalid input. Please enter a valid number for selection.");
System.out.print("Selection: ");
trainNum = Keyboard.readInt();
}
if (newPlayer.getOwnedAnimals().size() >= trainNum)
{
int isTrained = newPlayer.trainAnimal(newPlayer.getOwnedAnimals().get(trainNum - 1));
if (isTrained == 1)
{
// update countMove
newGame.countMoves -= 1;
System.out.println(spacing);
System.out.println("\nSuccessfully trained!");
}
else
{
System.out.println(spacing);
System.out.println("\nThis animal was already trained!");
System.out.println("\nNo actions deducted.");
}
}
else
{
System.out.println();
System.out.println("Invalid input. Please enter a valid number for selection.");
}
}
// if the user owns 0 animals
else
{
System.out.println(spacing);
System.out.println("You do not own any animals!");
}
}
}
// if when user wants to go to the animal circus
else if (prepOrCircus == 2)
{
// if user owns more than 1 animal, go to the circus
if (newPlayer.getOwnedAnimals().size() > 0)
{
break;
}
// if user owns no animals
else
{
System.out.println(spacing);
System.out.println("Please buy an animal before starting the circus.");
}
}
else
{
System.out.println();
System.out.println("Invalid input. Please enter a valid number for selection.");
}
} // end while
System.out.println(spacing);
// if user has no more moves left to buy/train animals
if (newGame.countMoves == 0)
{
System.out.println("You are out of actions.\nIt's time to go to the circus!");
}
System.out.println();
System.out.println("This is your circus, try to fit as many animals as possible:\n");
int i = 0;
while (i < newBoard.getRoster().size()){
System.out.println(newBoard.boardState(i));
System.out.println("What would you like to do?");
System.out.println("\t1) Place this animal");
System.out.println("\t2) Skip to next animal");
System.out.println("\t3) End the circus right now");
// user input
System.out.print("Selection: ");
int placeAnimal = Keyboard.readInt();
// while invalid input is chosen
while (placeAnimal != 1 && placeAnimal != 2 && placeAnimal != 3)
{
System.out.println ("Invalid input. Please enter a valid number for selection.");
System.out.print("Selection: ");
placeAnimal = Keyboard.readInt();
}
// if when user wants to place the animal on the board
if (placeAnimal == 1)
{
System.out.println(spacing);
System.out.println("The top left corner is considered (0, 0)\n");
System.out.println(newBoard.boardState(i));
// user input
System.out.println("Please input the X and Y coordinates of where to place");
System.out.print("X coordinate: ");
int x = Keyboard.readInt();
System.out.print("Y coordinate: ");
int y = Keyboard.readInt();
// if when the animal is successfully placed on the board
if (newBoard.addAnimal(newBoard.getRoster().get(i), y, x)){
System.out.println(spacing);
System.out.println(newBoard.getRoster().get(i).getName() + " successfully placed at (" + x + ", " + y + ").\n");
i += 1;
}
// if when the animal does not fit
else{
System.out.println(spacing);
System.out.println("It doesn't fit. Try again.\n");
}
}
// if when user wants to skip animal
else if (placeAnimal == 2)
{
System.out.println(spacing);
System.out.println(newBoard.getRoster().get(i).getName() + " has been skipped.\n");
i += 1;
}
// if when user wants to end the circus
else
{
System.out.println(spacing);
System.out.println("Circus has been ended.");
break;
}
}
// if when there are no more animals to place on the board
if (i == newBoard.getRoster().size())
{
System.out.println(spacing);
System.out.println("\nNo more animals left to place. Printing circus results...");
}
// prints out results of the circus
System.out.println("\nHow your circus looked:\n" + newBoard);
newCircus.results(newPlayer);
newCircus.setAmtEarned();
System.out.println(newPlayer.newBalance(newCircus));
System.out.println("\nWould you like to prepare for the next circus?");
System.out.println("\t1) Yes");
System.out.println("\t2) No, show me the final results");
// user input
System.out.print("Selection: ");
int keepPlaying = Keyboard.readInt();
System.out.println(spacing);
// while invalid input is chosen
while (keepPlaying != 1 && keepPlaying != 2)
{
System.out.println ("Invalid input. Please enter a valid number for selection.");
System.out.print("Selection: ");
keepPlaying = Keyboard.readInt();
}
// if when user wants to continue the game
if (keepPlaying == 1)
{
newGame.countMoves = 4;
newBoard.expandBoard(1);
newBoard.reset();
System.out.println(spacing);
}
// if when user wants to stop playing
if (keepPlaying == 2)
{
newGame.gameRunning = false;
}
} // end while
// end of game message + updates
System.out.println("Thanks for playing!\n");
String owned = "|";
for (Animal someAnimal : newPlayer.getOwnedAnimals())
{
owned += someAnimal.getName() + "|";
}
System.out.println("Animals you owned: " + owned);
System.out.println(newPlayer);
System.out.println("\n");
} // end main
} // end class