For this assignment you will simulate a given number of hold-at-N turns of a game called Pig, and report the estimated probabilities of the possible scoring outcomes. You are NOT implementing the game of Pig, only a single turn of this game. The value of N will be acquired via user input, as will the number of repetitions.
What is Pig?
Pig is a folk dice game with simple rules: Two players race to reach 100 points. In each turn, a player repeatedly rolls a die until either the player holds and is credited with the sum of the rolls so far (i.e. the current turn score) or rolls a 1 (“pig”), in which case the turn score is 0.
So at every point during a turn, the player is faced with a choice between two moves:
roll (again) – a roll of the die occurs
2 – 6: the number is added to the current turn score; the turn continues
1: the player loses all points accumulated in the turn (i.e. scores a 0); turn ends
hold – The turn ends as the the hold option is invoked for one reason or another.
Hold-at-N Turn Strategy
A good strategy to help decide when to hold and when to roll is the “hold-at-N strategy”:
the player chooses a number, N, that will hopefully both maximize their turn score while minimizing their chances of losing that score by rolling a 1; as soon as their current turn score reaches (or passes) N, the player holds.
We are going to test this strategy for different values of N, which will be supplied by user input, by simulating a number of turns (which will also be supplied by user input). Obviously, the larger the number of simulations, the better the estimate of probabilities.
For instance, suppose the user asks the program to test the strategy for N = 20.
We throw the die for a turn (“simulate a turn”), and get the following rolls:
Roll 1: 2 – current turn score = 2
Roll 2: 5 – current turn score = 7
Roll 3: 6 – current turn score = 13
Roll 4: 2 – current turn score = 15
Roll 5: 4 – current turn score = 19
Roll 6: 3 – current turn score = 22
At this point we end the turn by holding, since we have a score of 22 (which is at least our N).
Of course, if we simulate the same turn again, we might get:
Roll 1: 6 – current turn score = 6
Roll 2: 5 – current turn score = 11
Roll 3: 5 – current turn score = 16
Roll 4: 3 – current turn score = 19
Roll 5: 1 – current turn score = 0
We rolled a 1 which according to the rules ends the turn and grants a turn score of 0.
Question: for N = 20, what range of scores are possible? How many variables will you need to hold the probability estimates for those scores? What if we choose another number for N?
Again, remember that you are only implementing one turn of this game and then simulating it many times to estimate the probability of each scoring outcome of this turn strategy.
Random Seed Requirement
You will of course, be using the C++ cstdlib library function rand(), which you will “prime” with a seed value using the function srand(int).
As you know, if you want to get different random values every time you run a program using the rand function, you should seed it with srand(time(0));
When you are testing your program, however, you will want to directly compare your program’s output with the sample runs below, so you will want to work with the exact same random values that are used in our solution. To do this you have to seed rand with 333: srand(333);
Your values may differ from the sample results, even using the seed 333, due to differing implementations of rand() on different operating systems.
Input Requirements
Enter a single positive integer indicating the number at which to hold.
Enter a single positive integer indicating the number of turns to be simulated.
Larger numbers will tend to yield better estimations but take longer to execute.
We test with both small and large numbers, so testing may take some time.
Output Requirements
Prompt the user with: “What value should we hold at? ”
Prompt for number of simulations: “Hold-at-N turn simulations? ”
Output a blank line between the input prompt and table output.
On the next line, print “Score” and “Estimated Probability” separated by a tab (‘\t’).
After the simulations, print a table line for each score outcome that occurred, in increasing order of score.
For each score outcome, print the score, a tab, and the fraction of turn simulations that yielded that score rounded to six digits after the decimal place.
Common error
Make sure you count the results from every simulation – it is easy to accidentally omit the first or last run from your calculation. This will be barely noticeable if you have 1,000,000 simulation runs – but it makes a big difference if you only have 3 or 4!
Example Runs (User input has been bolded and underlined for emphasis.)
Notice that the results all have six digits to the right of the decimal (what formatting “manipulators” did we use to achieve that?)
Also note that in all cases (as we would expect!) the total probability – i.e. the sum of the probabilities of all possible scores – will be 1.000000 (100%)
So, for example, the 1 run simulation example estimates a 100% probability for the score that was actually rolled in the run.
What value should we hold at 17 Hold-at-N urn simulations 10000000 Score Estimated Probability 0.570301. 0.114186 0.108193 18 0.083909 19 0.062619 20 0.040803 21 22 0.019988 What value should we hold at 21 Hold-at-N urn simulations 2 Score Estimated Probability 0.000000 21 0.500000 0.000000 22 23 0.500000 24 0.000000 25 0.000000 0.000000 26 What value should we hold at 21 Hold-at-N urn simulations? 4 Score Estimated Probability 0.250000 21 0.250000 22 0.250000 23 0.250000 24 0.000000 0.000000 25 0.000000 26 What value should we hold at 21 Hold-at-N urn simulations 1 Score Estimated Probability 0.000000 21 0.000000 22 0.000000 23 1.000000 24 0.000000 25 0.000000 0.000000 26 What value should we hold at 21 Hold-at-N urn simulations 3 Score Estimated Probability 0.333333 0.333333 21 22 0.000000 0.333333 23 24 0.000000 0.000000 25 0.000000 26