This lab should help you with the concept of simple branching combined with fancy input while continuing to let you practice with random value generation.
Returning to the dice roll statistics program, you realize that many dice rolls in games require not just a simple roll and addition of the faces' dots. Some games adjust the score thusly achieved by adding or subtracting a set amount from the dot-total. Such rolls are designated by 'NdSaA': roll N dice of size S and adjust the total by A.
For instance, you might be told to roll 2d6+3. This would give values between 5 and 15 (inclusive). (The 2d6 gives 2 to 12 and adding 3 to each value in this range gives 5 to 15.) The new formulas for this adjusted dice roll are:
min = N+A max = N*S+A min+max avg = --------- 2
Hmm...the last one seems familiar...*grin*
Note, though, that the adjustment may be a positive value, a negative value, or even 0. When the adjustment is 0, the user may even leave it off of their input completely! Some sample rolls:
2d6 2d6-2 2d6+3 1d4+1 3d12-6 5d8+0
Notice in the above samples that the last one specifies a 0 adjustment but the first one simply left off the adjustment!
Calculate the three statistics for the user's adjusted dice roll and report them in a nice way. You should also give them a sample roll value (what might possibly) come up during an actual roll.
As an example, you might have the program interaction look something like (the parts displayed like this are typed by the user):
$ adjdicestat.out Welcome to the Dice Statistics Program!!! Enter your dice roll: 3d12+2 Thank you!! Calculating... Done. When rolling 3 size-12 dice adjusted by +2, your statistics will be: Minimum: 5 Average: 21.5 Maximum: 38 A typical dice roll might result in 13. Thank you for using the DSP!! Endeavor to have a tumbling day! $
Note how the average is a decimal value even though there is no possible way to roll a 21.5 on standard dice. (That's statistics for ya'!)
This assignment is (Level 3.5).
When dealing with a die roll that truly consists of a single die, many 'gamers' will simply say 'd4' instead of '1d4' (or 'd4-2' instead of '1d4-2').
Add (Level 1) to allow the user to leave off the N value if it is simply 1. (Hint: this is an optional leading part of their input...)
You realize that rolling dice (more than a single die) is not a uniform process but that our random number generating formula from lecture is supposed to be for uniform situations -- where everything has equal likelihood.
Add (Level 2) to make your sample roll more statistically accurate. Using the simple formula developed in the notes, we'll achieve a value in the proper range for the die roll, but it will be uniformly distributed (again, each value will be equally likely).
But that simply isn't the case! Look at something as simple as 2d4:
die 1 | die 2 | total --------+---------+--------- 1 | 1 | 2 1 | 2 | 3 1 | 3 | 4 1 | 4 | 5 2 | 1 | 3 2 | 2 | 4 2 | 3 | 5 2 | 4 | 6 3 | 1 | 4 3 | 2 | 5 3 | 3 | 6 3 | 4 | 7 4 | 1 | 5 4 | 2 | 6 4 | 3 | 7 4 | 4 | 8 dice total | 2 | 3 | 4 | 5 | 6 | 7 | 8 ----------------+-----+-----+-----+-----+-----+-----+----- ways to get it | 1 | 2 | 3 | 4 | 3 | 2 | 1
The first table shows all possible rolls and totals for the two 4-sided dice. The second table shows the possible totals and the number of ways each can be arrived at. Notice that a 5 total is 4 times as likely as either a 2 or an 8 total.
On the other hand, the roll of a single die has, of course, a minimum of 1 and a maximum of the number of sides on the die. And perhaps even more important to note, the roll of a single die *IS* uniformly distributed.
Rather than using some twisted statistics to model the behavior of multiple dice, it would be far easier to roll the dice individually (since the individuals are uniform and you can use our formula). Then you can just add up their respective values as you go. This means you'll have to repeat the rolling of a single die a number of times equal to the number of dice to be used in the die roll.
The longer your main program becomes, the more difficult it becomes to maintain and adjust. This (and other problems) is taken care of by simply using the idea of functions and top-down design principles! By modularizing the program, we not only reduce the size and complexity of the main, but we may also eliminate bugs, be able to re-use one function for two separate tasks, and even make the whole program more readable and manageable.
Add (Level 3) to use functions to break up your program into more manageable pieces instead of having lots of repetitious code in a long, long, long main program.
You realize that the random number generating formula is not tied to just die rolls and should ideally be placed into a library for easy re-use in other programs.
Add (Level 2.5) to place your random number generating formula into a function. And to place that function in a library. (If you only make the random formula into a function, you'll still get the credit for the above option to utilize functions to break up your program...at least part of it...)
If you did all above options, this lab could be worth as much as (Level 12).