-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameMethods.java
More file actions
471 lines (434 loc) · 17.6 KB
/
Copy pathGameMethods.java
File metadata and controls
471 lines (434 loc) · 17.6 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
/* GAME & BATTLE METHODS */
import java.util.Scanner;
public class GameMethods
{
Scanner input = new Scanner(System.in);
/**
* Prints the battle menu with two buttons
* @param but1 Title of button 1
* @param but2 Title of button 2
* @param enemy Current enemy
* @param party User's entire party
*/
public void PrintGUI(String but1, String but2, Enemy enemy, Playable[] party)
{
// format party boxes
String C1HP = GUIFormat(party[0].GetHP() + "/" + party[0].GetMaxHP(),1);
String C1NAME = GUIFormat(party[0].GetName(),1);
String C2HP = GUIFormat(party[1].GetHP() + "/" + party[1].GetMaxHP(),1);
String C2NAME = GUIFormat(party[1].GetName(),1);
String C3HP = GUIFormat(party[2].GetHP() + "/" + party[2].GetMaxHP(),1);
String C3NAME = GUIFormat(party[2].GetName(),1);
String C4HP = GUIFormat(party[3].GetHP() + "/" + party[3].GetMaxHP(),1);
String C4NAME = GUIFormat(party[3].GetName(),1);
// format enemy box
String ENAME = GUIFormat(enemy.GetName(),2);
String EHP = GUIFormat( (enemy.GetHP() + "/" + enemy.GetMaxHP() +" "+ enemy.HPBar(0)), 2 );
// printing gui
System.out.println("╔══════════╗ ╔═════════════════════════════════════╗ ╔══════════╗");
System.out.println("║"+ C1NAME +"║ ║"+ ENAME +"║ ║"+ C2NAME +"║");
System.out.println("║"+ party[0].HPBar(0) +"║ ║"+ EHP +"║ ║"+ party[1].HPBar(0) +"║");
System.out.println("║"+ C1HP +"║ ╚═════════════════════════════════════╝ ║"+ C2HP +"║");
System.out.println("╚══════════╝ ╚══════════╝");
System.out.println("╔══════════╗╔═══════════════════╦═══════════════════╗╔══════════╗");
System.out.println("║"+ C3NAME +"║║ ║ ║║"+ C4NAME +"║");
System.out.println("║"+ party[2].HPBar(0) +"║║ 1 "+ GUIFormat(but1,3) +" ║ 2 "
+ GUIFormat(but2,3) +" ║║" + party[3].HPBar(0) +"║");
System.out.println("║"+ C3HP +"║║ ║ ║║"+ C4HP +"║");
System.out.println("╚══════════╝╚═══════════════════╩═══════════════════╝╚══════════╝");
}
/**
* Prints the battle menu with four buttons
* @param but1 Title of button 1
* @param but2 Title of button 2
* @param but3 Title of button 3
* @param but4 Title of button 4
* @param enemy Current enemy
* @param party User's entire party
*/
public void PrintGUI(String but1, String but2, String but3, String but4, Enemy enemy, Playable[] party)
{
// format party boxes
String C1HP = GUIFormat(party[0].GetHP() + "/" + party[0].GetMaxHP(),1);
String C1NAME = GUIFormat(party[0].GetName(),1);
String C2HP = GUIFormat(party[1].GetHP() + "/" + party[1].GetMaxHP(),1);
String C2NAME = GUIFormat(party[1].GetName(),1);
String C3HP = GUIFormat(party[2].GetHP() + "/" + party[2].GetMaxHP(),1);
String C3NAME = GUIFormat(party[2].GetName(),1);
String C4HP = GUIFormat(party[3].GetHP() + "/" + party[3].GetMaxHP(),1);
String C4NAME = GUIFormat(party[3].GetName(),1);
// format enemy box
String ENAME = GUIFormat(enemy.GetName(),2);
String EHP = GUIFormat( (enemy.GetHP() + "/" + enemy.GetMaxHP() +" "+ enemy.HPBar(0)), 2 );
// printing gui
System.out.println("╔══════════╗ ╔═════════════════════════════════════╗ ╔══════════╗");
System.out.println("║"+ C1NAME +"║ ║"+ ENAME +"║ ║"+ C2NAME +"║");
System.out.println("║"+ party[0].HPBar(0) +"║ ║"+ EHP +"║ ║"+ party[1].HPBar(0) +"║");
System.out.println("║"+ C1HP +"║ ╚═════════════════════════════════════╝ ║"+ C2HP +"║");
System.out.println("╚══════════╝ ╚══════════╝");
System.out.println("╔══════════╗╔═══════════════════╦═══════════════════╗╔══════════╗");
System.out.println("║"+ C3NAME +"║║ 1 "+ GUIFormat(but1,3) +" ║ 2 "
+ GUIFormat(but2,3) +" ║║"+ C4NAME +"║");
System.out.println("║"+ party[2].HPBar(0) +"║╠═══════════════════╬"
+ "═══════════════════╣║"+ party[3].HPBar(0) +"║");
System.out.println("║"+ C3HP +"║║ 3 "+ GUIFormat(but3,3) +" ║ 4 "
+ GUIFormat(but4,3) +" ║║"+ C4HP +"║");
System.out.println("╚══════════╝╚═══════════════════╩═══════════════════╝╚══════════╝");
}
/**
* Formats text for GUI buttons
* @param text Text that is to be formatted
* @param type Type of button the text will be in
* @return Formatted string, aligned with empty spaces where needed
*/
public String GUIFormat(String text, int type)
{
int padding; // amount of empty spaces around text
String leftPad; // text formatted with empty spaces on the left
switch (type)
{
case 1 -> {
/* party members box (name, health num) */
padding = (10 - text.length()) / 2;
leftPad = String.format("%" + (padding + text.length()) + "s", text);
return String.format("%-10s", leftPad);
}
case 2 -> {
/* enemy box (name, health num) */
padding = (37 - text.length()) / 2;
leftPad = String.format("%" + (padding + text.length()) + "s", text);
return String.format("%-37s", leftPad);
}
case 3 -> {
/* action buttons */
return String.format("%-15s", text);
}
}
return "Error!";
}
/**
* Takes in user's input during battle (in the form of integers)
* @param min Minimum option
* @param max Maximum option
* @return User's chosen option
*/
public int MenuChoice(int min, int max)
{
int choice; // user's choice
boolean valid; // validity of choice (within range)
do {
System.out.print(">> ");
choice = input.nextInt();
input.nextLine(); /*absorb hanging line*/
valid = (choice >= min && choice <= max); // checking if valid
if (!valid) {
System.out.println("Error! Invalid choice.");
}
} while (!valid); // loop until valid choice is entered
return choice;
}
/**
* PLAYER'S TURN - user chooses to fight or use items
* @param enemy Current enemy
* @param party User's entire party
* @param inv User's inventory
*/
public void PlayerTurn(Enemy enemy, Playable[] party, Item[] inv)
{
boolean valid; // validity of choice
int choice; // choice
do {
PrintGUI("FIGHT","ITEMS",enemy,party);
choice = MenuChoice(1,2);
switch (choice)
{
case 1:
/* FIGHT */
valid = Fight(enemy, party);
break;
case 2:
/* ITEMS */
valid = Items(enemy, party, inv);
break;
default:
valid = false;
}
} while (!valid); /* if failed, rerun */
}
/**
* FIGHT MENU - user chooses a character and attack to use against enemy
* @param enemy Current enemy
* @param party User's entire party
* @return Validity of move
*/
public boolean Fight(Enemy enemy, Playable[] party)
{
/* SELECT A CHARACTER TO USE */
PrintGUI(party[0].GetName(),party[1].GetName(),party[2].GetName(),
party[3].GetName(),enemy,party);
int chr = MenuChoice(1,4);
if (party[chr-1].GetHP() <= 0) {
/* Return false if chosen character is dead */
System.out.println(party[chr-1].GetName() + " has fallen. "
+ "Revive them or select another hero!");
return false;
}
else {
/* SELECT AN ATTACK TO USE */
PrintGUI(party[chr - 1].GetAtkName(1),party[chr - 1].GetAtkName(2),
party[chr - 1].GetAtkName(3),party[chr - 1].GetAtkName(4),
enemy,party);
int atk = MenuChoice(1,4);
switch (atk)
{
case 1 -> {
party[chr - 1].Atk1(enemy);
return true;
}
case 2 -> {
party[chr - 1].Atk2(enemy, party);
return true;
}
case 3 -> {
party[chr - 1].Atk3(enemy, party);
return true;
}
case 4 -> {
party[chr - 1].Atk4(enemy);
return true;
}
default -> {
return false;
}
}
}
}
/**
* ITEMS MENU - user accesses inventory and chooses an item to use
* @param enemy Current enemy
* @param party User's entire party
* @param inv User's inventory
* @return If an item was used
*/
public boolean Items(Enemy enemy, Playable[] party, Item[] inv)
{
boolean valid; // validity of item use
int choice, chr; // user's item choice and character choice
String[] names = {"-","-","-"}; // default name for inventory menu
String[] qty = {"0", "0", "0"}; // default amount for inventory menu
/* REPLACING EMPTY BUTTONS WITH INVENTORY ITEMS */
for (int i = 0; i < inv.length; i++) {
if (inv[i] != null) {
names[i] = inv[i].GetName();
qty[i] = Integer.toString(inv[i].GetQty());
}
}
/* SELECT AN ITEM TO USE */
PrintGUI(names[0] + "(" + qty[0] + ")", names[1] + "(" + qty[1] + ")",
names[2] + "(" + qty[2] + ")", "BACK",enemy,party);
choice = MenuChoice(1,4);
if (choice == 4) {
// exit inventory menu, back to Player_Turn
return false;
}
else if (Integer.parseInt(qty[choice-1]) > 0) {
/* SELECT A CHARACTER TO USE ITEM ON */
System.out.println("Choose a character to use " + inv[choice-1].GetName() + " on:");
PrintGUI(party[0].GetName(),party[1].GetName(),party[2].GetName(),
party[3].GetName(),enemy,party);
chr = MenuChoice(1,4);
// Checking if item use succeeded
valid = inv[choice-1].Use(party[chr-1]);
return valid;
}
else {
System.out.println("Error! Item unavailable.");
return false;
}
}
/**
* ENEMY TURN - Chooses an attack to use against a character in the player's party
* @param enemy Current enemy
* @param party User's entire party
* @return Validity of attack
*/
public boolean EnemyTurn(Enemy enemy, Playable[] party)
{
int max, atk, target;
if (enemy.GetName().equals("DRAGON")
|| enemy.GetName().equals("WULVER")) {
max = 3; /* for boss enemies */
}
else {
max = 2; /* regular enemies */
}
/* RANDOM NUMBER TO CHOOSE ATTACK */
atk = enemy.RandInt(1,max);
/* RANDOM NUMBER TO CHOOSE TARGET */
do {
target = enemy.RandInt(1,4);
} while (CheckDead(party[target-1]));
switch (atk)
{
case 1 -> {
enemy.Atk1(party[target-1]);
return true;
}
case 2 -> {
enemy.Atk2(party[target-1]);
return true;
}
case 3 -> {
enemy.Atk3(party[target-1]);
return true;
}
default -> {
return false;
}
}
}
/**
* Checks if given character is dead
* @param target Character to check
* @return If character is dead
*/
public boolean CheckDead(Character target)
{
if (target.GetHP() < 0)
{
target.AddHP(0 - target.GetHP()); // set hp to 0 if it is below
}
return target.GetHP() <= 0;
}
/**
* Counts how many characters in the player's party are dead
* @param party User's entire party
* @param ind Index for recursion search
* @return Number of dead characters
*/
public int CountDead(Playable[] party, int ind)
{
if (ind == party.length) {
return 0;
}
else if (party[ind].GetHP() <= 0) {
party[ind].AddHP(0 - party[ind].GetHP());
return (1 + CountDead(party, ind+1));
}
else {
return CountDead(party, ind+1);
}
}
/**
* MAIN BATTLE METHOD - Loops player and enemy turn until someone dies
* @param enemy Current enemy
* @param party User's entire party
* @param inv User's inventory
* @return If player defeated the enemy
*/
public boolean Battle(Enemy enemy, Playable[] party, Item[] inv)
{
boolean EnemyDead;
boolean PartyDead = (CountDead(party, 0) == 4);
do
{
if (PartyDead)
{
System.out.println("Party has fallen. Battle failed! Restarting...");
for (int i = 0; i < party.length; i++)
{
party[i].Revive(20); // Revive party
}
}
PlayerTurn(enemy, party, inv); // Player's turn to attack
EnemyDead = CheckDead(enemy);
if (!EnemyDead) { // If enemy has not died, enemy makes a move
EnemyTurn(enemy, party);
PartyDead = (CountDead(party, 0) == 4);
}
} while (!EnemyDead && !PartyDead);
return EnemyDead;
}
/**
* Adds an item to the inventory
* @param inv User's inventory
* @param item Item to add
*/
public void AddInv(Item[] inv, Item item)
{
int index = SearchInv(inv, item, 0);
if (inv[index] != null && inv[index].equals(item)) {
item.AddQty(1);
}
else {
inv[index] = item;
}
System.out.println("[ You have received (1) " + inv[index].GetName() + "! ]");
}
/**
* Searches for an item in the player's inventory
* @param inv User's inventory
* @param item Item to look for
* @param index Index of search
* @return Index of item or last empty space
*/
public int SearchInv(Item[] inv, Item item, int index)
{
if (inv[index] == item) {
// returns position of item if found
return index;
}
else if (inv[index] == null) {
// return index of empty space if the key is not found
return index;
}
else {
// continues searching if not found
return SearchInv(inv, item, index+1);
}
}
/**
* Takes in yes or no input from user
* @return User's input
*/
public String YesNo()
{
String choice; // user's choice
boolean valid; // validity of choice
do {
System.out.print(">> ");
choice = input.nextLine();
switch (choice.toUpperCase())
{ /* CHECKING IF VALID CHOICE */
case "YES":
case "Y":
case "NO":
case "N":
valid = true;
break;
default:
valid = false;
break;
}
if (!valid) { /* DISPLAY MESSAGE IF INVALID */
System.out.println("Error! Please enter a valid choice. (yes or no)");
}
} while (!valid); /* LOOP UNTIL USER ENTERS VALID CHOICE */
return choice.toUpperCase();
}
/**
* Pauses program for given amount of time
* @param ms Milliseconds to wait
*/
public static void Wait(int ms)
{
try
{
Thread.sleep(ms);
}
catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
}
}