-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathMain.java
More file actions
471 lines (422 loc) · 19.4 KB
/
Main.java
File metadata and controls
471 lines (422 loc) · 19.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
package org.project;
import org.project.entity.enemies.Dragon;
import org.project.entity.enemies.Enemy;
import org.project.entity.enemies.Goblin;
import org.project.entity.enemies.Skeleton;
import org.project.entity.players.Assassin;
import org.project.entity.players.Knight;
import org.project.entity.players.Player;
import org.project.entity.players.Wizard;
import org.project.location.Location;
import org.project.object.armors.KnightArmor;
import org.project.object.armors.Nothing;
import org.project.object.consumables.Flask;
import org.project.object.weapons.Punch;
import org.project.object.weapons.Sword;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
List<Location> locations = levelConstructor();
Scanner scanner = new Scanner(System.in);
// Character Creation
Player player = null;
do {
System.out.println("\n========================================");
System.out.println(" CREATE YOUR HERO ");
System.out.println("========================================");
System.out.println("1. Wizard - Master of arcane magic");
System.out.println("2. Assassin - Deadly and precise");
System.out.println("3. Knight - Strong and resilient");
System.out.println("========================================");
System.out.print("Choose your class (1-3): ");
int classNumber = scanner.nextInt();
System.out.print("Enter your hero's name: ");
String name = scanner.next();
switch (classNumber) {
case 1:
player = new Wizard(name, new Punch(Wizard.BaseAttack), new Nothing());
System.out.println("\n>> " + name + " the Wizard enters the fray!");
break;
case 2:
player = new Assassin(name, new Punch(Assassin.BaseAttack), new Nothing());
System.out.println("\n>> " + name + " the Assassin emerges from the shadows!");
break;
case 3:
player = new Knight(name, new Punch(Knight.BaseAttack), new Nothing());
System.out.println("\n>> " + name + " the Knight charges into battle!");
break;
default:
System.out.println("Invalid class selection. Please choose agian");
}
}while(player==null);
//start playing
boolean playing = true;
while(playing) {
//choosing a location
showLocations(locations);
System.out.print("\nChoose your destination (0 to quit): ");
int locationIndex = scanner.nextInt() - 2;
if (locationIndex == -2) {
System.out.println("\n>> Farewell, adventurer!");
break;
}
enterLocation(locations, locationIndex,player);
//if we entered the shop go back to location selecting menu
if(locationIndex == -1)
continue;
//location loop
Location currentLocation = locations.get(locationIndex);
int round=0;
boolean inLocation = true;
while(inLocation) {
//Round Header
System.out.println("\n========================================");
System.out.printf(" ROUND %-3d - %-15s%n", ++round, currentLocation.getName());
System.out.println("========================================");
System.out.println(player.getStatus());
//Player's turn
player.reset();
displayCombatOptions();
int option = scanner.nextInt();
switch(option) {
case 1://Attack
enemyList(locations, locationIndex);
System.out.print("\nChoose your target: ");
Enemy enemy = currentLocation.getEnemies().get(scanner.nextInt() - 1);
player.attack(enemy);
System.out.printf("\n>> You strike the %s for %d damage!%n",
enemy.getClass().getSimpleName(),
player.getWeapon().getDamage());
if (enemy.isDead()) {
int gainedExp = enemy.getLevel() * (enemy.getMaxHP() / 5);
int gainedCoins = enemy.getLevel() * (enemy.getMaxHP() / 5);
System.out.printf(">> You defeated the %s! Gained %d XP and %d coins.%n",
enemy.getClass().getSimpleName(), gainedExp, gainedCoins);
player.collectExp(gainedExp);
player.setCoin(player.getCoin() + gainedCoins);
if (!(enemy instanceof Skeleton) || enemy.isResurrected()) {
currentLocation.getEnemies().remove(enemy);
}
}
break;
case 2:// Heal
int healAmount = player.getMaxHP() / 8;
player.heal(healAmount);
System.out.printf("\n>> You healed yourself for %d HP!%n", healAmount);
break;
case 3:// Defense
player.DefendStatusChange();
System.out.printf("\n>> You are now %sdefending.%n",
player.isDefending() ? "" : "not ");
break;
case 4:// Special Ability
enemyList(locations, locationIndex);
System.out.print("\nChoose your target: ");
enemy = currentLocation.getEnemies().get(scanner.nextInt() - 1);
player.Special(enemy);
System.out.printf("\n>> You use your special ability on the %s!%n",
enemy.getClass().getSimpleName());
if (enemy.isDead()) {
int gainedExp = enemy.getLevel() * (enemy.getMaxHP() / 5);
int gainedCoins = enemy.getLevel() * (enemy.getMaxHP() / 5);
System.out.printf(">> You defeated the %s! Gained %d XP and %d coins.%n",
enemy, gainedExp, gainedCoins);
player.collectExp(gainedExp);
player.setCoin(player.getCoin() + gainedCoins);
if (!(enemy instanceof Skeleton) || enemy.isResurrected()) {
currentLocation.getEnemies().remove(enemy);
}
}
case 5:
// Fill Mana
int manaGain = player.getMaxMp() / 5;
player.setMp(player.getMp() + manaGain);
System.out.printf("\n>> You focus and restore %d MP!%n", manaGain);
break;
case 6:// Leave Location
System.out.println("\n>> You prepare to retreat at the start of next round...");
inLocation = false;
break;
default:
System.out.println("\n>> Invalid option! You hesitate...");
}
// Check if location is cleared
if (locationClearedCheck(currentLocation)) {
inLocation = false;
locations.remove(currentLocation);
break;
}
//Enemies' turn
for(Enemy enemy : currentLocation.getEnemies()) {
//Skeleton Resurrection check
if (enemy.isDead() && enemy instanceof Skeleton) {
enemy.Special(player);
System.out.printf("\n>> The %s's bones rattle as it reforms!%n",
enemy.getClass().getSimpleName());
continue;
}
enemy.reset();
int random = (int) (Math.random() * 100) % 20;
if (random <= 9) {// Attack
//Dragon Special
if(enemy instanceof Dragon && player.isDefending())
{
enemy.Special(player);
System.out.printf("\n>> The %s Pierced through your guard!%n", enemy);
}else {
System.out.printf("\n>> The %s attacks you!%n", enemy);
enemy.attack(player);
}
if (player.isDead()) {
System.out.println("\n>> Your vision fades to black...");
inLocation = false;
playing = false;
break;
}
} else if (random <= 14) { // Defend
System.out.printf("\n>> The %s raises its guard!%n", enemy);
enemy.DefendStatusChange();
} else if (random <= 19 ) { // Heal
int healAmount = enemy.getMaxHP() / 10;
System.out.printf("\n>> The %s heals itself for %d HP!%n",
enemy.getClass().getSimpleName(), healAmount);
enemy.heal(healAmount);
}
}
}
}
//Death Message
if (!playing) {
System.out.println("\n========================================");
System.out.println(" GAME OVER ");
System.out.println("========================================");
System.out.println("Your hero " + player.getName() + " has fallen in battle.");
System.out.println("Final stats:");
System.out.println(player.getStatus());
System.out.println("========================================");
}
}
private static void displayCombatOptions() {
System.out.println("\nCOMBAT OPTIONS:");
System.out.println("1. Basic Attack");
System.out.println("2. Heal (12.5% of max HP)");
System.out.println("3. Toggle Defense");
System.out.println("4. Special Ability");
System.out.println("5. Focus (Restore 33% MP)");
System.out.println("6. Retreat");
System.out.print("Choose your action: ");
}
private static ArrayList<Location> levelConstructor() {
//list of all levels and its enemies
ArrayList<Location> locations = new ArrayList<>();
ArrayList<Enemy> enemies;
Location location;
//level 1
enemies = addEnemy(1,1,0,0,0,0);
location = new Location(enemies,"Jungle 1");
locations.add(location);
//level 2
enemies = addEnemy(1,2,1,1,0,0);
location = new Location(enemies,"Jungle 2");
locations.add(location);
//level 3
enemies = addEnemy(3,1,1,1,0,0);
location = new Location(enemies,"Jungle 3");
locations.add(location);
//level 4
enemies = addEnemy(0,0,2,2,0,0);
location = new Location(enemies,"Jungle 4");
locations.add(location);
//level 5
enemies = addEnemy(2,1,0 ,0, 1,1);
location = new Location(enemies,"Jungle 5");
locations.add(location);
//level 6
enemies = addEnemy(3,1,1,2,0,0);
location = new Location(enemies,"Desert 1");
locations.add(location);
//level 7
enemies = addEnemy(3,2,2,1,0,0);
location = new Location(enemies,"Desert 2");
locations.add(location);
//level 8
enemies = addEnemy(2,3,1,2,0,0);
location = new Location(enemies,"Desert 3");
locations.add(location);
//level 9
enemies = addEnemy(0,0,3,2,0,0);
location = new Location(enemies,"Desert 4");
locations.add(location);
//level 10
enemies = addEnemy(0,0,2,2,1,2);
location = new Location(enemies,"Desert 5");
locations.add(location);
//level 11
enemies = addEnemy(0,0,4,2,0,0);
location = new Location(enemies,"Mountain 1");
locations.add(location);
//level 12
enemies = addEnemy(8,2,0,0,0,0);
location = new Location(enemies,"Mountain 2");
locations.add(location);
//level 13
enemies = addEnemy(3,3,2,2,0,0);
location = new Location(enemies,"Mountain 3");
locations.add(location);
//level 14
enemies = addEnemy(1,5,3,3,0,0);
location = new Location(enemies,"Mountain 4");
locations.add(location);
//level 15
enemies = addEnemy(3,3,1,2,1,3);
location = new Location(enemies,"Mountain 5");
locations.add(location);
//level 16
enemies = addEnemy(4,4,2,3,0,0);
location = new Location(enemies,"Island 1");
locations.add(location);
//level 17
enemies = addEnemy(2,2,3,4,0,0);
location = new Location(enemies,"Island 2");
locations.add(location);
//level 18
enemies = addEnemy(3,3,1,6,0,0);
location = new Location(enemies,"Island 3");
locations.add(location);
//level 19
enemies = addEnemy(1,10,3,4,0,0);
location = new Location(enemies,"Island 4");
locations.add(location);
//level 20
enemies = addEnemy(2,6,2,4,1,5);
location = new Location(enemies,"Island 5");
locations.add(location);
return locations;
}
private static ArrayList<Enemy> addEnemy(int numS,int lvlS,int numG,int lvlG,int numD,int lvlD) {
//creating an enemy list
ArrayList<Enemy> enemies = new ArrayList<>();
for(int i=1; i<=numS; i++)
enemies.add(new Skeleton(lvlS,new Punch(Skeleton.BaseAttack)));
for(int i=1; i<=numG; i++)
enemies.add(new Goblin(lvlG,new Punch(Goblin.BaseAttack)));
for(int i=1; i<=numD; i++)
enemies.add(new Dragon(lvlD,new Punch(Dragon.BaseAttack)));
return enemies;
}
private static boolean locationClearedCheck(Location currentLocation) {
//it checks whether a location is cleared or not
if(currentLocation.getEnemies().isEmpty())
{
System.out.println("\nYou have cleared this location!");
return true;
}
return false;
}
public static void showLocations(List<Location> locations) {
//shows the list of locations
System.out.println("\n=== Available Locations ===");
System.out.println("1. Shop");
for (int i = 0; i < locations.size(); i++) {
System.out.println((i + 2) + ". " + locations.get(i).getName() +
" (" + locations.get(i).getEnemies().size() + " enemies)");
}
}
public static void enterLocation(List<Location> locations,int locationIndex,Player player) {
//enter a location or shop message
if (locationIndex == -1) {
enterShop(player);
} else {
System.out.println("\nYou enter " + locations.get(locationIndex).getName());
enemyList(locations, locationIndex);
}
}
private static void enterShop(Player player) {
//shop of the game
//exchanging coins for goods
Scanner input = new Scanner(System.in);
System.out.println("\n=== Welcome to the Shop ===");
System.out.println(player.getStatus());
System.out.println("\nAvailable items:");
System.out.println("1. Health Flask (25 coins) - Restores 25% HP");
System.out.println("2. Armor Services");
System.out.println(" - New Armor: 150 coins");
System.out.println(" - Repair: 75 coins");
System.out.println("3. Sword Upgrade");
System.out.println(" - New Sword: 85 coins");
System.out.println(" - Upgrade: 85 coins (+10% damage)");
System.out.println("4. Exit Shop");
System.out.print("Choose an option: ");
int select = input.nextInt();
switch(select)
{
case 1:
System.out.println("\nEach flask restores 25% of your max HP.");
if (player.getCoin() >= 25) {
player.setCoin(player.getCoin() - 25);
new Flask().use(player);
System.out.println("You drank a health flask!");
} else {
System.out.println("You don't have enough coins.");
}
break;
case 2:
if (player.getArmor() instanceof Nothing) {
System.out.println("\nA new armor costs 150 coins.");
if (player.getCoin() >= 150) {
player.setCoin(player.getCoin() - 150);
player.setArmor(new KnightArmor(player.getMaxHP() / 3));
System.out.println("You equipped new armor!");
} else {
System.out.println("You don't have enough coins.");
}
} else {
System.out.println("\nArmor repair costs 75 coins.");
if (player.getCoin() >= 75) {
player.setCoin(player.getCoin() - 75);
player.getArmor().repair();
System.out.println("Your armor has been repaired!");
} else {
System.out.println("You don't have enough coins.");
}
}
break;
case 3:
if (player.getWeapon() instanceof Punch) {
System.out.println("\nA new sword costs 85 coins.");
if (player.getCoin() >= 85) {
player.setCoin(player.getCoin() - 85);
player.setWeapon(new Sword(player.getWeapon().getDamage() * 3));
System.out.println("You equipped a new sword!");
} else {
System.out.println("You don't have enough coins.");
}
} else {
System.out.println("\nSword upgrade costs 85 coins.");
if (player.getCoin() >= 85) {
player.setCoin(player.getCoin() - 85);
player.getWeapon().levelUp();
System.out.printf("Your sword has been upgraded to lvl.%d" , player.getLevel());
} else {
System.out.println("You don't have enough coins.");
}
}
break;
case 4:
System.out.println("You leave the shop.");
break;
}
}
public static void enemyList(List<Location> locations,int locationIndex) {
//shows the list of all enemies
System.out.println("\nEnemies in this location:");
System.out.println("--------------------------");
int i = 1;
for (Enemy enemy : locations.get(locationIndex).getEnemies()) {
System.out.println(i++ + ". " + enemy.getStatus());
}
System.out.println("--------------------------");
}
}