Beruflich Dokumente
Kultur Dokumente
K. B.
ABSTRACT
In this paper, I derive an expression for average number of hits required to kill a monster in a game called Maplestory.
The equation has no known analytical solution, so I have also developed a program that calculates the exact solution to
the equation. Due to the computational load involved in most typical instances of the average hits problem, I present two
separate approximation techniques that serve to obtain an approximated solution to the equation in a more timely manner;
these techniques are named "the mean expansion method" and "the density reduction method". Merits and limitations to
these methods are discussed, but the density reduction method is deemed as the most effective means of determining the
approximate solution to an instance of the average hits problem.
1 Background
The average hits problem finds its origins in a computer game called Maplestory. The objective of the game is to achieve the
maximum level by obtaining experience points (EXP). There are several ways to obtain such points, but one of the main ways
is to kill monsters. Monsters have a property called hit points (HP). The monster dies once it is left with no hit points, and the
player is subsequently awarded with the experience points associated with the particular monster.
The means by which the player kills the monster, or the combat mechanic, is key here. Essentially, the player deals damage
to monsters by hitting them. The damage dealt is typically a function of several things such as the monster’s weapon defense
and the player’s equipment and "skills", but after taking these precalculations into consideration, the player’s damage is always
an integer chosen at random between a minimum attack, hereto abbreviated as (atk)min , and a maximum attack (atk)max . The
attack range is then defined as R ≡ (atk)max − (atk)min + 1, so that the attack range expresses the total number of possible
integers for each hit.
Since the objective of the game is to reach reach the maximum level, it is natural to inquire into the quickest way of
achieving this goal. Given that killing monsters is arguably the primary method by which to level up, it follows that there exists
a kind of monster that will yield the quickest EXP gain rate. This rate is best measured with respect to hits required to kill the
EXP
monster. In other words the EXP gain rate, µ, shall be defined as the amount of EXP gained per hit: µ = Hit(HP,(atk) min ,(atk)max )
.
The number of hits required to kill a monster is actually a function of HP, (atk)min , and (atk)max , and this is actually where the
problem is born.
There are many different ways of killing a monster as determined by the parameters listed above, and each distinct
combination of integers within the attack range that end up killing the monster can be of different sizes. This implies that, for
any given monster, the number of hits it takes to kill it can range depending on the value of each element in the sequence of hits.
Because of this, it is necessary to quantify that average number of hits required to kill the monster. Hence, the average hits
problem presents itself.
In fact, the average hits problem is similar to the infamous change-making problem, which asks for the minimum number of
coins that add up to some amount of money. However, the average hits problem is different in several ways. Most importantly,
the total damage dealt to the monster may be more than the monster’s HP. Moreover, the average hits problem does not concern
itself with the minimum number of hits required; it would like to know all possible sequences of hits. Let each full sequence of
hits that leads to a kill be defined as a kill route. Once all kill route sizes are known, the average number of hits required can be
determined.
(atk)min + (atk)max
(atk)avg =
2
which in this case evaluates to 2. With this attack value, it would take 2 hits to kill the monster. This is the lowest integer value
that satisfies the kill requirement: (atk)avg × n ≥ HP. What is the actual average number of hits required to kill the monster,
though? A 2-valued attack will always take 2 hits to kill a monster with 4 HP, but there are many more ways to reach this HP
given the full attack range of 1-3. To see all possible combinations of elements within the attack range that satisfy the kill
requirement, it is convenient to use tree diagrams. All possible ways to kill a monster with 4 HP using an attack range of 1-3
are shown in the hit tree diagram below.
The hit tree diagram begins at the top with the indicated HP value. From there, the tree branches into the next possible hit
values. After each hit, if the monster is not yet dead, then any value within the attack range is equally probable for the next
hit. This is an important point; it is assumed that all values within the attack range are equally likely to occur at any given
hit. When reading a hit tree diagram, one must maintain a running tally of the current HP after each hit. In figure 1, the final
hit in the kill route that requires 2 total hits is circled in blue, 3 total hits in red, and 4 total hits in green. From the hit tree
diagram, then, it is easy to see that there are 5 possible ways to get the kill in 2 hits, 8 possible ways to get the kill in 3 hits, and
3 possible ways to get the kill in 4 hits. I will call the degeneracy in the number of ways to kill in n hits the multiplicity, and
the multiplicity function will be defined as Ω(n). With this definition, we can summarize the results of this hit tree as follows.
That is, the multiplicity of 1-hit-kills is 0 in this case, the multiplicity of 2-hit-kills is 6, and so forth. To obtain the average
number of hits required from the set of all possible kill routes, we could be tempted to use an ensemble average. Let the average
number of hits required be denoted by γ. It might then be natural to suppose that
∑∞
n=1 nΩ(n)
γ=
∑∞n=1 Ω(n)
However, this is not true. This expression is only valid if all possible kill routes are equally likely, but that is not the case.
To reiterate, only the hit values within the attack range are equally likely; certain kill routes are more likely than others. To
accurately compute the average number of hits required, we must determine the probability of each kill route occurring. Let us
evaluate these probabilities for the tree in figure 1.
2/9
There are 6 possible ways of killing the monster in 2 hits, but we must consider the probability of each of these ways. There
is a 1/3 chance of hitting a 3 in the first hit, and any attack value thereafter will yield a kill. The probability associated with this
is 13 × 33 . If the value of the first hit were a 2, there would be 2 possible, subsequent outcomes that yield the kill. In this case, we
have the probability 13 × 23 . Finally, if a 1 were hit first, the probability of achieving a 2-hit kill would be 13 × 13 . The sum of
these probabilities gives the overall probability of getting a 2-hit kill:
1 3 1 2 1 1 2
P(2) = × + × + × =⇒ P(2) =
3 3 3 3 3 3 3
Here, P(2) represents the probability of a 2-hit kill. We may rearrange the expression above to reveal a pattern.
1 3 1 2 1 1 13 2 1 1 Ω(2)
P(2) = × + × + × = + + = (3 + 2 + 1) = 2
3 3 3 3 3 3 3 3 3 3 9 3
In general, we can say that the probability of an n-hit kill is
Ω(n)
P(n) = n
(atk)max − (atk)min + 1
∞ ∞
nΩ(n)
γ= ∑ nP(n) = ∑ n
n=1 n=1 R
where R ≡ (atk)max − (atk)min + 1. This is simply the total attack range. We can use the expression above to find the actual
average number of hits required.
2 Ω(3) 8 Ω(4) 3
P(1) = 0, P(2) = , P(3) = 3 = , P(4) = 4 =
3 3 27 3 81
∴ γ = 1 × 0 + 2 × 32 + 3 × 278 3
+ 4 × 81 =⇒ γ ≈ 2.37
This result is not too far removed from my initial prediction of 2 hits-to-kill, but it shows that the first-order approximation
is inappropriate to accurately determine the correct result. There are countless conceivable cases where the first-order
approximation fails. For example, if the monster’s HP were located at the average of the attack range, then the first-order
approximation would claim that it would take an average of 1 hit to kill the monster. Yet, we know that half the time the monster
should die in a single hit, and the other half of the time, it should take more than 1 hit. As a result, we should not rely on the
first-order approximation to return acceptable values.
3/9
new array are filtered out based on their value relative to the monster’s HP, and the number of entries removed is recorded. This
process continues until the vector is empty and the complete multiplicity function is consequently constructed.
To illustrate this process, suppose that the attack range is 1-3 and the monster’s HP is 3. The evolution of the population
would then develop like so:
2 3 4
1 2 3 → 1
2 →
3 4 5
→ 2 3
4 3 4 5 → 2 → 3 4 5 → | |
In the first step, the population is initialized from the attack range. The population is then filtered for damage values that are
greater than or equal to the HP threshold. In this case, the 3 gets removed, leaving a vector containing the elements 1 and 2.
Here, the algorithm establishes that Ω(1) = 1. The third step shows the resulting array from adding the attack range to both
elements of the reduced vector. The top row of the array displays the result of adding all values between and including 1 and 3
to the first vector entry of 1, and the second row of the array shows the result of the performed addition on the second vector
entry of 2. The array is then rearranged in step 4 to form a single vector containing the entire population created in the previous
step. Again, for the following step, this vector is reduced by filtering the damage values. In this case, 5 entries were removed
so that Ω(2) = 5. The process repeats, and the final nonzero output for the multiplicity function is Ω(3) = 3. With this, the
multiplicity function has been fully defined. Finally, the algorithm performs the remaining calculation as shown in the boxed
equation above to determine the precise number of average hits required.
4 Approximation Methods
Unfortunately, the method used above works for very small hit trees relative to the size of those found in practice. For hit trees
with large HP and large attack range, it is very difficult to construct and/or compute the multiplicity functions numerically. In
fact, if the HP is n times greater than the maximum attack value, then there will be at least Rn kill routes. For large ranges and
large HP values, this statement suggests that obtaining an average hit number becomes computationally taxing. In light of this,
I initially attempted to arrive at an analytical expression for the multiplicity function, but I could not. Therefore, I will focus on
a numerical approach for estimating the number of average hits required via extrapolation in a 3-dimensional space comprised
of minimum and maximum attack and monster HP.
There are countless ways to approximate the solution to an instance of the average hits problem. More pertinently, for any
combination of minimum attack, maximum attack, and monster hp, there will be an exact solution which may be approximated
using suitable techniques. Below, I have outlined two such techniques that aim to maximize solution accuracy and minimize
computational load. As an important disclaimer, these techniques are demonstrated via case studies on a single instance of the
average hits problem. The intention is to use this case as a canonical example that illustrates a typical solution space for the
problem. There are undoubtedly plenty of cases where the solution space may take on different and varying forms, but most
instances of the average hits problem should exhibit similar characteristics to that of the considered case.
Before presenting these methods, though, I would like to briefly touch on some other approaches I took. Initially, I tried
fixing the attack range and varying HP to find some sort of pattern that would allow me to extrapolate into higher HP values– the
motivation being that higher HP values lead to heavier computational load. I did find that, for most cases, beyond a certain point,
the relationship between average hits required and monster HP was linear for a fixed attack range. This was initially promising.
However, even if this were always the case, obtaining the constant of proportionality between monster HP and hits required
proved to be intractable because the problem is fundamentally numerical anyway. Nonetheless, the relationship is not always
linear. For certain attack ranges and HP values, I observed nonlinear behavior of varying nature. My best assessment of these
observations is that the complexity of the multiplicity function, which hitherto has no known analytical solution, complicates
the relationship between the average number of hits required and monster HP. While I had originally imagined single-variable
extrapolation would suffice, these shortcomings eventually led me to explore the space over two variable dimensions.
4/9
(a) Full Surface (b) Mesh Grid with Contours
similar-valued average hits required. It would be interesting, then, to see how the locus of points that define the surface contours
are related.
Figure 2(b) shows the top-down view of figure 2(a) but as a mesh with contour lines. The mesh is exact and only connects
each available data point, but the contour lines are not entirely accurate. Given the discrete nature of the data, Matlab must
interpolate between values to determine where the function is constant. Since discrete-valued functions are typically sparse
(discontinuous) when dealing with small domains, the contour line serves mostly as an approximation, but the contour lines do
reveal an interesting result.
The output associated with the attack range domain defined by a selected average attack tends to remain about the same,
or it may approach some value as the attack range increases about said average attack. To see this, consider figure 2(b). I
have labeled two sets of four points that correspond to the same average attack. The bottom set of points, corresponding to an
average attack of 4, align quite nicely with the contour line as the attack range increases about this average value. The top
set of points, corresponding to an average attack of 5, does not have a reference contour line, but the points’ corresponding
hits required values tend toward some amount as attack range is increased about the average attack. Despite the difference
in behavior between these two set of points, it suggests a powerful approach for approximating the average number of hits
required for large attack ranges.
Explicitly, the average number of hits required can be approximated by expanding an artificial attack range about the
average attack value of the original attack range– hence the name "mean expansion". For example, suppose I would like to
know the average number of hits required to kill a monster with 10 HP if my attack range were 2-8. Then, given figure 2(b), I
could run the computation using the average attack of 5 and an expansion factor (EF) of 1. Using an expansion factor of 1
simply means that the attack range will span 1 unit to the right and 1 unit to the left of the average attack. In this case, An
expansion factor of 1 would yield an artificial attack range of 4-6. Running the computation with this artificial range results in
an average of about 2.333 hits required to kill the monster. This is about a 6% error when compared to the actual value of 2.489.
The benefit of using this approximation technique is that it reduces the computation time significantly for larger attack ranges.
With the concept of mean expansion established, let us apply it to a typical instance of the average hits problem. Figure
3(a) shows how increasing expansion factor affects the prediction for average hits required over a range of HP values. As
the expansion factor is increased, the curves deviate away from a step-function-like shape; when the expansion factor is zero,
the associated curve would be a step function. Plateau behavior is observed for expansion factors up to about 15, and this is
because of the clumped nature of the artificial attack range. As the expansion factor approaches 20, the plateau length decreases
because of the span of the artificial attack range. With increasing expansion factor, the curves decrease in slope and pivot about
a certain point. The 40.5 HP mark shows a striking result.
In average-attack-valued increments along the HP axis, all curves appear to pass through the same point. However, as
monster HP increases to several times the average attack value, this coalescent behavior weakens. Immediately, in figure 3, it
can be seen that the second instance of coalescence is a bit more loose than the sharpness of the first one. Nonetheless, this
observation is useful because it makes clear that regardless of the expansion factor, the EF curve will always pass through some
multiple of 0.5 at an HP value corresponding to some multiple of the average attack. It may be tempting to conclude that a
5/9
linear model may yield an adequate approximation, but this is exactly what I have been trying to avoid due to its inability to
capture nonlinear behavior like the one present at around 60 HP. To reiterate, the curves do not pass exactly through the same
point, but this behavior shows that the curve will pass very near this point of coalescence.
6/9
obtaining accurate approximations; it converges much more quickly as spacing is reduced, and it achieves more accurate results
for the same number of attack range elements.
(a) Varying Spacing for Attack Range 20-60 (b) Effect of Spacing on Error
5 Conclusion
The average hits equation is well-posed, but there does not exist a closed-form solution for it. Accordingly, an algorithm has
been presented that numerically solves for the multiplicity function and ultimately the average hits equation. Because of the
inherent, numerical complexity of the problem, the algorithm that solves for the exact solution quickly becomes intractable at
higher attack ranges. To remedy this issue, two approximation methods have been presented. Though the mean expansion
method reveals an interesting fact about the mathematical space and is useful for setting upper and lower bounds to the
target solution, the density reduction technique is ultimately more effective at actually attaining an acceptably accurate result.
Therefore, for most instances of the average hits problem, I finally conclude that the density reduction method should be used.
With high-end resources or for lower attack ranges and HP values, the exact solution algorithm may be employed.
7/9
Appendix
Exact Solution Algorithm
function [avg_hits]=getavghits(atkmin,atkmax,hp)
range=[atkmin:atkmax];
rangesize=size(range);
%First hit
n=1;
multiplicity(n)=0;
population=range;
populationsize=size(population);
k=1;
while k<=populationsize(2)
if population(k)>=hp
population(k)=[];
multiplicity(n)=multiplicity(n)+1;
k=k-1;
end
k=k+1;
populationsize=size(population);
end
%Subsequent Hits
while isempty(population)==0
clear newpopulation;
n=n+1;
multiplicity(n)=0;
for k=1:populationsize(2)
newpopulation(k,:)=population(k)+range;
end
newpopulationsize=size(newpopulation);
row=1;
for p=1:rangesize(2):(newpopulationsize(1)*newpopulationsize(2)-newpopulationsize(2)+1)
population(1,p:p+rangesize(2)-1)=newpopulation(row,:);
row=row+1;
end
populationsize=size(population);
k=1;
while k<=populationsize(2)
if population(k)>=hp
population(k)=[];
multiplicity(n)=multiplicity(n)+1;
k=k-1;
end
k=k+1;
populationsize=size(population);
end
populationsize=size(population);
end
8/9
multiplicitysize=size(multiplicity);
for s=1:multiplicitysize(2)
probability(s)=multiplicity(s)/(rangesize(2))^s;
end
avg_hits=0;
for k=1:multiplicitysize(2)
avg_hits=avg_hits+k*probability(k);
end
end
9/9