Game Development Reference
In-Depth Information
// 1..4 = 4
if ( index <= 4 ) return GUESS_33;
// 5..7 = 3
if ( index <= 7 ) return GUESS_22;
// 8..37 = 30
if ( index <= 37 ) return GUESS_RANDOM;
// 38..100 = 63
return GUESS_SEMI;
}
By looking closely, we can see some familiar numbers. In each of the if state-
ments, we were checking to see if the random number was less than a specified
number. The first one, if ( index <= 4 ) , is testing to see if index lands in the
first bucket (the “33� guessers). Likewise, the second statement, if ( index <= 7 ) ,
is testing to see if our random number lands in the second bucket—between 5 and
7 inclusive. This continues to the third if statement, checking to see if the number
is between 8 and 37 (inclusive). If we have not exited the routine after the third
statement, the number is above 37, and we return GUESS_SEMI from the function.
This arrangement is a very familiar construct to most programmers. While it is
certainly functional, it has one serious drawback. If we want to change the bucket
widths—even just one of them, we have to change some (or even all ) of the if state-
ments. Specifically, we have to change the statement for the bucket we are chang-
ing the size of and all the ones that occur after it. The worst-case scenario occurs if
we want to change the first bucket. That means we have to change all of the if
statements in the entire function. For example, if we decide that the “33� guessers
occur 5% of the time instead of 4% (at the expense of 1% of the semi-logical
guessers), our new code would look like this:
GUESS_TYPE CGuesser::GetGuessType()
{
int index = DieRoller.SingleDie( 100, false );
// 1..5 = 5
if ( index <= 5 ) return GUESS_33;
Search WWH ::




Custom Search