-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAdventure2.java
More file actions
458 lines (412 loc) · 13 KB
/
Adventure2.java
File metadata and controls
458 lines (412 loc) · 13 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
import java.util.*;
import java.io.*;
public class Adventure2 {
// Do not edit attributes
private Hero hero;
private Scanner keyboard;
public static void main(String[] args) throws IOException {
Adventure2 adventure = new Adventure2();
System.out.println("***Gayanga Madusha Hewathudallage 20342398***");
System.out.println("*** Adventure Part 2 ***");
adventure.run();
}
public Adventure2() {
keyboard = new Scanner(System.in);
hero = null;
}
public void run() throws IOException {
int choice = -1;
while (choice != 0) {
displayMenu();
System.out.print("Enter choice >> ");
choice = keyboard.nextInt();
System.out.println();
keyboard.nextLine();
process(choice);
}
}
private void displayMenu() {
System.out.println();
System.out.println("~~~ ADVENTURE 2 MENU ~~~");
System.out.println("1. Create Hero");
System.out.println("2. Display Hero");
System.out.println("3. Heal Hero");
System.out.println("4. Single Battle");
System.out.println("5. Battle from List");
System.out.println("6. Save Hero to File");
System.out.println("7. Load Hero from File");
System.out.println("0. Quit");
}
private void process(int choice) {
if (choice == 1) {
createHero();
} else if (choice == 2) {
displayHero();
} else if (choice == 3) {
healHero();
} else if (choice == 4) {
singleBattle();
} else if (choice == 5) {
battleFromList();
} else if (choice == 6) {
saveHero();
} else if (choice == 7) {
loadHero();
}
}
private void healHero() {
if (this.hero != null) {
if (this.hero instanceof Mage) {
System.out.println("Do you want to use Magic Heal?");
try {
char inp = readYNChar();
if (inp == 'Y') {
((Mage) hero).magicHeal();
System.out.println("Magic Heal Used");
} else if (inp == 'N') {
normalHealHero();
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
} else {
normalHealHero();
}
} else {
System.out.println("No hero exists");
}
}
private void normalHealHero() {
System.out.println("What size of potion would you like to use?");
System.out.println("1. Small");
System.out.println("2. Medium");
System.out.println("3. Large");
try {
int inp = readInt(1, 3);
if (hero.usePotion(inp)) {
System.out.println("Portion Consumed");
} else {
System.out.println("This portion is not available");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
private void giveHeroExperience() {
if (this.hero != null) {
System.out.println("How much experience do you want to give the hero <0-100000>?");
try {
int inp = readInt(0, 100000);
if (hero.gainExperience(inp)) {
System.out.println("Hero has leveled up!");
} else {
System.out.println("Hero has recieved some experience");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
} else {
System.out.println("No hero exists");
}
}
private void damageHero() {
if (this.hero != null) {
System.out.println("How much do you want to damage the hero <0-100000>?");
try {
int inp = readInt(0, 100000);
if (hero.takeDamage(inp)) {
System.out.println("Hero has taken damage");
} else {
System.out.println("Hero has fainted");
System.out.println("You will need a new Hero!");
this.hero = null;
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
} else {
System.out.println("No hero exists");
}
}
private void saveHero() {
if (this.hero != null) {
File dataDir = new File("data");
if (!dataDir.exists()) {
System.out.println("Directory does not exist. Will be created");
dataDir.mkdir();
}
File heroFile = new File("data/hero.bin");
try {
PrintWriter outfile = new PrintWriter(heroFile);
outfile.println(hero.getClass().getName());
outfile.println(hero.getHealth());
outfile.println(hero.getLevel());
outfile.println(hero.getExperience());
outfile.println(hero.getMaxHealth());
outfile.println(hero.getNextLevelExperience());
outfile.println(hero.getInventory().getSmallPotions());
outfile.println(hero.getInventory().getMediumPotions());
outfile.println(hero.getInventory().getLargePotions());
if (hero instanceof Archer) {
outfile.println(((Archer) hero).getBonusExp());
}
if (hero instanceof Mage) {
outfile.println(((Mage) hero).getHealRate());
}
outfile.close();
System.out.println("Hero saved");
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
} else {
System.out.println("No Hero exists");
}
}
private void loadHero() {
if (this.hero != null) {
System.out.println("Do you want to overwrite the current Hero? Y\\N ");
try {
char inp = readYNChar();
if (inp == 'Y') {
loadNewHero();
} else if (inp == 'N') {
System.out.println("Current Hero remains.");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
} else {
loadNewHero();
}
}
private void loadNewHero() {
File dataDir = new File("data");
if (!dataDir.exists()) {
System.out.println("Data directory does not exist");
} else {
File heroFile = new File("data/hero.bin");
if (!heroFile.exists()) {
System.out.println("Hero save file does not exist");
} else {
try {
Scanner in = new Scanner(heroFile);
String type = in.nextLine();
int health = in.nextInt();
int level = in.nextInt();
int experience = in.nextInt();
int maxHealth = in.nextInt();
int nextLevelExperience = in.nextInt();
int smallPotions = in.nextInt();
int mediumPotions = in.nextInt();
int largePotions = in.nextInt();
Inventory inventory = new Inventory(smallPotions, mediumPotions, largePotions);
if (type.equals("Archer")) {
double bonusExp = in.nextDouble();
this.hero = new Archer(health, level, experience, maxHealth, nextLevelExperience, inventory,
bonusExp);
System.out.println("Archer loaded");
} else if (type.equals("Warrior")) {
this.hero = new Warrior(health, level, experience, maxHealth, nextLevelExperience, inventory);
System.out.println("Warrior loaded");
} else if (type.equals("Mage")) {
int healRate = in.nextInt();
this.hero = new Mage(health, level, experience, maxHealth, nextLevelExperience, inventory,
healRate);
System.out.println("Mage loaded");
}
in.close();
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
}
}
private void displayHero() {
if (this.hero != null) {
this.hero.display();
} else {
System.out.println("No hero exists");
}
}
private void createHero() {
if (this.hero != null) {
System.out.println("Do you want to remove the current Hero? Y\\N ");
try {
char inp = readYNChar();
if (inp == 'Y') {
createTypeHero();
} else if (inp == 'N') {
System.out.println("Current Hero remains.");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
} else {
createTypeHero();
}
}
private void createTypeHero() {
System.out.println("What type of Hero would you like to create?");
System.out.println("1. Archer");
System.out.println("2. Warrior");
System.out.println("3. Mage");
try {
int inp = readInt(1, 3);
if (inp == 1) {
this.hero = new Archer();
} else if (inp == 2) {
this.hero = new Warrior();
} else if (inp == 3) {
this.hero = new Mage();
}
System.out.println("Hero Created");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
private void singleBattle() {
if (this.hero != null) {
System.out.print("Enter the monster's name >> ");
Scanner in = new Scanner(System.in);
String name = in.nextLine();
System.out.print("Enter the monster's level <1-20> >> ");
try {
int level = readInt(1, 20);
Monster monster = new Monster(name, level);
System.out.println("~~~~~~BATTLE~~~~~~");
battleRun(monster);
} catch (Exception e) {
System.out.println(e.getMessage());
}
} else {
System.out.println("No hero exists");
}
}
private void battleFromList() {
if (this.hero != null) {
char input = 'N';
MonsterList monsterList = new MonsterList();
do {
System.out.print("Enter the monster's name >> ");
Scanner in = new Scanner(System.in);
String name = in.nextLine();
System.out.print("Enter the monster's level <1-20> >> ");
int level;
try {
level = readInt(1, 20);
Monster monster = new Monster(name, level);
monsterList.insertAtFront(monster);
System.out.print("Enter another monster? ");
input = readYNChar();
} catch (Exception e) {
System.out.println(e.getMessage());
}
} while (input == 'Y');
int i = 1;
do {
Monster monster = monsterList.removeEnd();
System.out.println("\n~~~~~~BATTLE~"+i+"~~~~~");
battleRun(monster);
i++;
} while ((monsterList.getHead()!= null) && (this.hero != null));
} else {
System.out.println("No hero exists");
}
}
private void battleRun(Monster monster) {
if (this.hero.getLevel() < monster.getLevel()) {
if (battle(hero, monster)) {
// gain exp
if (this.hero.gainExperience(monster.rewardExperience())) {
System.out.println("Hero recieved " + monster.rewardExperience() + " EXP");
System.out.println("Hero has leveled up!");
} else {
System.out.println("Hero recieved " + monster.rewardExperience() + " EXP");
}
// gain portions
this.hero.addPotions(monster.rewardPotions());
System.out.println("Hero recieved " + monster.rewardPotions().getSmallPotions() + " x Small, "
+ monster.rewardPotions().getMediumPotions() + " x Medium and "
+ monster.rewardPotions().getLargePotions() + " x Large Potions");
} else {
this.hero = null;
System.out.println("You will need a new Hero!");
}
} else {
if (battle(monster, hero)) {
this.hero = null;
System.out.println("You will need a new Hero!");
} else {
// gain exp
if (this.hero.gainExperience(monster.rewardExperience())) {
System.out.println("Hero recieved " + monster.rewardExperience() + "EXP");
System.out.println("Hero has leveled up!");
} else {
System.out.println("Hero recieved " + monster.rewardExperience() + "EXP");
}
// gain portions
this.hero.addPotions(monster.rewardPotions());
System.out.println("Hero recieved " + monster.rewardPotions().getSmallPotions() + " x Small, "
+ monster.rewardPotions().getMediumPotions() + " x Medium and "
+ monster.rewardPotions().getLargePotions() + " x Large Potions");
}
}
}
private boolean battle(BattleReady fighter1, BattleReady fighter2) {
int amount1 = fighter1.attack();
System.out.println(fighter1.getName() + " does " + amount1 + " damage!");
if (fighter2.takeDamage(amount1)) {
System.out.println(fighter2.getName() + " has " + fighter2.getHealth() + " health remaining.");
System.out.println("-----------------------------------------------------------------------");
} else {
System.out.println(fighter2.getName() + " fainted!");
return true;
}
int amount2 = fighter2.attack();
System.out.println(fighter2.getName() + " does " + amount2 + " damage!");
if (fighter1.takeDamage(amount2)) {
System.out.println(fighter1.getName() + " has " + fighter1.getHealth() + " health remaining.");
System.out.println("-----------------------------------------------------------------------");
} else {
System.out.println(fighter1.getName() + " fainted!");
return false;
}
return battle(fighter1, fighter2);
}
public int readInt(int min, int max) throws Exception {
int i = 0;
while (i < 5) {
try {
System.out.print("Enter choice >> ");
Scanner in = new Scanner(System.in);
int n = in.nextInt();
ValueChecker.checkValue(n, min, max);
return n;
} catch (InputMismatchException e) {
System.out.println("Input is not an integer");
} catch (IntRangeException e) {
System.out.println(e.getMessage());
}
i++;
}
throw new Exception("Valid value not entered after 5 attempts, exiting function");
}
public char readYNChar() throws Exception {
int i = 0;
while (i < 5) {
try {
System.out.print("Enter choice >> ");
Scanner in = new Scanner(System.in);
char c = in.next().charAt(0);
ValueChecker.checkValue(c);
return c;
} catch (InputMismatchException e) {
System.out.println("Input is not a character");
} catch (YNException e) {
System.out.println(e.getMessage());
}
i++;
}
throw new Exception("Valid value not entered after 5 attempts, exiting function");
}
}