Game Development Reference
In-Depth Information
unsigned short CDie::SingleDie( unsigned short NumSides,
bool ZeroBased /*= true */ )
{
unsigned short ThisRoll = rand() % NumSides;
// if the die roll is not 0-based,
// add one to it.
if ( !ZeroBased ) {
ThisRoll += 1;
} // end if
return ThisRoll;
}
By calling the function with the desired number of sides as the parameter, we
will get a random number simulating the roll of the single die.
unsigned short RollResult = SingleDie( 11 );
Note that the parameter ZeroBased allows us to determine what the starting
number of the die will be. If ZeroBased is true (the default), then the die roll will
start with 0. For example, a d6 roll would generate the numbers 0 to 5. If ZeroBased
is false, then a d6 roll will generate the numbers 1 to 6 like the six-sided die we are
familiar with.
If we wanted to simulate the 3d11 roll from our example above, we could call
the function SingleDie() three times, passing in the number 11 each time, and
add the results together. On the other hand, since we may be finding ourselves
doing plenty of multiple-dice calls, we can create a function that condenses all of
this into one package.
unsigned short CDie::MultipleDie( unsigned short NumDie,
unsigned short NumSides,
bool ZeroBased /*= true */ )
{
unsigned short TotalRoll = 0;
Search WWH ::




Custom Search