Solution to 8d24: Lucky battling fighters
See code at solutions/code/tutorialquestions/question8d24
As with the solution to 7ec8, compare your solution with mine. I decided to model the three strategies using an enumeration:
public enum Strategy {
AGGRESSIVE, DEFENSIVE, AVERAGE
}
A Fighter
is equipped with a strategy
field of this type. The implementations
of takeDamage
and calculateDamage
have been adjusted to use luck to
resist, or inflict more, damage, depending on the fighter's strategy.
For the Average
strategy, I decided that a Fighter
should test their luck if their
stamina has dropped below half of its original value. For this to work, I added an initialStamina
field to Fighter
. I made this field final
, because it should not change after
a Fighter
has been created.
In takeDamage
, I have defined two constants, AGGRESSIVE_RESISTANCE_THRESHOLD
and AVERAGE_RESISTANCE_THRESHOLD
, which are used to decide when an aggressive or average
fighter will test their luck in order to reduce inflicted damage. These constants make it easier to
understand the calculations my code is making, and would make it trivial to change these thresholds.
Similarly, in calculateDamage
I have defined two constants, AGGRESSIVE_MULTIPLIER
and MISS_PENALTY
which record the multiple by which damage should be increased, or the amount
by which damage should be reduced, when luck testing is successful and unsuccessful, respectively.