-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArmy.java
251 lines (204 loc) · 6.49 KB
/
Army.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
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
package BattleSimulator;
import java.util.ArrayList;
public class Army
{
private ArrayList<Archers> archersList = new ArrayList<Archers>();
private ArrayList<CavalryArchers> cavalryArchersList = new ArrayList<CavalryArchers>();
private ArrayList<HeavyCavalry> heavyCavalryList = new ArrayList<HeavyCavalry>();
private ArrayList<Infantry> infantryList = new ArrayList<Infantry>();
private ArrayList<LightCavalry> lightCavalryList = new ArrayList<LightCavalry>();
private ArrayList<Spearmen> spearmenList = new ArrayList<Spearmen>();
public ArrayList<BasicUnit> wholeArmy = new ArrayList<BasicUnit>();
private int totalUnits;
private String armyName;
// populates each unit's list with the number of units got from the user
public Army(String name)
{
armyName = name;
System.out.print("Archers: ");
int archers = InputTester.checkInteger();
for (int i = 0; i < archers; i++)
archersList.add(new Archers(name));
System.out.print("Cavalry Archers: ");
int cavalryArchers = InputTester.checkInteger();
for (int i = 0; i < cavalryArchers; i++)
cavalryArchersList.add(new CavalryArchers(name));
System.out.print("Heavy Cavalry: ");
int heavyCavalry = InputTester.checkInteger();
for (int i = 0; i < heavyCavalry; i++)
heavyCavalryList.add(new HeavyCavalry(name));
System.out.print("Infantry: ");
int infantry = InputTester.checkInteger();
for (int i = 0; i < infantry; i++)
infantryList.add(new Infantry(name));
System.out.print("Light Cavalry: ");
int lightCavalry = InputTester.checkInteger();
for (int i = 0; i < lightCavalry; i++)
lightCavalryList.add(new LightCavalry(name));
System.out.print("Spearmen: ");
int spearmen = InputTester.checkInteger();
for (int i = 0; i < spearmen; i++)
spearmenList.add(new Spearmen(name));
constructWholeArmy();
System.out.println();
}
public Army(String name, int n)
{
armyName = name;
for (int i = 0; i < n; i++)
{
int randomNumber = 1 + (int)(Math.random() * 6);
switch (randomNumber)
{
case 1:
archersList.add(new Archers(name));
break;
case 2:
cavalryArchersList.add(new CavalryArchers(name));
break;
case 3:
heavyCavalryList.add(new HeavyCavalry(name));
break;
case 4:
infantryList.add(new Infantry(name));
break;
case 5:
lightCavalryList.add(new LightCavalry(name));
break;
case 6:
spearmenList.add(new Spearmen(name));
break;
}
}
constructWholeArmy();
}
public String getArmyName()
{
return armyName;
}
public void constructWholeArmy()
{
for (int i = 0; i < archersList.size(); i++)
wholeArmy.add(archersList.get(i));
for (int i = 0; i < cavalryArchersList.size(); i++)
wholeArmy.add(cavalryArchersList.get(i));
for (int i = 0; i < heavyCavalryList.size(); i++)
wholeArmy.add(heavyCavalryList.get(i));
for (int i = 0; i < infantryList.size(); i++)
wholeArmy.add(infantryList.get(i));
for (int i = 0; i < lightCavalryList.size(); i++)
wholeArmy.add(lightCavalryList.get(i));
for (int i = 0; i < spearmenList.size(); i++)
wholeArmy.add(spearmenList.get(i));
}
public void removeUnit(BasicUnit unit)
{
String unitType = unit.getType();
switch (unitType)
{
case "Archers":
archersList.remove(unit);
break;
case "Cavalry Archers":
cavalryArchersList.remove(unit);
break;
case "Heavy Cavalry":
heavyCavalryList.remove(unit);
break;
case "Infantry":
infantryList.remove(unit);
break;
case "Light Cavalry":
lightCavalryList.remove(unit);
break;
case "Spearmen":
spearmenList.remove(unit);
break;
}
wholeArmy.clear();
constructWholeArmy();
}
public void printArmy()
{
System.out.println(armyName + " Archers: " + archersList.size());
System.out.println(armyName + " Cavalry Archers: " + cavalryArchersList.size());
System.out.println(armyName + " Heavy Cavalry: " + heavyCavalryList.size());
System.out.println(armyName + " Infantry: " + infantryList.size());
System.out.println(armyName + " Light Cavalry: " + lightCavalryList.size());
System.out.println(armyName + " Spearmen: " + spearmenList.size());
}
public BasicUnit getRandomUnit(String name)
{
int n;
if (! checkType(name))
{
System.out.println("There are no units of that type!");
return getRandomTypeUnit();
}
switch (name)
{
case "Archers":
n = (int)(Math.random() * archersList.size());
return archersList.get(n);
case "Cavalry Archers":
n = (int)(Math.random() * cavalryArchersList.size());
return cavalryArchersList.get(n);
case "Heavy Cavalry":
n = (int)(Math.random() * heavyCavalryList.size());
return heavyCavalryList.get(n);
case "Infantry":
n = (int)(Math.random() * infantryList.size());
return infantryList.get(n);
case "Light Cavalry":
n = (int)(Math.random() * lightCavalryList.size());
return lightCavalryList.get(n);
default:
n = (int)(Math.random() * spearmenList.size());
return spearmenList.get(n);
}
}
public BasicUnit getRandomTypeUnit()
{
ArrayList<BasicUnit> allUnits = new ArrayList<BasicUnit>();
archersList.forEach((unit) -> allUnits.add(unit));
cavalryArchersList.forEach((unit) -> allUnits.add(unit));
heavyCavalryList.forEach((unit) -> allUnits.add(unit));
infantryList.forEach((unit) -> allUnits.add(unit));
lightCavalryList.forEach((unit) -> allUnits.add(unit));
spearmenList.forEach((unit) -> allUnits.add(unit));
int randomNumber = (int)(Math.random() * allUnits.size());
return allUnits.get(randomNumber);
}
public int getTotalUnits()
{
totalUnits = wholeArmy.size();
return totalUnits;
}
// checks whether a specific type is present in the current army
public boolean checkType(String name)
{
boolean check = false;
switch(name)
{
case "Archers":
check = ! archersList.isEmpty();
break;
case "Cavalry Archers":
check = ! cavalryArchersList.isEmpty();
break;
case "Heavy Cavalry":
check = ! heavyCavalryList.isEmpty();
break;
case "Infantry":
check = ! infantryList.isEmpty();
break;
case "Light Cavalry":
check = ! lightCavalryList.isEmpty();
break;
case "Spearmen":
check = ! spearmenList.isEmpty();
break;
}
return check;
}
}