-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWagon.java
More file actions
406 lines (379 loc) · 10.3 KB
/
Wagon.java
File metadata and controls
406 lines (379 loc) · 10.3 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
// SHOOTING MINIGAME!
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
public class Wagon{
private int distanceTraveled;
private int speedBoost;
private int food;
private int health;
private int turnCount;
private int choice;
private int foodPerTurn;
private int wagonSpeed;
private boolean done;
private ArrayList<Integer> highscores;
private String toWrite;
private boolean gun;
private int foodmultiplier;
private boolean boxer;
private boolean kidney;
private int numPeople;
private int doctorRejections;
private String name;
private int player;
private ArrayList<Item> store;
private ArrayList<Item> inv;
private Item oven;
public Wagon(){
done = false;
speedBoost = 1;
turnCount = 0;
distanceTraveled = 0;
food = 10;
health = 10;
foodPerTurn = 0;
wagonSpeed = 5;
highscores = new ArrayList<Integer>();
toWrite = new String();
foodmultiplier = 5;
kidney = false;
numPeople = 1;
doctorRejections = 0;
boxer = false;
store = new ArrayList<Item>();
inv = new ArrayList<Item>();
HealthGain sandwich = new HealthGain(2, 2, "sandwich");
HealthGain milk = new HealthGain(1, 1, "expired milk");
SpeedBoost coke = new SpeedBoost(8, 2, "strange white powder");
oven = new Item(2, false, "oven");
store.add(sandwich);
store.add(milk);
store.add(oven);
store.add(coke);
}
public static void main (String [] args){
System.out.println("You are on a wagon.");
System.out.println("\n\n");
Wagon game = new Wagon();
game.play();
}
public void play(){
intro();
Missions.espionage(player);
while(!done){
turnCount++;
food -= numPeople;
playTurn();
randomEvent();
checkDone();
}
}
public void intro(){
System.out.println("Who are you?");
System.out.println("(1) Jockey");
System.out.println("(2) Chef");
System.out.println("(3) Boxer");
System.out.println("(4) Tank");
player = Prompt.getInt("-> ",1,4);
switch(player){
case 1: wagonSpeed += 3; break;
case 2: foodmultiplier += 2; break;
case 3: boxer = true; break;
case 4: health = 20; break;
default:
}
}
public void playTurn(){
System.out.printf("You have traveled %2d miles, have %2d health, %3d food, and %2d horses%n", distanceTraveled, health, food, wagonSpeed);
System.out.printf("Inventory: %s", inv);
System.out.println();
if(food < 1){
lowFood();
}
System.out.println("(1) Go forward.");
System.out.println("(2) Get food.");
System.out.println("(3) Use item.");
System.out.println("(4) Cook.");
choice = Prompt.getInt("\n\nWhat do you want to do -> ");
switch(choice){
case 1: forward(); break;
case 2: food(); break;
case 3: use(); break;
case 4: cook(); break;
default:
}
}
public void lowFood(){
System.out.println("You should really get some food soon");
if(food < 0){
System.out.println("You lose health for not eating.");
health += food;
}
if(food < -3){
System.out.println("One of your horses gets mad because you have no food and runs away with your wagon. You spend three turns trying to catch it.");
turnCount += 3;
}
}
public void forward(){
int subtotal = (int)(Math.random() * wagonSpeed * speedBoost) + 1;
System.out.printf("%nYou travel %1d miles.%n", subtotal);
distanceTraveled += subtotal;
}
public void food(){
int subtotal = (int)(Math.random() * 5) + 1;
food += subtotal;
System.out.println("\nYou get " + subtotal + " food\n");
}
public void use(){
int choice = Prompt.getInt("Index of item to be used\n->");
Item toUse = inv.get(choice);
if(toUse instanceof HealthGain){
health += ((HealthGain)toUse).use();
}
else if(toUse instanceof SpeedBoost){
speedBoost += ((SpeedBoost)toUse).use();
}
inv.remove(choice);
}
public void randomEvent(){
System.out.println("\n");
int random = (int)(Math.random() * 12);
switch(random){
case 1:
clumsy(); break;
case 2:
goats(); break;
case 3:
//kickedByHorse(); break;
case 4:
horseSalesmen(); break;
case 5:
doctor(); break;
case 6:
if(!gun){
guns();
}
break;
case 7:
doctor(); break;
case 8:
if(!gun){
others(); break;
}
case 9:
kidneys(); break;
case 10:
store(); break;
case 11:
if(turnCount > 50)
Missions.espionage(player);
break;
default:
break;
}
newLine();
}
public void newLine(){
System.out.println();
System.out.print("\n----------------------------------------------\n");
System.out.println();
}
public void checkDone(){
if(distanceTraveled > 100){
System.out.println("YOU WON IN " + turnCount + " TURNS!");
highScore();
done = true;
}
if(health < 0){
System.out.println("YOU DIED IN " + turnCount + " TURNS!");
done = true;
}
}
public void clumsy(){
System.out.println("You fall of your wagon losing 2 health.");
health -= 2;
}
public void goats(){
System.out.println("A herd of goat attack your wagon.");
System.out.println("(1) Bribe them away with food.");
System.out.println("(2) FIGHT THE GOATS.");
choice = Prompt.getInt("\n\n What do you want to do -> ");
switch(choice){
case 1:
if(food > 4){
System.out.println("You give the goats 5 food and they leave."); food -= 5;
break;
}else{
System.out.println("You don't have enough food.");
}
case 2:
if(boxer){
System.out.println("Being a boxer, you punch one of the goats in the face and the rest run away.");
break;
}else if(gun){
System.out.println("You're gun makes quick work of the goats.");
health -= 3;
break;
}else{
System.out.println("You lose 5 health fighting the goats.");
health -= 5;
break;
}
default:
}
}
public void kickedByHorse(){
System.out.println("One of your horses gets mad and kicks you.");
health -= 1;
}
public void horseSalesmen(){
int choice = Prompt.getInt("You chance upon a traveling horse salesmen. Would you like to by a horse for 5 food? \n (1) yes \n (2) no \n ->");
if(choice == 1){
if(food < 5){
System.out.println("You can't afford a horse. The salesmen punches you for wasting his time.");
health--;
}
if(food > 5){
System.out.println("You attach the horse to your wagon.");
wagonSpeed++;
food -= 5;
}
}
}
public void doctor(){
int choice = Prompt.getInt("You chance upon a traveling doctor. Would you like to heal completely for 5 food? \n (1) yes \n (2) no \n ->");
if(choice == 1){
if(food < 5){
System.out.println("You can't afford that. The doctor punches you for wasting his time.");
health--;
}
if(food > 5){
System.out.println("You replenish your health.");
health = 10;
food -= 5;
doctorRejections = 0;
}
}
if(choice == 2){
doctorRejections++;
if(doctorRejections > 3){
System.out.println("The doctor gets mad at you for always rejecting his services");
System.out.println("He sends three waves of goat at you");
goats();
goats();
goats();
doctorRejections = 0;
}
}
}
public void guns(){
int choice = Prompt.getInt("You chance upon a traveling gun salesmen. Would you like to buy a gun for 15 food? \n (1) yes \n (2) no \n ->");
if(choice == 1){
if(food < 15){
System.out.println("You can't afford that. The gun salesmen punches you for wasting his time.");
health--;
}
if(food > 15){
System.out.println("You buy a gun.");
gun = true;
food -= 15;
}
}
if(choice == 2){
}
}
public void others(){
int choice = Prompt.getInt("While on your wagon, someone waves you down and asks for a ride. Do you let him on? \n (1) yes \n (2) no \n->");
if(choice == 1){
if(food > 15){
System.out.println("The hungry hitchhiker drools at the site of all your food. He then steals your kidney, takes some food, and runs away.");
health -= 2;
food -= 2;
kidney = true;
}
if(food < 15){
System.out.println("The hitchhiker gives you the rest of his horses and his gun.");
wagonSpeed += 3;
foodPerTurn++;
gun = true;
numPeople++;
}
}
}
public void kidneys(){
if(kidney){
System.out.println("YOU SEE THE GUY WHO STOLE YOUR KIDNEY EARLIER!");
System.out.println("He says, 'hey man I really bad about the whole kidney thing, here's some food.'");
food += 5;
}
}
public void store(){
System.out.println("YOU FIND A WALMART!");
System.out.println("This is what they have.");
System.out.println("(0) nothing");
for(int i = 0; i < store.size(); i++){
System.out.printf("(%d) %4s%n", i + 1, store.get(i));
}
int choice = Prompt.getInt("What do you want?\n->", -1, store.size());
if(choice == 0)
return;
Item temp = store.get(choice - 1);
inv.add(temp);
food -= temp.cost;
store.remove(choice - 1);
}
public void cook(){
if(!inv.contains(oven)){
System.out.println("You don't have anything to cook with.");
return;
}
int choice1 = Prompt.getInt("Index of item 1 -> ", 1, inv.size());
int choice2 = Prompt.getInt("Index of item 2 -> ", 2, inv.size());
System.out.println("You end up burning yourself.");
health -= 1;
}
public void highScore(){
readToString();
writeString();
printScores();
}
public void readToString(){ // Opens the file and gets the next line
Scanner fromfile = OpenFile.open("highscores.txt");
highscores.add(turnCount);
while(fromfile.hasNext()){
highscores.add(fromfile.nextInt());
}
Sorts.insertionSort(highscores);
for(int i = 0; i < highscores.size(); i++){
toWrite += highscores.get(i);
toWrite += "\n";
}
}
public void writeString(){ // Writes toWrite to the file
try{
File newfile = new File("highscores.txt");
PrintWriter writer = new PrintWriter(newfile);
writer.print(toWrite);
writer.close();
}catch(IOException e){
System.out.println("Could not write to the file, sorry.");
System.exit(1);
}
}
public void printScores(){
boolean yourscore = true;
System.out.println("\n\n HIGH SCORES \n\n");
for(int i = 0; i < highscores.size(); i++){
if(highscores.get(i) == turnCount && yourscore){
System.out.printf("%n%3d %5d%n", i + 1, highscores.get(i));
yourscore = false;
}else{
System.out.printf("%n%3d %5d%n", i + 1, highscores.get(i));
}
}
System.out.println();
}
}