Game Development Reference
In-Depth Information
} // end for
// Index didn't land in a bucket!
assert( 0 && “Index out of range� );
// As a default, however, we will return a random guesser
return GUESS_RANDOM;
}
The first thing we did in the function GetGuessType() is to generate our ran-
dom number, index . There is one change to this line from the technique we used
before. Instead of hard-coding the number 100, we changed our random number
call to be between 1 and mMaxIndex . This is important. We are now set up to gen-
erate a random number between 1 and whatever the combined width of all of our
buckets happens to be. For example, if we decided that our “33� guess bucket was
a width of 8 wide rather than 4 and made no other changes , the total width of all
buckets would be 104 rather than 100 (notice that we are no longer saying “per-
cent�). If we had continued to generate a random number between 1 and 100, we
would not be giving full credit to the bucket that we have now pushed to the right—
ending at 104 instead of 100. We will address how dynamic bucket widths can be
used to our advantage a little later on.
The next statement in the function sets NumBuckets to the number of buckets
we have in our vector. Again, this is something that we can leverage. If we decide to
add a fifth type of guesser to our experiment, this code would automatically ac-
count for it.
Once we know the number of buckets that we are going to search, we loop
through them. The test is the same as we did before: We check to see if our ran-
domly generated index is less than the edge of the current bucket designated by
mvGuessTypeList[i] . If it is, we return the GuessType associated with that bucket.
If not, we move on.
Note that this code should always return a GuessType before it exits the loop.
I leave it to you, gentle reader, to insert error-trapping code of your choice (such
as my assert() function), return a default GuessType , or devise any other
manner of graceful exit.
There is another method of finding out in which bucket our metaphorical
bouncing ball landed. It will be much more entertaining to set up a few more buck-
ets to search before we open the lid on that method.
Search WWH ::




Custom Search