Game Development Reference
In-Depth Information
The locations of the edges of the buckets do change, however, and this is where
our focus must lie.
B UILDING B UCKETS
The numbers at the bottom of each of the two depictions in Figure 12.2 represent
the cumulative sizes of the buckets that we have added. For example, in the top
group, the first bucket we added was the probability of the group that guesses “33.�
We had determined that the size of that group was 4%. The edge of this bucket is,
therefore, 4. If our random number is 1, 2, 3, or 4, we pick the first group.
It is important that we notice that the right edge of a bucket is inclusive. For the
bucket above, the edge is 4, not 5. We can think of this as being “anything in the 4s
is still fair game… but 5 is on the other side.� This will be an important distinction
to remember as we write our code later.
The second group that we add—the “22� guessers—occurs 3% of the time. We
add this 3% to the original 4% from the first group. Therefore, they would occupy
the next three slots on the number line—5, 6, and 7. The bucket edge for this group
would be 7. As we will explore later, we only need to store one edge for each bucket.
We can infer the other edge by the bucket immediately to the left.
The third group, the random guessers, represented 30% of the whole. As above,
we add 30 to the edge of the preceding group (7). Therefore, the edge of this bucket
would be 37. If our randomly generated number falls anywhere in the range of 8 to
37, we select the third bucket.
Naturally, we repeat the process for the fourth bucket, the semi-logical
guessers. The width of their bucket is 63; their bucket spans the range from 38 to
100. It is important that we do not simply assume that anything that doesn't land in
the first three buckets lands in the fourth. We need to make sure that we keep track
of the actual width that we intend the last bucket to be. The reason this is impor-
tant is that we do not want to assume the total width for the combined buckets. We
shall revisit this issue in a moment.
P UTTING I TIN C ODE
In the previous chapter, we laid out some code for selecting which of the four groups
of guessers we were going to generate. The code for that was relatively simple.
GUESS_TYPE CGuesser::GetGuessType()
{
int index = DieRoller.SingleDie( 100, false );
Search WWH ::




Custom Search