-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMysticMayhem.java
More file actions
1670 lines (1531 loc) · 61.4 KB
/
MysticMayhem.java
File metadata and controls
1670 lines (1531 loc) · 61.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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;
//marker interfaces
interface Highlander{}
interface Marshlander{}
interface Sunchild{}
interface Mystic{}
class InputManager {
private static Scanner scanner = new Scanner(System.in);
public static int integerInput() {
while (true) {
try {
return Integer.parseInt(scanner.nextLine().trim());
} catch (NumberFormatException e) {
System.out.println("Invalid input. Please enter a valid integer.");
}
}
}
public static String stringInput() {
return scanner.nextLine().trim();
}
}
class FileManager {
public static void savePlayerMap(Map<Integer,Player> players) {
//writing the serialized player objects to the file
try{
FileOutputStream filestream = new FileOutputStream("PlayerMap.ser");
ObjectOutputStream os = new ObjectOutputStream(filestream);
os.writeObject(players);
os.close();
}
catch(IOException e){
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public static Map<Integer, Player> loadPlayerMap() {
Map<Integer,Player> players_got_from_file = new HashMap<Integer,Player>();
try{
FileInputStream filestream2 = new FileInputStream("PlayerMap.ser");
ObjectInputStream os2 = new ObjectInputStream(filestream2);
players_got_from_file = (Map<Integer,Player>)os2.readObject();
os2.close();
}
catch(Exception e){
return players_got_from_file;
}
return players_got_from_file;
}
public static void saveNameMap(Map<String,Integer> usernames) {
//writing the serialized player objects to the file
try{
FileOutputStream filestream = new FileOutputStream("usernames.ser");
ObjectOutputStream os = new ObjectOutputStream(filestream);
os.writeObject(usernames);
os.close();
}
catch(IOException e){
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public static Map<String,Integer> loadNameMap() {
Map<String,Integer> players_got_from_file = new HashMap<String,Integer>();
try{
FileInputStream filestream2 = new FileInputStream("usernames.ser");
ObjectInputStream os2 = new ObjectInputStream(filestream2);
players_got_from_file = (Map<String,Integer>)os2.readObject();
os2.close();
}
catch(Exception e){
return players_got_from_file;
}
return players_got_from_file;
}
public static void saveNumOfPlayers(int n) {
try (DataOutputStream dos = new DataOutputStream(new FileOutputStream("number.dat"))) {
dos.writeInt(n);
} catch (IOException e) {
e.printStackTrace();
}
}
public static int retrieveNumOfPlayers() {
int numOfPlayers = 0;
try (DataInputStream dis = new DataInputStream(new FileInputStream("number.dat"))) {
numOfPlayers = dis.readInt();
} catch (IOException e) {
return numOfPlayers;
}
return numOfPlayers;
}
}
abstract class Equipment implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
//all attributes are private
//all attributes are read only once created
private String name;
private int price;
private int attackModifier;
private int defenceModifier;
private int healthModifier;
private int speedModifier;
public String getName() {
return name;
}
public int getPrice() {
return price;
}
public int getAttackModifier() {
return attackModifier;
}
public int getDefenceModifier() {
return defenceModifier;
}
public int getHealthModifier() {
return healthModifier;
}
public int getSpeedModifier() {
return speedModifier;
}
public Equipment(String name, int price, int attackModifier, int defenceModifier, int healthModifier, int speedModifier) {
this.name = name;
this.price = price;
this.attackModifier = attackModifier;
this.defenceModifier = defenceModifier;
this.healthModifier = healthModifier;
this.speedModifier = speedModifier;
}
}
abstract class Armour extends Equipment {
private static final long serialVersionUID = 1L;
// Armour has 0 attack modifier
public Armour(String name, int price, int defenceModifier,int HealthModifier, int speedModifier) {
super(name, price, 0, defenceModifier,HealthModifier, speedModifier);
}
}
abstract class Artefact extends Equipment {
private static final long serialVersionUID = 1L;
public Artefact(String name, int price, int attackModifier, int defenceModifier, int healthModifier, int speedModifier) {
super(name, price, attackModifier, defenceModifier, healthModifier, speedModifier);
}
}
abstract class Archer extends Character {
private static final long serialVersionUID = 1L;
public Archer(String name, int price, int attack, int defence, int health, int speed) {
super(name, price, attack, defence, health, speed);
}
@Override
public String getCatagory() {
return "Archer";
}
}
abstract class Knight extends Character {
private static final long serialVersionUID = 1L;
public Knight(String name, int price, int attack, int defence, int health, int speed) {
super(name, price, attack, defence, health, speed);
}
@Override
public String getCatagory() {
return "Knight";
}
}
abstract class Mage extends Character {
private static final long serialVersionUID = 1L;
public Mage(String name, int price, int attack, int defence, int health, int speed) {
super(name, price, attack, defence, health, speed);
}
@Override
public String getCatagory() {
return "Mage";
}
}
abstract class Healer extends Character {
private static final long serialVersionUID = 1L;
public Healer(String name, int price, int attack, int defence, int health, int speed) {
super(name, price, attack, defence, health, speed);
}
@Override
public String getCatagory() {
return "Healer";
}
public void attack(Player targetPlayer, Character lowestHealthCharacter) {
}
}
abstract class MythicalCreature extends Character {
private static final long serialVersionUID = 1L;
public MythicalCreature(String name, int price, int attack, int defence, int health, int speed) {
super(name, price, attack, defence, health, speed);
}
@Override
public String getCatagory() {
return "Mythical Creature";
}
}
abstract class Character implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private int originalPrice;
private int price;
private int attack;
private int defence;
private double health;
private int speed;
private Armour armour;
private Artefact artifact;
private double currentHealth;
private boolean bonusTurn = false;
private boolean autoHeal = false;
public Character(String name, int price, int attack, int defence, double health, int speed) {
this.name = name;
this.originalPrice = price;
this.price = price;
this.attack = attack;
this.defence = defence;
this.health = health;
this.speed = speed;
this.currentHealth = health;
}
public abstract String getCatagory();
public void equipArmour(Armour armour) {
this.armour = armour;
this.defence += armour.getDefenceModifier();
this.speed += armour.getSpeedModifier();
this.health += armour.getHealthModifier();
}
public void equipArtefact(Artefact artifact) {
this.artifact = artifact;
this.attack += artifact.getAttackModifier();
this.defence += artifact.getDefenceModifier();
this.health += artifact.getHealthModifier();
this.speed += artifact.getSpeedModifier();
}
public String getName() {
return name;
}
public int getPrice() {
return price;
}
public int getAttack() {
return attack;
}
public void setAttack(int attack) {
this.attack = attack;
}
public int getDefence() {
return defence;
}
public void setDefence(int defence) {
this.defence = defence;
}
public double getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public Armour getArmour() {
return armour;
}
public Artefact getArtefact() {
return artifact;
}
public void addEquipmentPrize() {
this.price = this.originalPrice;
if(!(armour == null)) {
this.price += (int)Math.round(0.2*armour.getPrice());
}
if(!(artifact == null)) {
this.price += (int)Math.round(0.2*artifact.getPrice());
}
}
@Override
public String toString() {
return getCatagory() + ":: name : " + name +
", price : " + price +
", attack : " + attack +
", defence : " + defence +
", health : " + health +
", speed : " + speed +
", armour : " + armour +
", artifact : " + artifact;
}
public void addTerrainEffect(String battleGround) {
switch (battleGround) {
case "Hillcrest":
if (this instanceof Highlander) {
System.out.println(this.getName()+" : Attack and Defence increased by 1, Bonus Turn enabled.");
attack += 1;
defence += 1;
bonusTurn = true;
}
if (this instanceof Marshlander || this instanceof Sunchild) {
System.out.println(this.getName()+" : Speed decreased by 1.");
speed -= 1;
}
break;
case "Marshland":
if (this instanceof Marshlander) {
System.out.println(this.getName()+" : Defence increased by 2.");
defence += 2;
}
if (this instanceof Sunchild) {
System.out.println(this.getName()+" : Attack decreased by 1.");
attack -= 1;
}
if (this instanceof Mystic) {
System.out.println(this.getName()+" : Speed decreased by 1.");
speed -= 1;
}
break;
case "Desert":
if (this instanceof Marshlander) {
System.out.println(this.getName()+" : Health decreased by 1.");
health -= 1;
currentHealth -= 1;
}
if (this instanceof Sunchild) {
System.out.println(this.getName()+" : Attack increased by 1.");
attack += 1;
}
break;
case "Arcane":
if (this instanceof Highlander || this instanceof Marshlander) {
System.out.println(this.getName()+" : Speed and Defence decreased by 1.");
speed -= 1;
defence -= 1;
}
if (this instanceof Mystic) {
System.out.println(this.getName()+" : Attack increased by 2, Auto Heal enabled.");
attack += 2;
autoHeal = true;
}
break;
// Additional cases for other terrains
default:
System.out.println("No terrain effects for: " + battleGround);
break;
}
}
public void revertTerrainEffect(String battleGround) {
switch (battleGround) {
case "Hillcrest":
if(this instanceof Highlander) {
attack -= 1;
defence -= 1;
bonusTurn = false;
}
if(this instanceof Marshlander) {
speed += 1;
}
if(this instanceof Sunchild) {
speed += 1;
}
break;
case "Marshland":
if(this instanceof Marshlander) {
defence -= 2;
}
if(this instanceof Sunchild) {
attack += 1;
}
if(this instanceof Mystic) {
speed += 1;
}
break;
case "Desert":
if(this instanceof Marshlander) {
health += 1;
currentHealth += 1;
}
if(this instanceof Sunchild) {
attack -= 1;
}
break;
case "Arcane":
if(this instanceof Highlander) {
speed += 1;
defence += 1;
}
if(this instanceof Marshlander) {
speed += 1;
defence += 1;
}
if(this instanceof Mystic) {
attack -= 2;
autoHeal = false;
}
break;
// Additional cases for other terrains
default:
System.out.println("No terrain effects for: " + battleGround);
break;
}
}
public void resetHealth() {
this.currentHealth = health;
}
public double getCurrentHealth() {
return currentHealth;
}
public void takeDamage(double damage) {
this.currentHealth -= damage;
if (!isAlive()) {
System.out.println(name+" died!");
}
}
public void heal(double hp) {
this.currentHealth += hp;
if (this.currentHealth > health) {
this.currentHealth = health;
}
}
public boolean isAlive() {
return this.currentHealth > 0;
}
public boolean hasBonusTurn() {
return bonusTurn;
}
public boolean hasAutoHeal() {
return autoHeal;
}
}
class Player implements Serializable {
private static final long serialVersionUID = 1L;
public static int numOfPlayers = 0;
// name, goldCoins are read and write
// username, userID, homeground are read only
private String name;
private String username;
private int userID;
private int xpLevel;
private int goldCoins;
private Map<String, Character> army;
private String homeground;
public Map<String, Character> getArmy() {
return army;
}
public void addCharacter(Character newchar) {
//adds a given character replacing any current character of the sub class
army.put(newchar.getCatagory(), newchar);
}
public Player(String name, String username, String homeground) {
this.name = name;
this.username = username;
this.homeground = homeground;
this.userID = numOfPlayers;
this.xpLevel = 0;
this.goldCoins = 500;//initial 500 gold
this.army = new HashMap<>();
numOfPlayers ++;
}
public Player(String name, String username, String homeground, int xp, int gc) {
this.name = name;
this.username = username;
this.homeground = homeground;
this.userID = numOfPlayers;
this.xpLevel = xp;
this.goldCoins = gc;
this.army = new HashMap<>();
numOfPlayers ++;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUsername() {
return username;
}
public int getUserID() {
return userID;
}
public int getGoldCoins() {
return goldCoins;
}
public void recieveGoldCoins(int coins) {
this.goldCoins += coins;
System.out.println(username + " recieved" + coins +" coins.");
}
public void spendGoldCoins(int coins) {
this.goldCoins -= coins;
System.out.println(username + " spent " + coins +" coins.");
}
public void addXp() {
this.xpLevel++;
System.out.println(username + "'s xp level upgraded");
}
public int getXpLevel() {
return this.xpLevel;
}
public String getHomeground() {
return homeground;
}
public void displayArmy() {
System.out.println(name + "'s Army:");
for (Character character : army.values()) {
System.out.println(character);
}
}
public void displayStats() {
System.out.println("Name : " + name);
System.out.println("XP Level: " + xpLevel);
System.out.print("Army : ");
for(Character c: army.values()) {
System.out.print(c.getName()+"--");
}
System.out.println("\n");
}
public Character leastHealthCharacter() {
if (army.isEmpty()) {
// If the army is empty, return null or handle the case appropriately
return null;
}
Character leastHealthChar = null;
double minHealth = Integer.MAX_VALUE;
for (Character character : army.values()) {
double curhealth = character.getCurrentHealth();
if (curhealth < minHealth && character.isAlive()) {
minHealth = curhealth;
leastHealthChar = character;
}
}
return leastHealthChar;
}
}
//Archer sub classes
class Shooter extends Archer implements Highlander {
private static final long serialVersionUID = 1L;
public Shooter() {super("Shooter", 80, 11, 4, 6, 9);}}
class Ranger extends Archer implements Highlander {
private static final long serialVersionUID = 1L;
public Ranger() { super("Ranger", 115, 14, 5, 8, 10); }}
class Sunfire extends Archer implements Sunchild {
private static final long serialVersionUID = 1L;
public Sunfire() { super("Sunfire", 160, 15, 5, 7, 14); }}
class Zing extends Archer implements Sunchild {
private static final long serialVersionUID = 1L;
public Zing() { super("Zing", 200, 16, 9, 11, 14); }}
class Saggitarius extends Archer implements Mystic {
private static final long serialVersionUID = 1L;
public Saggitarius() { super("Saggitarius", 230, 18, 7, 12, 17); }}
//Knight sub classes
class Squire extends Knight implements Marshlander {
private static final long serialVersionUID = 1L;
public Squire() { super("Squire", 85, 8, 9, 7, 8); }}
class Cavalier extends Knight implements Highlander {
private static final long serialVersionUID = 1L;
public Cavalier() {super("Cavalier", 110, 10, 12, 7, 10);}}
class Templar extends Knight implements Sunchild {
private static final long serialVersionUID = 1L;
public Templar() { super("Templar", 155, 14, 16, 12, 12);}}
class Zoro extends Knight implements Highlander {
private static final long serialVersionUID = 1L;
public Zoro() { super("Zoro", 180, 17, 16, 13, 14);}}
class Swiftblade extends Knight implements Marshlander {
private static final long serialVersionUID = 1L;
public Swiftblade() {super("Swiftblade", 250, 18, 20, 17, 13);}}
//Mage sub classes
class Warlock extends Mage implements Marshlander {
private static final long serialVersionUID = 1L;
public Warlock() {super("Warlock", 100, 12, 7, 10, 12);}}
class Illusionist extends Mage implements Mystic {
private static final long serialVersionUID = 1L;
public Illusionist() {super("Illusionist", 120, 13, 8, 12, 4);}}
class Enchanter extends Mage implements Highlander {
private static final long serialVersionUID = 1L;
public Enchanter() {super("Enchanter", 160, 16, 10, 13, 16);}}
class Conjurer extends Mage implements Highlander {
private static final long serialVersionUID = 1L;
public Conjurer() {super("Conjurer", 195, 18, 15, 14, 12);}}
class Eldritch extends Mage implements Mystic {
private static final long serialVersionUID = 1L;
public Eldritch() {super("Eldritch", 270, 19, 17, 18, 14);}}
//Healer sub classes
class Soother extends Healer implements Sunchild {
private static final long serialVersionUID = 1L;
public Soother() {super("Soother", 95, 10, 8, 9, 6);}}
class Medic extends Healer implements Highlander {
private static final long serialVersionUID = 1L;
public Medic() {super("Medic", 125, 12, 9, 10, 7);}}
class Alchemist extends Healer implements Marshlander {
private static final long serialVersionUID = 1L;
public Alchemist() {super("Alchemist", 150, 13, 13, 13, 13);}}
class Saint extends Healer implements Mystic {
private static final long serialVersionUID = 1L;
public Saint() {super("Saint", 200, 16, 14, 17, 9);}}
class Lightbringer extends Healer implements Sunchild {
private static final long serialVersionUID = 1L;
public Lightbringer() {super("Lightbringer", 260, 17, 15, 19, 12);}}
//MythicalCreature sub classes
class Dragon extends MythicalCreature implements Sunchild {
private static final long serialVersionUID = 1L;
public Dragon() {super("Dragon", 120, 12, 14, 15, 8);}}
class Basilisk extends MythicalCreature implements Marshlander {
private static final long serialVersionUID = 1L;
public Basilisk() {super("Basilisk", 165, 15, 11, 10, 12);}}
class Hydra extends MythicalCreature implements Marshlander {
private static final long serialVersionUID = 1L;
public Hydra() {super("Hydra", 205, 12, 16, 15, 11);}}
class Phoenix extends MythicalCreature implements Sunchild {
private static final long serialVersionUID = 1L;
public Phoenix() {super("Phoenix", 275, 17, 13, 17, 19);}}
class Pegasus extends MythicalCreature implements Mystic {
private static final long serialVersionUID = 1L;
public Pegasus() {super("Pegasus", 340, 14, 18, 20, 20);}}
//Armour sub classes
class Chainmail extends Armour {
private static final long serialVersionUID = 1L;
public Chainmail() { super("Chainmail", 70, 1, 0, -1); } }
class Regalia extends Armour {
private static final long serialVersionUID = 1L;
public Regalia() { super("Regalia", 105, 1, 0, 0); } }
class Fleece extends Armour {
private static final long serialVersionUID = 1L;
public Fleece() { super("Fleece", 150, 2, 1, -1); } }
//Artefact sub classes
class Amulet extends Artefact {
private static final long serialVersionUID = 1L;
public Amulet() { super("Amulet", 200, 1, -1, 1, 1); } }
class Excalibur extends Artefact {
private static final long serialVersionUID = 1L;
public Excalibur() { super("Excalibur", 150, 2, 0, 0, 0); } }
class Crystal extends Artefact {
private static final long serialVersionUID = 1L;
public Crystal() { super("Crystal", 210, 2, 1, -1, -1); } }
//main game
public class MysticMayhem{
public static Map<Integer,Player> players = null;
public static Map<String,Integer> usernames = null;
public static Map<Integer, String> Category = Map.of(0, "Archer",1, "Knight",2, "Mage",3, "Healer",4, "Mythical Creature");
public static int currentPlayerID = -1; // -1 Indicates no players logged in
//Select log in/ Create new
public static void mainMenu() {
while (true) {
System.out.println("1)Login");
System.out.println("2)Create a profile");
System.out.println("3)Exit");
System.out.println("\nPlease select a menu option");
int com = InputManager.integerInput();
if (com == 1) {
Login();
} else if (com == 2) {
CreateProfile();
} else if (com == 3) {//exit
break;
} else {
System.out.println("Invalid option");
continue;
}
}
}
//Creates a profile and logs in
public static void CreateProfile() {
System.out.print("Enter your name: ");
String name = InputManager.stringInput();
String username;
while(true) {
System.out.print("Enter your username: ");
username = InputManager.stringInput();
if (!usernames.containsKey(username)) {
break;
}
System.out.println("This username already exists. Use a different username");
}
//Enter homeground
String homeground = null;
while (homeground == null) {
System.out.println("Select your homeground: ");
System.out.println("1) Hillcrest");
System.out.println("2) Marshland");
System.out.println("3) Desert ");
System.out.println("4) Arcane ");
int com = InputManager.integerInput();
switch (com) {
case 1:
homeground = "Hillcrest";
break;
case 2:
homeground = "Marshland";
break;
case 3:
homeground = "Desert";
break;
case 4:
homeground = "Arcane";
break;
default:
System.out.println("Invalid option");
}
}
//Create player object
Player player = new Player(name, username,homeground);
players.put(player.getUserID(), player);
usernames.put(player.getUsername(),player.getUserID());
while (true) {
//Buying first army
System.out.println("Create your army to battle");
System.out.println("You have "+ player.getGoldCoins() +" Gold coins.");
displayCharacterPrizes();
System.out.println("\nYou must buy a single character from each catagory.");
int id;
int Gold_Coin_on_hand=player.getGoldCoins();
System.out.println("Gold coins on hand : "+Gold_Coin_on_hand+"\n");
System.out.println("Archer (0-4) 0: Shooter 1:Ranger 2:Sunfire 3:Zing 4:Saggitarius (0 is recommended): ");
id=InputManager.integerInput();
if(id<0 || id>4){
System.out.println("Invalid Selection , Select Again"+"\n");
continue;
}
Character archer = createChar(id);
Gold_Coin_on_hand =Gold_Coin_on_hand-archer.getPrice();
System.out.println("Gold coins on hand : "+Gold_Coin_on_hand+"\n");
System.out.println("Knight(5-9) 5:Sqire 6:Cavalier 7:Templar 8:Zoro 9:Swiftblade (5 is recommended) ");
id=InputManager.integerInput();
if(id<5 || id>9){
System.out.println("Invalid Selection , Select Again\n");
continue;
}
Character knight = createChar(id);
Gold_Coin_on_hand =Gold_Coin_on_hand-knight.getPrice();
System.out.println("Gold coins on hand : "+Gold_Coin_on_hand+"\n");
System.out.println("Mage(10-14) 10:Warlock 11:Illusionist 12:Enchanter 13:Conjurer 14:Eldritch (10 is recommended)");
id=InputManager.integerInput();
if(id<10 || id>14){
System.out.println("Invalid Selection , Select Again\n");
continue;
}
Character mage = createChar(id);
Gold_Coin_on_hand =Gold_Coin_on_hand-mage.getPrice();
System.out.println("Gold coins on hand : "+Gold_Coin_on_hand+"\n");
System.out.println("Healer(15-19) 15:Soother 16:Medic 17:Alchemist 18:Saint 19:Lightbringer (15 is recommended)");
id=InputManager.integerInput();
if(id<15 || id>19){
System.out.println("Invalid Selection , Select Again\n");
continue;
}
Character healer = createChar(id);
Gold_Coin_on_hand =Gold_Coin_on_hand-healer.getPrice();
if(Gold_Coin_on_hand<120){
System.out.println("You Have No Enough Money To Progress, Please Select Again\n");
continue;
}
System.out.println("Gold coins on hand : "+Gold_Coin_on_hand+"\n");
System.out.println("MythicalCreature(20-24) 20:Dragon 21:Basillisk 22:Hydra 23:Phoenix 24:pegasus (20 is recommended)");
id=InputManager.integerInput();
if(id<20 || id>24){
System.out.println("Invalid Selection , Select Again\n");
continue;
}
Character mythicalCreature = createChar(id);
Gold_Coin_on_hand =Gold_Coin_on_hand-mythicalCreature.getPrice();
if ( Gold_Coin_on_hand<0) {
System.out.println("Not enough money for this army, Please select again\n");
continue;
}
//Buy the army
player.addCharacter(archer);
player.addCharacter(knight);
player.addCharacter(mage);
player.addCharacter(healer);
player.addCharacter(mythicalCreature);
player.spendGoldCoins(player.getGoldCoins()- Gold_Coin_on_hand);
break;
}
//Log in
currentPlayerID = player.getUserID();
//Save
FileManager.saveNameMap(usernames);
FileManager.savePlayerMap(players);
BattleMenu();
}
//Log in using the user name
public static void Login() {
if (Player.numOfPlayers == 0) {
System.out.println("No Players are registered. Please create a new profile first\n");
return;
}
String username;
while(true) {
System.out.println("Registered usernames:\n");
for(int i=0;i<Player.numOfPlayers;i++) {
System.out.println(players.get(i).getUsername());
}
System.out.print("\nEnter your username: ");
username = InputManager.stringInput();
if (usernames.containsKey(username)) {
break;
}
System.out.println("Couldn't find a username called \""+username+"\"");
}
currentPlayerID = usernames.get(username);
BattleMenu();
}
//Select to pass/ battle/ upgrade Army or change name
public static void BattleMenu() {
while (true) {
System.out.println("\nYour State:- Gold coins: "+players.get(currentPlayerID).getGoldCoins()+
"|| XP level: "+players.get(currentPlayerID).getXpLevel());
int currentOpID = randOpID();
if (currentOpID == -1) {
System.out.println("No Opponents to show\n");
}
else {
System.out.println("Random Opponent:");
players.get(currentOpID).displayStats();
}
System.out.println("1) Pass");
System.out.println("2) Battle");
System.out.println("3) Upgrade Army");
System.out.println("4) Log out");
System.out.println("5) Change Player Name"); // New menu option
System.out.println("\nPlease select a menu option");
int com = InputManager.integerInput();
if (com == 1) {
continue;
} else if (com == 2) {
Battle(players.get(currentPlayerID), players.get(currentOpID));
continue;
} else if (com == 3) {
ArmyUpgradeMenu();
continue;
} else if (com == 4) {
// Log out
currentPlayerID = -1;
break;
} else if (com == 5) {
changePlayerName();
continue;
} else {
System.out.println("Invalid option");
continue;
}
}
}
//Generate random opponent ID
public static int randOpID() {
if (Player.numOfPlayers <= 1) {
// If there is only one player, return null
return -1;
}
Random random = new Random();
int randomID;
do {
randomID = random.nextInt(Player.numOfPlayers);
} while (randomID == currentPlayerID);
return randomID;
}
//Select to Buy new characters or equipment
private static void ArmyUpgradeMenu() {
while (true) {
players.get(currentPlayerID).displayArmy();
System.out.println("1)Buy New Character");
System.out.println("2)Buy New Equipment");
System.out.println("3)Back to Battle Menu");
System.out.println("\nPlease select a menu option");
int com = InputManager.integerInput();
if (com == 1) {
CharacterShop();
continue;
} else if (com == 2) {
EquipmentShop();
continue;
} else if (com == 3) {
break;
} else {
// Default case, handle unexpected values of command
System.out.println("Invalid option");
continue;
}
}
}
//Select and buy characters
private static void CharacterShop() {
Player p = players.get(currentPlayerID);