-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinalTest.java
More file actions
380 lines (304 loc) · 11.8 KB
/
FinalTest.java
File metadata and controls
380 lines (304 loc) · 11.8 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
import java.util.Random;
import java.util.Scanner;
public class FinalTest {
static Random random = new Random();
static Scanner input = new Scanner(System.in);
static int playerHealth = 100;
static int starsCollected = 0;
static int progress = 0;
static int healthPotions = 3; // Added variable for health potions
static final int MAX_HEALTH_POTIONS = 3;
static final int TOTAL_STARS_NEEDED = 4;
// Enemies
static String[] enemies = {"wizard", "troll", "soldier", "giant"};
public static void main(String[] args) {
FinalTest game = new FinalTest();
game.startQuest();
int maxStars = TOTAL_STARS_NEEDED;
while (starsCollected < maxStars && playerHealth > 0) {
game.displayProgress();
String randomEnemy = enemies[random.nextInt(enemies.length)];
int choice = game.getChoice();
switch (choice) {
case 1:
//game.clearScreen();
game.battle(randomEnemy);
//game.clearScreen();
break;
case 2:
game.drinkPotion();
//game.clearScreen();
break;
case 3:
System.out.println("You chose to quit. Game over!");
System.exit(0);
break;
case 4:
//game.clearScreen();
game.displayInstructions();
break;
default:
System.out.println("Invalid choice. Please enter 1, 2, 3, or 4. Try again.");
}
}
}
private void startQuest() {
System.out.println("\nWelcome to Fantasy Quest!");
timePause(1500);
System.out.println("In this epic adventure, you, our beloved noble hero, have been chosen by the king to embark on a quest ");
timePause(1500);
System.out.println("Behold, noble hero! Our realm is in danger, and our king has chosen you to help protect it.");
timePause(3000);
char userAnswerToContinue;
do {
System.out.println("\nDo you possess the bravery to embark on this quest and ensure our kingdom's safety? (y/n)");
String userAnswer = input.next();
userAnswerToContinue = Character.toLowerCase(userAnswer.charAt(0));
if (userAnswerToContinue == 'y') {
System.out.println();
System.out.println("Excellent choice, noble hero, your quest begins now!");
timePause(1500);
System.out.println("You have to successfully fight off 4 creatures to gain a star to fully regain control of our kingdom");
timePause(1500);
System.out.println("Your quest starts now!");
timePause(3000);
} else if (userAnswerToContinue == 'n') {
System.out.println("\nDang, if you change your mind please come back!");
System.exit(0);
} else {
System.out.println("Invalid Input. Please enter 'y' or 'n'.");
}
} while (userAnswerToContinue != 'y' && userAnswerToContinue != 'n');
}
private void battle(String enemyType) {
String enemyDetails = getEnemyDetails(enemyType);
String enemyQuote = getEnemyQuote(enemyType);
System.out.println();
System.out.println("You have entered the battle against the " + enemyType + "!");
timePause(500);
System.out.println(enemyDetails);
timePause(500);
System.out.println();
System.out.println("Enemy's Quote: \"" + enemyQuote + "\"");
System.out.println();
timePause(500);
int enemyHealth = random.nextInt(50) + 50;
int damageToEnemy;
int damageToPlayer;
while (enemyHealth > 0 && playerHealth > 0) {
displayProgress();
displayColoredEnemyHealth(enemyHealth);
System.out.println();
System.out.println("Options:");
System.out.println("1. Continue Battle");
System.out.println("2. Drink a health potion");
System.out.println("3. Flee");
int choice;
do {
System.out.print("Enter your choice (1, 2, or 3): ");
try {
choice = Integer.parseInt(input.next());
} catch (NumberFormatException e) {
choice = -1;
}
switch (choice) {
case 1:
// Display battle details before clearing the screen
damageToEnemy = Math.max(0, random.nextInt(20) + 10);
damageToPlayer = Math.max(0, random.nextInt(10) + 5);
clearScreen();
System.out.println("You are currently fighting " + enemyType + "!");
System.out.println("You deal " + damageToEnemy + " damage to the " + enemyType);
System.out.println("The " + enemyType + " deals " + damageToPlayer + " damage to you");
enemyHealth = Math.max(0, enemyHealth - damageToEnemy);
playerHealth = Math.max(0, playerHealth - damageToPlayer);
break;
case 2:
drinkPotion();
timePause(1500);
clearScreen();
break;
case 3:
System.out.println("You chose to flee. Returning to the main menu.");
clearScreen();
return;
default:
System.out.println("Invalid choice. Please enter 1, 2, or 3. Try again.");
}
} while (choice < 1 || choice > 3);
if (playerHealth <= 0) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
clearScreen();
System.out.println("You have been defeated. Game over!");
System.exit(0);
}
System.out.println("Enemy Health: " + enemyHealth);
if (enemyHealth <= 0) {
System.out.println("You have defeated the " + enemyType + "!");
int healthIncrease =Math.min(20, 100-playerHealth);
playerHealth += healthIncrease;
starsCollected++;
progress = starsCollected * 100 / TOTAL_STARS_NEEDED;
System.out.println("You gained a star!");
if(starsCollected<TOTAL_STARS_NEEDED){
System.out.println("Prepare yourself! Another enemy awaits you!");
}
break;
}
}
}
private String getEnemyDetails(String enemyType) {
String enemyDetails="";
switch (enemyType) {
case "wizard":
enemyDetails= "A powerful sorcerer skilled in the arcane arts.";
break;
case "troll":
enemyDetails= "A massive creature with tough skin and immense strength.";
break;
case "soldier":
enemyDetails= "A trained warrior wielding a sword and shield.";
break;
case "giant":
enemyDetails= "A towering giant capable of crushing foes with a single blow.";
break;
}
return enemyDetails;
}
private String getEnemyQuote(String enemyType) {
String enemyQuote="";
switch (enemyType) {
case "wizard":
enemyQuote= "Prepare to face the fury of my magical prowess!";
break;
case "troll":
enemyQuote="I'll smash you to pieces with my mighty fists!";
break;
case "soldier":
enemyQuote= "Stand aside or face the might of my blade!";
break;
case "giant":
enemyQuote= "Puny human, you're no match for my colossal strength!";
break;
}
return enemyQuote;
}
private void displayColoredEnemyHealth(int health) {
String healthColor;
String ANSI_RESET = "\u001B[0m";
if (health >= 80) {
healthColor = "\u001B[32m";
} else if (health < 80 && health >= 50) {
healthColor = "\u001B[33m";
} else {
healthColor = "\u001B[31m";
}
System.out.println("Enemy Health: " + healthColor + health + ANSI_RESET);
}
private void displayProgress() {
timePause(1500);
System.out.println("Current progress: " + progress + "%");
timePause(500);
System.out.println("Stars Collected: " + starsCollected);
timePause(500);
System.out.println("Health Potions Left: " + healthPotions);
System.out.println();
timePause(1500);
displayColoredHealth(playerHealth, "Player");
}
private void displayColoredHealth(int health, String entity) {
String healthColor;
String ANSI_RESET = "\u001B[0m";
int[] thresholds = {50, 80};
healthColor = "\u001B[32m";
for (int threshold : thresholds) {
if (health < threshold) {
healthColor = "\u001B[31m";
break;
}
}
System.out.println(entity + " Health: " + healthColor + health + ANSI_RESET);
}
private void drinkPotion() {
int potionHeal = random.nextInt(29) +2;
if (playerHealth ==100) {
System.out.println("Your health is already at maximum. No need for a health potion.");
return;
}
if (healthPotions <= 0) {
System.out.println("No health potions left. Choose another option.");
return;
}
else {
healthPotions--;
System.out.println("You drank a health potion and healed for " + potionHeal + " health.");
}
playerHealth += potionHeal;
if (playerHealth > 100) {
playerHealth = 100;
}
System.out.println("Current health: " + playerHealth);
System.out.println("Health Potions Left: " + healthPotions);
}
private int getChoice() {
int choice;
do {
System.out.println("Options:");
System.out.println("1. Fight");
System.out.println("2. Drink a health potion");
System.out.println("3. Quit");
System.out.println("4. Help/Instructions");
System.out.println();
System.out.print("Enter your choice (1, 2, 3, or 4): ");
try {
choice = Integer.parseInt(input.next());
} catch (NumberFormatException e) {
choice = -1;
}
if (choice < 1 || choice > 4) {
System.out.println("Invalid choice. Please enter 1, 2, 3, or 4. Try again.");
}
} while (choice < 1 || choice > 4);
return choice;
}
private void displayInstructions() {
clearScreen();
System.out.println("\n=== Instructions ===\n");
timePause(500);
System.out.println("Your objective is to regain control of the kingdom");
System.out.println("You have to gain 5 crowns by fighting and defeating enemies to gain control and win");
System.out.println("You are given 4 potions that give you 70 health each to continue your quest");
timePause(500);
System.out.println("\nGood luck! Regain control of our kingdom Hero!\n");
System.out.println("====================");
int returnChoice;
do {
timePause(2200);
System.out.println("Press '1' to return to the main menu.");
try {
returnChoice = Integer.parseInt(input.next());
} catch (NumberFormatException e) {
returnChoice = -1;
}
if (returnChoice != 1) {
System.out.println("Invalid choice. Please press '1' to return to the main menu. Try again.");
}
} while (returnChoice != 1);
clearScreen();
}
private void timePause(int x) {
try {
Thread.sleep(x);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void clearScreen() {
System.out.print("\033[H\033[2J");
System.out.flush();
}
}