-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
360 lines (318 loc) · 13.5 KB
/
Copy pathMain.java
File metadata and controls
360 lines (318 loc) · 13.5 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
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.List;
/**
* Elden Ring - a text RPG. Author: James DeSilver.
*
* <p>This class is the game's script: it walks the Tarnished through character creation, the
* narrative, and each boss in turn, visiting a Site of Grace between fights. The prose lives in
* {@link Story}, the bosses in {@link Bestiary}, the weapons in {@link Armory}, and combat in
* {@link Combat}; here we just sequence them.
*/
public class Main {
public static void main(String[] args) {
try {
run();
} catch (Throwable t) {
Console.println("");
Console.println("An unforeseen calamity befalls the Lands Between. The journey ends here.");
}
}
/** The full game script, wrapped by {@link #main} so no unforeseen error can ever crash to a stack trace. */
private static void run() {
Console.clear();
Console.narrate(Story.INTRO);
Console.print("What is thy name? ");
Player player = new Player(Console.readLine());
Console.clear();
Console.narrate(Story.TUTORIAL);
// Character creation: pick a starting weapon and spend starting runes.
List<Weapon> wheel = new ArrayList<>(Armory.tier1());
player.setHand(buyWeapon(player, wheel));
player.setStats(levelUp(player, player.getStats()));
Console.clear();
Console.narrate(Story.CREATION_COMPLETE);
Console.speak("Your character, " + player.getName() + ", is ready to begin the journey.");
Console.narrate(Story.JOURNEY_TO_MARGIT);
fight(player, Bestiary.margit(), wheel);
Console.narrate(Story.AFTER_MARGIT);
wheel = new ArrayList<>(Armory.tier2());
siteOfGrace(player, wheel);
Console.narrate(Story.BEFORE_GODRICK);
fight(player, Bestiary.godrick(), wheel);
Console.narrate(Story.AFTER_GODRICK);
wheel = new ArrayList<>(Armory.tier3());
siteOfGrace(player, wheel);
Console.narrate(Story.BEFORE_RENNALA);
fight(player, Bestiary.rennala(), wheel);
Console.narrate(Story.AFTER_RENNALA);
siteOfGrace(player, wheel);
Console.narrate(Story.BEFORE_RED_WOLF);
fight(player, Bestiary.redWolf(), wheel);
Console.narrate(Story.AFTER_RED_WOLF);
siteOfGrace(player, wheel);
Console.narrate(Story.BEFORE_SERPENT);
fight(player, Bestiary.serpent(), wheel);
Console.narrate(Story.AFTER_SERPENT);
wheel = new ArrayList<>(Armory.tier4());
siteOfGrace(player, wheel);
Console.narrate(Story.BEFORE_MOHG);
fight(player, Bestiary.mohg(), wheel);
Console.narrate(Story.AFTER_MOHG);
siteOfGrace(player, wheel);
Console.narrate(Story.BEFORE_MORGOTT);
fight(player, Bestiary.morgott(), wheel);
Console.narrate(Story.AFTER_MORGOTT);
siteOfGrace(player, wheel);
Console.narrate(Story.BEFORE_FIRE_GIANT);
fight(player, Bestiary.fireGiant(), wheel);
Console.narrate(Story.AFTER_FIRE_GIANT);
siteOfGrace(player, wheel);
Console.narrate(Story.ERDTREE_DECISION);
boolean eldenLord = chooseEnding();
Console.narrate(Story.BEFORE_BEAST_CLERGYMAN);
fight(player, Bestiary.beastClergyman(), wheel);
Console.narrate(Story.AFTER_BEAST_CLERGYMAN);
wheel = new ArrayList<>(Armory.tier5());
siteOfGrace(player, wheel);
Console.narrate(Story.BEFORE_GIDEON);
fight(player, Bestiary.gideon(), wheel);
Console.narrate(Story.AFTER_GIDEON);
siteOfGrace(player, wheel);
Console.narrate(Story.BEFORE_GODFREY);
fight(player, Bestiary.godfrey(), wheel);
Console.narrate(Story.AFTER_GODFREY);
wheel = new ArrayList<>(Armory.tier6());
siteOfGrace(player, wheel);
Console.narrate(Story.BEFORE_MALENIA);
fight(player, Bestiary.malenia(), wheel);
Console.narrate(Story.AFTER_MALENIA);
siteOfGrace(player, wheel);
Console.narrate(Story.BEFORE_RADAGON);
fight(player, Bestiary.radagon(), wheel);
Console.narrate(Story.AFTER_RADAGON);
if (eldenLord) {
Console.narrate(Story.EPILOGUE_ELDEN_LORD);
return;
}
Console.narrate(Story.EPILOGUE_CHAOS);
}
// ----- Site of Grace -----
/** The hub between fights: buy a weapon, level up, upgrade the current weapon, or leave. */
private static void siteOfGrace(Player player, List<Weapon> wheel) {
while (true) {
Console.clear();
Console.println("What dost thou wish to do?\n1) Purchase New Weapon\n2) Level Up\n3) Upgrade Weapon\n4) Leave\n");
switch (Console.readInt()) {
case 1 -> player.setHand(buyWeapon(player, wheel));
case 2 -> player.setStats(levelUp(player, player.getStats()));
case 3 -> {
Console.print("This will cost you " + player.getHand().getUpgradePrice() + " runes. ");
if (confirm()) {
if (player.getRunes() < player.getHand().getUpgradePrice()) {
Console.println("Not enough runes.");
} else {
player.getHand().upgrade();
}
}
}
case 4 -> {
Console.clear();
return;
}
default -> Console.speak("Invalid action. Try again.\n");
}
}
}
/**
* Lets the player buy a weapon from the wheel (or back out with -1). Buying refunds the price of
* the current weapon and leaves it in the wheel's slot, swapping the chosen weapon into hand.
*/
private static Weapon buyWeapon(Player player, List<Weapon> wheel) {
Console.println("Runes: " + player.getRunes() + "\n");
Console.println("Choose a Weapon: (-1 to back out)\n");
for (int i = 0; i < wheel.size(); i++) {
Console.println((i + 1) + ". " + wheel.get(i).getName() + " - Price: " + wheel.get(i).getPrice() + " Runes");
}
Console.println("");
while (true) {
int choice = Console.readInt();
if (choice == Console.INVALID) {
Console.speak("Invalid input. Please enter a valid number.\n");
continue;
}
if (choice == -1) {
return player.getHand();
}
choice--;
if (choice < 0 || choice >= wheel.size()) {
Console.speak("Invalid choice. Please select a valid weapon.\n");
continue;
}
if (player.getRunes() < wheel.get(choice).getPrice()) {
Console.println("Not enough runes. Choose a different weapon.\n");
continue;
}
if (!confirm()) {
continue;
}
player.spendRunes(wheel.get(choice).getPrice());
player.addRunes(player.getHand().getPrice());
Weapon chosen = wheel.get(choice);
wheel.set(choice, player.getHand());
return chosen;
}
}
// ----- Leveling up -----
/** A snapshot of the stat allocation, used to undo a point spend. */
private record UndoState(int[] stats, int runesSpent) {}
/**
* Spends runes across the eight stats, one at a time, with -1 to undo the previous spend.
* On confirmation, raises max HP/FP from Vigor/Mind and returns the new stat array.
*/
private static int[] levelUp(Player player, int[] stats) {
Deque<UndoState> history = new ArrayDeque<>();
int currentStatIndex = 0;
while (true) {
while (currentStatIndex < Stat.COUNT) {
Console.clear();
printStatRoster();
Console.println("Current Stats: " + Arrays.toString(stats));
Console.println("Runes remaining: " + player.getRunes());
Console.println("");
Console.print("Points into " + Stat.values()[currentStatIndex].label() + " (or enter -1 to undo): ");
int runesSpent = promptStatPoints(player.getRunes(), player.getStats(currentStatIndex));
if (runesSpent == -1) {
if (history.isEmpty()) {
Console.println("No actions to undo.");
continue;
}
UndoState last = history.pop();
stats = last.stats();
player.addRunes(last.runesSpent());
Console.println("Undo successful.");
if (currentStatIndex > 0) {
currentStatIndex--;
}
continue;
}
history.push(new UndoState(Arrays.copyOf(stats, stats.length), runesSpent));
stats[currentStatIndex] += runesSpent;
player.spendRunes(runesSpent);
Console.println("Runes remaining: " + player.getRunes());
Console.println("");
currentStatIndex++;
}
Console.clear();
printStatRoster();
Console.println("Current Stats: " + Arrays.toString(stats));
Console.println("");
if (confirm()) {
break;
}
currentStatIndex = Stat.COUNT - 1; // step back to revisit the last stat
}
player.setHp(player.getHp() + player.getStats(Stat.VIGOR.ordinal()) * 30);
player.setFp(player.getFp() + player.getStats(Stat.MIND.ordinal()) * 30);
return stats;
}
/** Prints the "The Stats: ..." roster line followed by a blank line. */
private static void printStatRoster() {
StringBuilder roster = new StringBuilder("The Stats: ");
Stat[] all = Stat.values();
for (int i = 0; i < all.length; i++) {
roster.append(all[i].label());
if (i < all.length - 1) {
roster.append(", ");
}
}
Console.println(roster.toString());
Console.println("");
}
/**
* Reads how many runes to put into a stat, re-prompting until the input is valid: a non-negative
* amount that neither pushes the stat past 99 nor exceeds available runes, or -1 to undo.
*/
private static int promptStatPoints(int runes, int currentStat) {
while (true) {
int amount = Console.readInt();
if (amount == Console.INVALID) {
Console.speak("Invalid input. Please enter a number.");
} else if ((long) amount + currentStat > 99) {
Console.println("Cannot go over 99.");
} else if (amount > runes) {
Console.println("Not enough runes.");
} else if (amount < -1) {
Console.println("Has to be positive or -1 to undo.");
} else {
return amount;
}
}
}
// ----- Combat orchestration -----
/**
* Fights a boss, restoring the player's HP/FP/heals afterward. On defeat the player may rest at a
* Site of Grace and try again; on victory they collect the boss's runes and gain a heal.
*/
private static void fight(Player player, Boss boss, List<Weapon> wheel) {
int normalHp = player.getHp();
int normalFp = player.getFp();
int heals = player.getHealingTotal();
int bossHp = boss.getHp();
Combat combat = new Combat(player, boss);
while (true) {
if (!combat.start()) {
player.setHp(normalHp);
player.setFp(normalFp);
player.setHealingTotal(heals);
boss.setHp(bossHp);
boss.setPhase(1);
if (player.getRunes() > 0) {
siteOfGrace(player, wheel);
}
continue;
}
player.setHp(normalHp);
player.setFp(normalFp);
player.setHealingTotal(heals);
player.addRunes(boss.getRunes());
player.addHealingTotal();
return;
}
}
// ----- The ending choice -----
/** The decision at the Erdtree. Returns true to become Elden Lord (Melina is sacrificed). */
private static boolean chooseEnding() {
Console.clear();
while (true) {
Console.println("Decide.\n1) Let Melina fullfill her mission.\n2) Let chaos take the world.");
int answer = Console.readInt();
if (answer == 1) {
Console.narrate(Story.ENDING_LET_MELINA);
return true;
} else if (answer == 2) {
Console.narrate(Story.ENDING_TAKE_HER_PLACE);
return false;
} else {
Console.clear();
Console.narrate(Story.DECISION_RETRY);
}
}
}
/** Asks a yes/no question, re-prompting until the player answers. Accepts Y/YES or N/NO, in any case. */
private static boolean confirm() {
while (true) {
Console.println("Are you sure? (Y or N)\n");
String answer = Console.readLine().trim().toUpperCase();
if (answer.equals("Y") || answer.equals("YES")) {
return true;
}
if (answer.equals("N") || answer.equals("NO")) {
return false;
}
Console.speak("Invalid input. Please enter Y or N.\n");
}
}
}