-
Notifications
You must be signed in to change notification settings - Fork 1
/
BasicUnit.java
215 lines (174 loc) · 4.85 KB
/
BasicUnit.java
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
package BattleSimulator;
import java.util.ArrayList;
public abstract class BasicUnit
{
private int soldierNumber = 50;
private double basicAttack = 0.5;
private double attack;
private String armyName;
private int damageTaken;
public ArrayList<BasicUnit> enemyList = new ArrayList<BasicUnit>();
public ArrayList<BasicUnit> defenderList = new ArrayList<BasicUnit>();
public BasicUnit(String name)
{
armyName = name;
}
public String getArmyName()
{
return armyName;
}
public String displayArmyName()
{
return "*" + armyName + "*";
}
// ensuring getters for attack and defense preferences
public abstract String getHighest();
public abstract String getHigh();
public abstract String getNeutral();
public abstract String getLow();
public abstract String getLowest();
public abstract String getDefend1();
public abstract String getDefend2();
public abstract String getName();
public abstract String getType();
public int getSoldierNumber()
{
return soldierNumber;
}
public void setSoldierNumber(int n)
{
soldierNumber = n;
}
public void addDamage(int n)
{
damageTaken += n;
}
public void applyDamage()
{
setSoldierNumber(getSoldierNumber() - damageTaken);
if (getSoldierNumber() < 0)
setSoldierNumber(0);
damageTaken = 0;
}
public void setBasicAttack(double n)
{
basicAttack = n;
}
// attack depends on the types of units involved
public double getAttack(BasicUnit unit)
{
if (unit.getType().equals(getHighest()))
{
attack = basicAttack + 0.25;
return attack;
}
else if (unit.getType().equals(getHigh()))
{
attack = basicAttack + 0.15;
return attack;
}
else if (unit.getType().equals(getLow()))
{
attack = basicAttack - 0.1;
return attack;
}
else if (unit.getType().equals(getLowest()))
{
attack = basicAttack - 0.2;
return attack;
}
else
return basicAttack;
}
public BasicUnit getAttackTarget(Army army)
{
BasicUnit target;
this.setBasicAttack(0.5);
if (army.checkType(getHighest()))
target = army.getRandomUnit(getHighest());
else if (army.checkType(getHigh()))
target = army.getRandomUnit(getHigh());
else if (army.checkType(getType()) || army.checkType(getNeutral()))
{
if (army.checkType(getType()) && ! army.checkType(getNeutral()))
target = army.getRandomUnit(getType());
else if (! army.checkType(getType()) && army.checkType(getNeutral()))
target = army.getRandomUnit(getNeutral());
else
{
int random = 1 + (int)(Math.random() * 2);
if (random == 1)
target = army.getRandomUnit(getType());
else
target = army.getRandomUnit(getNeutral());
}
}
else
target = army.getRandomTypeUnit();
System.out.printf("%35s attacks %-28s\n", this.getName(), target.getName());
return target;
}
public BasicUnit getDefenseTarget(Army army)
{
BasicUnit target;
this.setBasicAttack(0.5);
if (army.checkType(getDefend1()))
target = army.getRandomUnit(getDefend1());
else if (army.checkType(getDefend2()))
target = army.getRandomUnit(getDefend2());
else
target = army.getRandomTypeUnit();
System.out.printf("%35s defends %-28s\n", this.getName(), target.getName());
return target;
}
public BasicUnit getRandomTarget(Army army1, Army army2)
{
int number = 1 + (int)(Math.random() * 2);
BasicUnit armyTest;
BasicUnit target;
armyTest = army1.getRandomTypeUnit();
if (this.getArmyName().equals(armyTest.getArmyName()))
{
if (number == 1)
target = this.getAttackTarget(army2);
else
target = this.getDefenseTarget(army1);
}
else
{
if (number == 1)
target = this.getAttackTarget(army1);
else
target = this.getDefenseTarget(army2);
}
return target;
}
public int findUnit(BasicUnit[][] pairs)
{
for (int i = 0; i < pairs.length; i++)
if (this.getName().equals(pairs[i][0].getName()))
return i;
return -1;
}
public ArrayList<BasicUnit> getCurrentEnemies(BasicUnit[][] targets)
{
for (int i = 0; i < targets.length; i++)
{
if (targets[i][0].getName().equals(this.getName()) &&
! targets[i][0].getArmyName().equals(targets[i][1].getArmyName()))
enemyList.add(targets[i][1]);
if (targets[i][1].getName().equals(this.getName()) &&
! targets[i][0].getArmyName().equals(targets[i][1].getArmyName()))
enemyList.add(targets[i][0]);
}
return enemyList;
}
public ArrayList<BasicUnit> getCurrentDefenders(BasicUnit[][] targets)
{
for (int i = 0; i < targets.length; i++)
if (targets[i][1].getName().equals(this.getName())
&& targets[i][0].getArmyName().equals(this.getArmyName()))
defenderList.add(targets[i][0]);
return defenderList;
}
}