-
Notifications
You must be signed in to change notification settings - Fork 0
/
JavamonTest.java
261 lines (179 loc) · 8.45 KB
/
JavamonTest.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
252
253
254
255
256
257
258
259
260
261
public class JavamonTest {
public static void testDefaultGets() {
System.out.println("TEST: Constructor # 1/ Default data fields / Getters / toString.");
Javamon j = new Javamon("MYNAME");
if (j.getHealth() != 100)
throw new RuntimeException("After instantiation, getHealth returns " + j.getHealth() + "; default should be 100, set in constructor");
if (j.getStrength() != 20)
throw new RuntimeException("After instantiation, getStrength returns " + j.getStrength() + "; default should be 20, set in constructor");
if (j.getAccuracy() != .8)
throw new RuntimeException("After instantiation, getAccuracy returns " + j.getAccuracy() + "; default should be .8, set in constructor");
if (!j.toString().equals("MYNAME [100]"))
throw new RuntimeException("toString returns \"" + j + "\"; not proper format/showing default health, should be \"MYNAME [100]\"");
System.out.println("\tPASSED!");
}
public static void testValidSetsAndGets(){
System.out.println("TEST: Getters & Setters (valid inputs).");
Javamon j = new Javamon("MYORIGNAME");
j.setName("MYNEWNAME");
if(!j.getName().equals("MYNEWNAME"))
throw new RuntimeException("Getter/Setter for name doesn't work");
j.setHealth(1234);
if (j.getHealth() != 1234)
throw new RuntimeException("Getter/Setter for health doesn't work");
j.setStrength(1234);
if (j.getStrength() != 1234)
throw new RuntimeException("Getter/Setter for strength doesn't work");
j.setAccuracy(.125);
if (j.getAccuracy() != .125)
throw new RuntimeException("Getter/Setter for accuracy doesn't work");
if (!j.toString().equals("MYNEWNAME [1234]"))
throw new RuntimeException("toString returns \"" + j + "\"; not showing updated name/health (or of proper format), should be \"MYNEWNAME [1234]\"");
System.out.println("\tPASSED!");
}
public static void testInvalidSetsAndGets(){
System.out.println("TEST: Setters (handling invalid inputs).");
Javamon j = new Javamon("MYNAME");
j.setHealth(-1);
if (j.getHealth() != 0)
throw new RuntimeException("setHealth allows negative values; should enforce minimum of 0 health.");
j.setStrength(0);
if (j.getStrength() != 1)
throw new RuntimeException("setStrength allows non-positive values; should enforce minimum of 1 strength.");
j.setAccuracy(.009);
if (j.getAccuracy() != .01)
throw new RuntimeException("setAccuracy allows values less than 1%; should enforce minimum of 1% accuracy.");
j.setAccuracy(1.1);
if (j.getAccuracy() != 1.0)
throw new RuntimeException("setAccuracy allows values above 100%; should enforce maximum of 100% accuracy.");
if (!j.toString().equals("MYNAME [0]"))
throw new RuntimeException("toString returns \"" + j + "\"; not showing updated name/health (or of proper format), should be \"MYNAME [0]\"");
System.out.println("\tPASSED!");
}
public static void testTakeDamageDefeated(){
System.out.println("TEST: takeDamage / isDefeated.");
Javamon j = new Javamon("MYNAME");
j.setHealth(10);
j.takeDamage(9);
if (j.getHealth() != 1)
throw new RuntimeException("takeDamage does not reduce health properly when taking damage less than health.");
if (j.isDefeated() )
throw new RuntimeException("isDefeated incorrectly states being defeated at 1 health.");
j.setHealth(10);
j.takeDamage(10);
if (j.getHealth() != 0)
throw new RuntimeException("takeDamage does not reduce health to 0 when taking damage equal to health.");
if (!j.isDefeated())
throw new RuntimeException("isDefeated incorrectly states being undefeated at 0 health.");
j.setHealth(10);
j.takeDamage(11);
if (j.getHealth() != 0)
throw new RuntimeException("takeDamage does not reduce health to 0 when taking damage greater than health.");
if (!j.isDefeated())
throw new RuntimeException("isDefeated incorrectly states being undefeated at 0 health.");
System.out.println("\tPASSED!");
}
public static void testCalculateAttackStrength(int strength){
System.out.println("TEST: calculateAttackStrength");
// Passing this test does not guarantee fully working,
// only basic sanity checks.
// Check report to get a better idea of how things are working.
int TRIALS = 10000;
Javamon j = new Javamon("MYNAME");
j.setStrength(strength);
int totalDamage = 0;
for (int i = 0; i < TRIALS; i++){
int damage = j.calculateAttackDamage();
if (damage <= 0){
throw new RuntimeException("calculateAttackDamage returned nonpositive damage; should never do this");
}
if (damage > strength){
throw new RuntimeException("calculateAttackDamage returned damage greater than strength; should never do this");
}
totalDamage += damage;
}
System.out.println("\tPASSED...? Check REPORT:");
System.out.printf("\tcalculateAttackDamage with %d strength over %d trials: Average %.1f\n", strength, TRIALS, (double) totalDamage / TRIALS);
}
public static void testCalculateAttackSuccess(double accuracy){
System.out.println("TEST: calculateAttackSuccess");
// Check report to get a better idea of how things are working.
int TRIALS = 1000000;
Javamon j = new Javamon("MYNAME");
j.setAccuracy(accuracy);
int totalHits = 0;
for (int i = 0; i < TRIALS; i++){
boolean hit = j.calculateAttackSuccess();
if (hit)
totalHits ++;
}
if (totalHits == 0){
throw new RuntimeException("calculateAttackSuccess always returns false.");
}
if (totalHits == TRIALS && accuracy != 1){
throw new RuntimeException("calculateAttackDamage always returns true.");
}
System.out.println("\tPASSED...? Check REPORT:");
System.out.printf("\tcalculateAttackSuccess with %.2f accuracy over %d trials: %d are trues\n", accuracy, TRIALS, totalHits);
}
public static void testAttemptAttack(int strength, double accuracy){
System.out.println("TEST: attemptAttack");
// Passing this test does not guarantee fully working,
// only basic sanity checks.
// Check report to get a better idea of how things are working.
int TRIALS = 10000;
Javamon j = new Javamon("Attacker");
Javamon j2 = new Javamon("Defender");
int enemyHealth = strength * 2;
//Run 1 - check valid attack damages (100% accuracy)
j.setHealth(100);
j.setStrength(strength);
j.setAccuracy(1);
for (int i = 0; i < TRIALS; i++){
j2.setHealth(enemyHealth);
j.attemptAttack(j2);
int damage = enemyHealth - j2.getHealth();
if (damage < 0 )
throw new RuntimeException("attemptAttack caused negative damage; should never do this");
if (damage == 0 )
throw new RuntimeException("attemptAttack caused 0 damage at 100% accuracy; should not ever miss");
if(damage > strength)
throw new RuntimeException("attemptAttack caused " + damage + " at 10 strength; should not deal more damage than strength");
if (j.getHealth() != 100)
throw new RuntimeException("attemptAttack changed the attacker's health; only the defender's health should change");
j2.setHealth(1);
j.attemptAttack(j2);
if (j2.getHealth() < 0 )
throw new RuntimeException("attemptAttack caused enemy health to become negative; should never do this");
}
System.out.println("\tPASSED...? Check REPORT:");
//Run 2 - try attacking with given accuracy
j.setStrength(strength);
j.setAccuracy(accuracy);
int countMisses = 0;
int totalDamage = 0;
for (int i = 0; i < TRIALS; i++){
j2.setHealth(enemyHealth);
j.attemptAttack(j2);
int damage = enemyHealth - j2.getHealth();
if (damage == 0) {
countMisses++;
}
else
totalDamage += damage;
}
int countHits = TRIALS - countMisses;
System.out.printf("\t%d attacks @ Str: %d & Acc: %.2f:\n\tHits: %d | Misses: %d\n\tAverage Damage dealt on hit: %.1f\n",
TRIALS, strength, accuracy, countHits, countMisses, (double) totalDamage/countHits);
}
public static void main(String[] args){
testDefaultGets();
testValidSetsAndGets();
testInvalidSetsAndGets();
testTakeDamageDefeated();
//You may change the parameters of the following tests.
testCalculateAttackStrength(50);
testCalculateAttackSuccess(.75);
testAttemptAttack(100, .5);
}
}