Repository navigation
Expand file tree
/
Copy pathGameSystem.java
More file actions
284 lines (247 loc) · 9.67 KB
/
Copy pathGameSystem.java
File metadata and controls
284 lines (247 loc) · 9.67 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
import java.util.*;
public class GameSystem {
public static void main(String[] args) {
Warrior w = new Warrior("DUYGU", 100, 20, 6, 3, 60);
Mage m = new Mage("GANDALF", 70, 15, 2, 1, 30);
Archer a = new Archer("Robin", 80, 18, 4, 2, 75);
Item sword = new Item("Sword of Might", "ATTACK", 5);
w.addItemToInventory(sword);
w.equipItemFromInventory(0);
w.warriorAttack(m);
}
}
class Item {
private String name;
private String bonusType;
private int bonusValue;
public Item(String name, String bonusType, int bonusValue) {
this.name = name;
this.bonusType = bonusType;
this.bonusValue = bonusValue;
}
public String getName() {
return name;
}
public String getBonusType() {
return bonusType;
}
public int getBonusValue() {
return bonusValue;
}
public String toString() {
return getName() + " [" + getBonusType() + "Bonus:" + getBonusValue() + "]";
}
}
class Character {
protected String name;
private int health;
protected int attackPower;
Item[] inventory;
Item[] equipment;
protected int inventoryCapacity;
protected int equipmentCapacity;
int bonushealth = 0;
int bonusaccuracy = 0;
int bonusmana = 0;
int bonusAttackPower = 0;
public Character(String name, int health, int attackPower, int inventoryCapacity, int equipmentCapacity) {
this.name = name;
this.health = health;
this.attackPower = attackPower;
this.inventoryCapacity = inventoryCapacity;
this.equipmentCapacity = equipmentCapacity;
this.equipment = new Item[equipmentCapacity];
this.inventory = new Item[inventoryCapacity];
}
public void takeDamage(int damage) {
health = (health + bonushealth) - damage;
if (health < 0) {
health = 0;
}
System.out.println(name + " takes " + damage + " damage. (Current Health: " + health + ")");
}
public String toString() {
return name + " [Current Health: " + (health) + ", Total Attack: " + (attackPower + bonusAttackPower) + ", ";
}
public void addItemToInventory(Item ıtem) {
for (int i = 0; i < inventory.length; i++) {
if (inventory[i] == null) {
inventory[i] = ıtem;
break;
}
}
}
public void removeItemFromInventory(int index) {
if (index < 0 || index >= inventory.length || inventory[index] == null) {
System.out.println("Invalid index.");
return;
}
for (int i = index; i < inventory.length - 1; i++) {
inventory[i] = inventory[i + 1];
}
inventory[inventory.length - 1] = null;
}
public void equipItemFromInventory(int invIndex) {
if (invIndex < 0 || invIndex >= inventory.length || inventory[invIndex] == null) {
System.out.println("Invalid index");
return;
}
for (int j = 0; j < equipment.length; j++) {
if (equipment[j] == null) {
equipment[j] = inventory[invIndex];
System.out.println(name + " equips " + equipment[j].getName() + " from inventory." + " (Bonus "
+ equipment[j].getBonusType() + ": " + equipment[j].getBonusValue() + ")");
if (equipment[j].getBonusType().equals("HEALTH")) {
bonushealth += equipment[j].getBonusValue();
}
if (equipment[j].getBonusType().equals("ATTACK")) {
bonusAttackPower += equipment[j].getBonusValue();
}
if (equipment[j].getBonusType().equals("MANA") && this instanceof Mage) {
bonusmana += equipment[j].getBonusValue();
}
if (equipment[j].getBonusType().equals("ACCURACY") && this instanceof Archer) {
bonusaccuracy += equipment[j].getBonusValue();
}
removeItemFromInventory(invIndex);
return;
}
}
}
public void unequipItem(int equipIndex) {
if (equipIndex < 0 || equipIndex >= equipment.length || equipment[equipIndex] == null) {
System.out.println("Invalid index.");
return;
}
Item removed = equipment[equipIndex];
addItemToInventory(removed);
System.out.println(name + " removes " + equipment[equipIndex].getName() + " from equipment. (BONUS "
+ equipment[equipIndex].getBonusType() + ": 0)");
switch (removed.getBonusType()) {
case "HEALTH":
bonushealth -= removed.getBonusValue();
health -= bonushealth;
break;
case "ATTACK":
bonusAttackPower -= removed.getBonusValue();
attackPower -= bonusAttackPower;
break;
case "MANA":
if (this instanceof Mage) {
bonusmana -= removed.getBonusValue();
}
break;
case "ACCURACY":
if (this instanceof Archer) {
bonusaccuracy -= removed.getBonusValue();
}
break;
}
equipment[equipIndex] = null;
for (int i = equipIndex; i < equipment.length - 1; i++) {
equipment[i] = equipment[i + 1];
}
equipment[equipment.length - 1] = null;
}
}
class Warrior extends Character {
private double armor;
public Warrior(String name, int health, int attackPower, int inventoryCapacity, int equipmentCapacity,
double armor) {
super(name, health, attackPower, 6, 3);
this.armor = armor;
}
public void warriorTakeDamage(int damage, boolean IgnoreArmor) {
if (IgnoreArmor == true || armor == 0) {
takeDamage(damage);
} else {
System.out.println(this.name + "'s damage reduced.");
if (armor >= 50) {
int damagereduction = (int) Math.round(damage * 90.0 / 100.0);
takeDamage(damagereduction);
} else {
int damagereduction2 = (int) Math.round(damage * 95.0 / 100.0);
takeDamage(damagereduction2);
}
}
armor -= 10;
System.out.println(name + " Remaning Armor: " + armor);
}
public void warriorAttack(Character target) {
int totalAttackPower = attackPower + bonusAttackPower;
System.out.println(name + " attacks " + target.name + " with total power " + totalAttackPower);
if (target instanceof Warrior) {
((Warrior) target).warriorTakeDamage(attackPower, false);
} else {
target.takeDamage(totalAttackPower);
}
}
@Override
public String toString() {
return "Warrior " + super.toString() + "Current Armor: " + armor + ", " + "Bonus Health: " + bonushealth
+ ", Bonus AttackPower: " + bonusAttackPower + "]";
}
}
class Mage extends Character {
private int mana;
public Mage(String name, int health, int attackPower, int inventoryCapacity, int equipmentCapacity, int mana) {
super(name, health, attackPower, 2, 1); // inventory = 2, equipment = 1
this.mana = mana;
}
public void mageAttack(Character target) {
int totalAttackPower = attackPower + bonusAttackPower;
int totalmana = mana + bonusmana;
if (mana >= 10) {
System.out.println(
this.name + " casts a spell on " + target.name + " dealing " + (totalAttackPower + 5) + " damage!");
if (target instanceof Warrior) {
((Warrior) target).warriorTakeDamage(totalAttackPower + 5, false);
} else {
target.takeDamage(totalAttackPower + 5);
}
if (mana > 10) {
mana -= 10;
} else {
mana = 0;
}
System.out.println(this.name + "'s Remaning Mana: " + mana);
} else {
System.out.println(
this.name + " casts a spell on " + target.name + " dealing " + totalAttackPower + " damage!");
if (target instanceof Warrior) {
((Warrior) target).warriorTakeDamage(totalAttackPower, false);
} else {
target.takeDamage(totalAttackPower);
}
}
}
public String toString() {
return "Mage " + super.toString() + "Current mana: " + mana + ", Bonus Health: " + bonushealth
+ ", Bonus AttackPower: " + bonusAttackPower + ", Bonus MANA: " + bonusmana + "]";
}
}
class Archer extends Character {
private int accuracy;
public Archer(String name, int health, int attackPower, int inventoryCapacity, int equipmentCapacity,
int accuracy) {
super(name, health, attackPower, 4, 2);
this.accuracy = accuracy;
}
public void archerAttack(Character target) {
int totalAttackPower = attackPower + bonusAttackPower;
int totalAccuracy = accuracy + bonusaccuracy;
if (totalAccuracy >= 80) {
totalAttackPower = totalAttackPower + 5;
}
System.out.println(name + " attacks " + target.name + " with total power " + totalAttackPower);
if (target instanceof Warrior) {
((Warrior) target).warriorTakeDamage(totalAttackPower, false);
} else {
target.takeDamage(totalAttackPower);
}
}
public String toString() {
return "Archer " + super.toString() + "Accuracy: " + accuracy + ", Bonus Health: " + bonushealth
+ ", Bonus AttackPower: " + bonusAttackPower + ", Bonus ACCURACY: " + bonusaccuracy + "]";
}
}