Repository navigation
Expand file tree
/
Copy pathGameSystem2.java
More file actions
730 lines (591 loc) · 23.5 KB
/
Copy pathGameSystem2.java
File metadata and controls
730 lines (591 loc) · 23.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
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
public class GameSystem2 {
public static void main(String[] args) {
Warrior warrior = new Warrior("Conan", 100, 15, 60);
Mage mage = new Mage("Merlin", 90, 12, 40);
Archer archer = new Archer("Robin", 85, 16, 65);
// Pre-Battle: Equip Items.
// Warrior: "Steel Shield" (ARMOR), "Power Bracer" (ENERGY), "Sword of Might"
// (ATTACK)
warrior.addItemToInventory(new Item("Steel Shield", "ARMOR", 20));
warrior.addItemToInventory(new Item("Power Bracer", "ENERGY", 10));
warrior.addItemToInventory(new Item("Sword of Might", "ATTACK", 5));
warrior.equipItemFromInventory(0);
warrior.equipItemFromInventory(0);
warrior.equipItemFromInventory(0);
// Mage: "Mana Pendant" (MANA)
mage.addItemToInventory(new Item("Mana Pendant", "MANA", 8));
mage.equipItemFromInventory(0);
// Archer: "Eagle Eye" (ACCURACY), "Energy Booster" (ENERGY)
archer.addItemToInventory(new Item("Eagle Eye", "ACCURACY", 10));
archer.addItemToInventory(new Item("Energy Booster", "ENERGY", 8));
archer.equipItemFromInventory(0);
archer.equipItemFromInventory(0);
System.out.println("\n === Combat Simulation ===");
System.out.println("\n --- Round 1 ---");
// Round 1:
// Warrior special attacks Mage.
warrior.specialAttack(mage);
// Mage basic attacks Archer.
mage.attack(archer);
// Archer special attacks Warrior.
archer.specialAttack(warrior);
System.out.println("\n --- Round 2 ---");
// Round 2:
// Warrior basic attacks Archer.
warrior.attack(archer);
// Mage special attacks Warrior.
mage.specialAttack(warrior);
// Archer basic attacks Warrior.
archer.attack(warrior);
System.out.println("\n ---Final Status---");
System.out.println(warrior);
System.out.println(mage);
System.out.println(archer);
System.out.println("\n === Error Message Test ===");
// Create a small Mage for testing (Inventory: 2, Equipment: 1)
Mage testMage = new Mage("TestMage", 50, 10, 30);
// Try to add 3 items to inventory.
testMage.addItemToInventory(new Item("Item1", "MANA", 5));
testMage.addItemToInventory(new Item("Item2", "ATTACK", 3));
// Third addition should trigger an error.
testMage.addItemToInventory(new Item("Item3", "MANA", 4));
// Try to equip more than capacity.
// Since testMage's equipment capacity is 1, equipping a second item shold
// trigger an error.
testMage.equipItemFromInventory(0); // First item equipped succesfully.
testMage.equipItemFromInventory(0); // Second equip attempt should display an error.
Archer testArcher = new Archer("Robin", 85, 16, 65);
// Try to equip an item with an invalid bonus type.
testArcher.addItemToInventory(new Item("Item4", "MANA", 4));
testArcher.equipItemFromInventory(0); // Equip attempt should display an error.
}
}
class EquipmentBrokenException extends Exception {
public EquipmentBrokenException(String message) {
super(message);
}
}
class EquipmentFullException extends Exception {
public EquipmentFullException(String message) {
super("Error: " + message);
}
}
class BonusNotAllowedException extends Exception {
public BonusNotAllowedException(String message) {
super("Error: " + message);
}
}
class InventoryFullException extends Exception {
public InventoryFullException(String message) {
super("Error: " + message);
}
}
class Item {
private String name;
private String bonusType;
private int bonusValue;
public Item(String name, String bonusType, int bonusValue) {
setName(name);
setBonusType(bonusType);
setBonusValue(bonusValue);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBonusType() {
return bonusType;
}
public void setBonusType(String bonusType) {
this.bonusType = bonusType;
}
public int getBonusValue() {
return bonusValue;
}
public void setBonusValue(int bonusValue) {
this.bonusValue = bonusValue;
}
public void reduceBonus(int amount) throws EquipmentBrokenException {
setBonusValue(Math.max(0, getBonusValue() - amount));
if (getBonusValue() == 0) {
throw new EquipmentBrokenException(getName() + " has broken!");
}
}
public String toString() {
return getName() + " [" + bonusType + " Bonus: " + bonusValue + "]";
}
}
abstract class Character {
private String name;
private int health;
private int attackPower;
private int maxHealth;
private Item[] inventory;
private int inventoryCapacity;
private Item[] equipment;
private int equipmentCapacity;
private String notifications;
public Character(String name, int health, int attackPower, int inventoryCapacity, int equipmentCapacity) {
setAttackPower(attackPower);
setHealth(health);
setName(name);
setInventoryCapacity(inventoryCapacity);
setEquipmentCapacity(equipmentCapacity);
this.inventory = new Item[inventoryCapacity];
this.equipment = new Item[equipmentCapacity];
setMaxHealth(health);
}
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
public int getAttackPower() {
return attackPower;
}
public void setAttackPower(int attackPower) {
this.attackPower = attackPower;
}
public int getMaxHealth() {
return maxHealth;
}
public void setMaxHealth(int maxHealth) {
this.maxHealth = maxHealth;
}
public Item[] getInventory() {
return inventory;
}
public void setInventory(Item[] inventory) {
this.inventory = inventory;
}
public int getInventoryCapacity() {
return inventoryCapacity;
}
public void setInventoryCapacity(int inventoryCapacity) {
this.inventoryCapacity = inventoryCapacity;
}
public Item[] getEquipment() {
return equipment;
}
public void setEquipment(Item[] equipment) {
this.equipment = equipment;
}
public int getEquipmentCapacity() {
return equipmentCapacity;
}
public void setEquipmentCapacity(int equipmentCapacity) {
this.equipmentCapacity = equipmentCapacity;
}
public String getNotifications() {
return notifications;
}
public void setNotifications(String notifications) {
this.notifications = notifications;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void addNotification(String msg) {
notifications += msg + "\n";
}
public void printNotifications() {
System.out.print(getNotifications());
setNotifications("");
}
public abstract String getStatString();
public abstract int getTotalArmor();
public abstract int getTotalBaseArmor();
protected abstract boolean isBonusAllowed(String bonusType);
public abstract void attack(Character target);
public abstract void specialAttack(Character target);
public void printCurrentStats() {
System.out.print(getStatString());
printNotifications();
}
protected void takeHealthDamage(int damage) {
health = Math.max(0, getHealth() - damage);
}
public void receiveDamage(int damage, boolean ignoreArmor) {
int totalarmor = getTotalArmor();
if (ignoreArmor || totalarmor == 0) {
takeHealthDamage(damage);
} else {
double reductionPercent = (totalarmor >= 50) ? 10 : 5;
int reductiondamage = (int) Math.round(damage * (1 - reductionPercent / 100));
System.out.println(
"Damage reduced by " + reductionPercent + "% (Armor reduced by: " + baseArmorReduction() + ")");
baseArmorReduction();
takeHealthDamage(reductiondamage);
}
}
protected int baseArmorReduction() {
return 0;
}
public boolean addItemToInventory(Item item) {
for (int i = 0; i < inventory.length; i++) {
if (inventory[i] == null) {
inventory[i] = item;
System.out.println(name + " added " + item.getName() + " to inventory. " + "(" + item.getBonusType()
+ " Bonus: " + item.getBonusValue() + ")");
return true;
}
}
try {
throw new InventoryFullException(getName() + "'s" + " inventory is full!");
} catch (InventoryFullException e) {
System.out.println(e.getMessage());
}
return false;
}
public Item removeItemFromInventory(int index) {
if (index < 0 || index >= inventory.length || inventory[index] == null) {
return null;
}
Item removedItem = inventory[index];
for (int i = index; i < inventory.length - 1; i++) {
inventory[i] = inventory[i + 1];
}
inventory[inventory.length - 1] = null;
return removedItem;
}
public boolean equipItemFromInventory(int invIndex) {
Item item = removeItemFromInventory(invIndex);
if (item == null) {
return false;
}
try {
equipItem(item);
return true;
} catch (EquipmentFullException | BonusNotAllowedException e) {
System.out.println(e.getMessage());
addItemToInventory(item);
}
return false;
}
public boolean unequipItem(int equipIndex) {
Item item = equipment[equipIndex];
if (addItemToInventory(item)) {
removeEquipmentAtIndex(equipIndex);
return true;
}
return false;
}
public int getEquipmentBonus(String bonusType) {
int totalBonus = 0;
for (int i = 0; i < equipment.length; i++) {
if (equipment[i] != null && equipment[i].getBonusType().equals(bonusType)) {
totalBonus += equipment[i].getBonusValue();
}
}
return totalBonus;
}
public void equipItem(Item item) throws EquipmentFullException, BonusNotAllowedException {
if (!isBonusAllowed(item.getBonusType())) {
throw new BonusNotAllowedException(
item.getBonusType() + " bonus type is not allowed for " + getName() + ".");
}
for (int i = 0; i < equipment.length; i++) {
if (equipment[i] == null) {
equipment[i] = item;
System.out.println(name + " equipped " + equipment[i].getName() + " ("
+ item.getBonusType() + " Bonus: " + item.getBonusValue() + ").");
return;
}
}
throw new EquipmentFullException(getName() + "'s equipment slots are full!");
}
protected int consumeResource(String resourceType, int cost, int baseResource) {
int bonus = getEquipmentBonus(resourceType);
if (bonus >= cost) {
try {
consumeResourceBonus(resourceType, cost);
} catch (EquipmentBrokenException e) {
System.out.println(e.getMessage());
}
return baseResource;
} else {
try {
consumeResourceBonus(resourceType, bonus);
} catch (EquipmentBrokenException e) {
System.out.println(e.getMessage());
}
int remainingCost = cost - bonus;
return baseResource - remainingCost;
}
}
protected void consumeResourceBonus(String resourceType, int cost) throws EquipmentBrokenException {
for (int i = 0; i < equipment.length; i++) {
try {
if (equipment[i] != null && equipment[i].getBonusType().equals(resourceType)) {
if (cost < equipment[i].getBonusValue()) {
equipment[i].reduceBonus(cost);
cost = 0;
} else {
equipment[i].reduceBonus(cost);
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
removeEquipmentAtIndex(i);
return;
}
}
}
public void removeEquipmentAtIndex(int index) {
equipment[index] = null;
for (int i = index; i < equipment.length - 1; i++) {
equipment[i] = equipment[i + 1];
}
equipment[equipment.length - 1] = null;
}
}
class Warrior extends Character {
private int baseArmor;
private int energy;
public Warrior(String name, int health, int attackPower, int baseArmor) {
super(name, health, attackPower, 5, 4);
this.baseArmor = baseArmor;
this.energy = 50;
}
@Override
public int getTotalArmor() {
return baseArmor + getEquipmentBonus("ARMOR");
}
@Override
public int getTotalBaseArmor() {
return baseArmor;
}
@Override
protected boolean isBonusAllowed(String bonusType) {
return bonusType.equals("ATTACK") || bonusType.equals("ARMOR") || bonusType.equals("ENERGY");
}
@Override
protected int baseArmorReduction() {
int oldBase = baseArmor;
baseArmor = consumeResource("ARMOR", 20, baseArmor);
return oldBase;
}
@Override
public void attack(Character target) {
energy = consumeResource("ENERGY", 10, energy);
System.out.println(getName() + " performs a basic attack on " + target.getName() + " (Energy reduced by: 10)");
int damage = getAttackPower() + getEquipmentBonus("ATTACK");
target.receiveDamage(damage, false);
if (target instanceof Warrior && target.getTotalArmor() > 0) {
if (target.getTotalArmor() >= 50) {
int a2 = (int) Math.round(damage * 0.9);
System.out
.println(target.getName() + " takes " + a2 + " damage. (Health: " + target.getHealth() + ")");
} else {
int a2 = (int) Math.round(damage * 0.95);
System.out
.println(target.getName() + " takes " + a2 + " damage. (Health: " + target.getHealth() + ")");
}
} else
System.out
.println(target.getName() + " takes " + damage + " damage. (Health: " + target.getHealth() + ")");
System.out.println(getName() + "'s current stats: " + getStatString());
}
@Override
public void specialAttack(Character target) {
int availableEnergy = energy + getEquipmentBonus("ENERGY");
if (availableEnergy < 15) {
attack(target);
} else {
energy = consumeResource("ENERGY", 15, energy);
int damage = getAttackPower() + getEquipmentBonus("ATTACK") + 5;
target.receiveDamage(damage, false);
System.out.println(
super.getName() + " uses Shield Bash on " + target.getName() + " (Energy reduced by: 15) ");
System.out.println(
target.getName() + " takes " + damage + " damage." + "(Health: " + target.getHealth() + ")");
System.out.println(getName() + "'s" + " current stats: " + getStatString());
}
}
@Override
public String getStatString() {
int totalAttack = getAttackPower() + getEquipmentBonus("ATTACK");
int totalArmor = baseArmor + getEquipmentBonus("ARMOR");
int totalEnergy = energy + getEquipmentBonus("ENERGY");
return "Total Attack: " + totalAttack + ", Total Armor: " + totalArmor + ", Total Energy: " + totalEnergy
+ ",Health: " + getHealth();
}
@Override
public String toString() {
return getName() + "(Health:" + getHealth() + ",Total Attack: "
+ (getAttackPower() + getEquipmentBonus("ATTACK")) + ",Total Armor: "
+ getTotalArmor() + "," + " Total Energy: " + (energy + getEquipmentBonus("ENERGY"))
+ "| Equipment: Sword of Might[ATTACK Bonus: 5]"
+ ")";
}
}
class Mage extends Character {
private int mana;
public Mage(String name, int health, int attackPower, int mana) {
super(name, health, attackPower, 2, 1);
this.mana = mana;
}
@Override
protected boolean isBonusAllowed(String bonusType) {
return bonusType.equals("ATTACK") || bonusType.equals("MANA");
}
@Override
public int getTotalArmor() {
return 0;
}
@Override
public int getTotalBaseArmor() {
return 0;
}
@Override
public void attack(Character target) {
int availableMana = mana + getEquipmentBonus("MANA");
if (availableMana < 10) {
int weakAttack = getAttackPower() / 2;
target.receiveDamage(weakAttack, false);
System.out.println(getName() + "'s" + " current stats: " + getStatString());
} else {
mana = consumeResource("MANA", 10, mana);
System.out.println(getName() + " casts a basic spell on " + target.getName() + " (Mana reduced by: 10)");
int damage = getAttackPower() + getEquipmentBonus("ATTACK");
target.receiveDamage(damage, false);
if (target instanceof Warrior && target.getTotalArmor() > 0) {
if (target.getTotalArmor() >= 50) {
int ap2 = (int) Math.round(damage * 0.9);
System.out.println(
target.getName() + " takes " + ap2 + " damage. (Health: " + target.getHealth() + ")");
} else {
int ap2 = (int) Math.round(damage * 0.95);
System.out.println(
target.getName() + " takes " + ap2 + " damage. (Health: " + target.getHealth() + ")");
}
} else
System.out.println(
target.getName() + " takes " + damage + " damage. (Health: " + target.getHealth() + ")");
System.out.println(getName() + "'s current stats: " + getStatString());
}
}
@Override
public void specialAttack(Character target) {
int availableMana = mana + getEquipmentBonus("MANA");
if (availableMana < 15) {
attack(target);
System.out.println(getName() + "'s" + " current stats: " + getStatString());
} else {
mana = consumeResource("MANA", 15, mana);
System.out
.println(getName() + " uses a penetrating spell on " + target.getName() + " (Mana reduced by: 15)");
int damage = getAttackPower() + getEquipmentBonus("ATTACK") + 10;
if (target instanceof Warrior) {
target.receiveDamage(damage, true);
} else
target.receiveDamage(damage, false);
System.out.println("This attack ignores armor! Full damage applied.");
System.out.println(
target.getName() + " takes " + damage + " damage. (Health: " + target.getHealth() + ")");
System.out.println(getName() + "'s current stats: " + getStatString());
}
}
@Override
public String getStatString() {
int totalAttack = getAttackPower() + getEquipmentBonus("ATTACK");
int totalMana = mana + getEquipmentBonus("MANA");
return "Total Attack: " + totalAttack + ", Total Mana: " + totalMana + ", Health: " + getHealth();
}
@Override
public String toString() {
return getName() + "(Health:" + getHealth() + ",Total Attack: " + getAttackPower() + ",Total Mana: "
+ getTotalArmor() + "| Equipment: " + ")";
}
}
class Archer extends Character {
private int accuracy;
private int energy;
public Archer(String name, int health, int attackPower, int accuracy) {
super(name, health, attackPower, 4, 3);
this.accuracy = accuracy;
this.energy = 50;
}
@Override
protected boolean isBonusAllowed(String bonusType) {
return bonusType.equals("ATTACK") || bonusType.equals("ACCURACY") || bonusType.equals("ENERGY");
}
@Override
public int getTotalArmor() {
return 0;
}
@Override
public int getTotalBaseArmor() {
return 0;
}
@Override
public void attack(Character target) {
energy += getEquipmentBonus("ENERGY");
if (energy < 10) {
int weakDamage = getAttackPower() / 2;
target.receiveDamage(weakDamage, false);
} else {
energy = consumeResource("ENERGY", 10, energy);
int damage = getAttackPower() + getEquipmentBonus("ATTACK");
System.out.println(getName() + " fires a basic arrow at " + target.getName() + "(Energy reduced by: 10)");
if (accuracy >= 80) {
damage += 5;
}
target.receiveDamage(damage, false);
if (target instanceof Warrior && target.getTotalArmor() > 0) {
if (target.getTotalArmor() >= 50) {
int ap2 = (int) Math.round(damage * 0.9);
System.out.println(
target.getName() + " takes " + ap2 + " damage. (Health: " + target.getHealth() + ")");
} else {
int ap2 = (int) Math.round(damage * 0.95);
System.out.println(
target.getName() + " takes " + ap2 + " damage. (Health: " + target.getHealth() + ")");
}
} else
System.out.println(
target.getName() + " takes " + damage + " damage. (Health: " + target.getHealth() + ")");
System.out.println(getName() + "'s current stats: " + getStatString());
}
}
@Override
public void specialAttack(Character target) {
int availableEnergy = energy + getEquipmentBonus("ENERGY");
if (availableEnergy < 15) {
attack(target);
return;
}
energy = consumeResource("ENERGY", 15, energy);
System.out.println(
getName() + " performs a critical arrow shot on " + target.getName() + " (Energy reduced by: 15)");
int damage = getAttackPower() + getEquipmentBonus("ATTACK") + 5;
if (accuracy >= 80) {
damage += 5;
}
boolean ignoreArmor = target instanceof Warrior;
target.receiveDamage(damage, ignoreArmor);
System.out.println("This attack ignores armor! Full damage applied.");
System.out.println(target.getName() + " takes " + damage + " damage." + "(Health: " + target.getHealth() + ")");
System.out.println(getName() + "'s current stats: " + getStatString());
}
@Override
public String getStatString() {
int totalAttack = getAttackPower() + getEquipmentBonus("ATTACK");
int totalAccuracy = accuracy + getEquipmentBonus("ACCURACY");
int totalEnergy = energy + getEquipmentBonus("ENERGY");
return "Total Attack: " + totalAttack + ", Total Accuracy: " + totalAccuracy + ", Total Energy:" + totalEnergy
+ ",Health: " + getHealth();
}
@Override
public String toString() {
return getName() + "(Health:" + getHealth() + ",Total Attack: " + getAttackPower() + ",Total Accuracy: "
+ accuracy + "," + " Total Energy: " + energy + "| Equipment: Eagle Eye[ATTACK Bonus: 10]"
+ ")";
}
}