Question
The assignment is to recreate one turn of a game called pig- Two players race to reach 100 points. In each turn, a player repeatedly
The assignment is to recreate one turn of a game called pig- 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.
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).
When you are testing your program, you will want to directly compare your programs 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);
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.)
What value should we hold at? 17 Hold-at-N turn simulations? 10000000 Score Estimated Probability 0 0.570301 17 0.114186 18 0.108193 19 0.083909 20 0.062619 21 0.040803 22 0.019988 | What value should we hold at? 21 Hold-at-N turn simulations? 1 Score Estimated Probability 0 0.000000 21 0.000000 22 0.000000 23 1.000000 24 0.000000 25 0.000000 26 0.000000 |
What value should we hold at? 21 Hold-at-N turn simulations? 2 Score Estimated Probability 0 0.000000 21 0.500000 22 0.000000 23 0.500000 24 0.000000 25 0.000000 26 0.000000 | What value should we hold at? 21 Hold-at-N turn simulations? 3 Score Estimated Probability 0 0.333333 21 0.333333 22 0.000000 23 0.333333 24 0.000000 25 0.000000 26 0.000000 |
What value should we hold at? 21 Hold-at-N turn simulations? 4 Score Estimated Probability 0 0.250000 21 0.250000 22 0.250000 23 0.250000 24 0.000000 25 0.000000 26 0.000000 |
Notice that the results all have six digits to the right of the decimal
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.
I have started the assignment but am having difficulties getting it to do exactly as it is supposed to. This is what I have got:
#include
using namespace std;
int main() { //initialize random number generator srand(333); //srand (time(0));
//variables int hold_value ; // what value should we hold at double simulation_number ; // how many simulations int score_counter ; // value counter int score ; // score from rolling dice int dice_roll ; // random number generated double score0 ; // counter for 0 scores double score_hold_value ; // counter for hold_value scores double score_hold_value_1 ; // counter for hold_value + 1 scores double score_hold_value_2 ; // counter for hold_value + 2 scores double score_hold_value_3 ; // counter for hold_value + 3 scores double score_hold_value_4 ; // counter for hold_value + 4 scores double score_hold_value_5 ; // counter for hold_value + 5 scores
//initializing variables //score_counter = 0; score = 0; score0 = 0; score_hold_value = 0; score_hold_value_1 = 0; score_hold_value_2 = 0; score_hold_value_3 = 0; score_hold_value_4 = 0; score_hold_value_5 = 0;
cout << "What value should we hold at? "; cin >> hold_value; cout << endl; cout << "Hold-at-N turn simulations? "; cin >> simulation_number;
for (score_counter = 1; score_counter <= simulation_number; score_counter++) { dice_roll = (rand() % 6) + 1 ; //cout << "dice roll " << dice_roll << " score "<< score << " score counter " << score_counter << endl;
if (dice_roll == 1) { score = 0; // **cout << "dice roll " << dice_roll; // **cout << "score " << score << "score counter " << score_counter << endl; cout << score << endl; //*** break; score = 0; score0+=1; cout << "hold value: " < else if (dice_roll >= 2 && dice_roll <= 6) { score = 0; // *************** score+= dice_roll; // **cout << "dice roll " << dice_roll << " score " << score << " score counter " << score_counter<< endl; cout << score << endl; //*** while (!(score >= hold_value && score <= hold_value + 5)) { dice_roll = 1 + (rand() % 6) ; if (dice_roll == 1) { score = 0; // ** cout << "dice roll " << dice_roll; // **cout << " score " << score; // **cout << " score counter " << score_counter < else if (dice_roll >= 2 && dice_roll <= 6) { score+= dice_roll; // **cout << "dice roll " << dice_roll << " score " << score << " score counter " << score_counter<< endl; cout << score << endl; //*** if (score == hold_value) { //cout << "dice roll " << dice_roll; //cout << " score " << score << " score counter " << score_counter<< endl; //score = 0; //score_counter++; break; } else if (score > hold_value && score <= hold_value +5) { //cout << "dice roll " << dice_roll; //cout << " score " << score << " score counter " << score_counter<< endl; //score = 0; //score_counter++; break; } if (score == 0) { //score0 = 0; score0+=1; //score0++; cout << "hold value: "<< score0 << endl; } else if (score == hold_value) { //score_hold_value = 0; score_hold_value+=1; //score_hold_value++; cout << "hold value: "<< score_hold_value << endl; } else if (score == hold_value + 1) { //score_hold_value_1 = 0; score_hold_value_1+= 1; //score_hold_value_1++; cout << "hold value 1: " << score_hold_value_1 << endl; } else if (score == hold_value + 2) { //score_hold_value_2 = 0; score_hold_value_2+=1; //score_hold_value_2++; cout << "hold value 2: " << score_hold_value_2 << endl; } else if (score == hold_value + 3) { //score_hold_value_3 = 0; score_hold_value_3+=1; //score_hold_value_3++; cout << "hold value 3: " << score_hold_value_3 << endl; } else if (score == hold_value + 4) { //score_hold_value_4 = 0; score_hold_value_4+=1; //score_hold_value_4++; cout << "hold value 4: "<< score_hold_value_4 << endl; } else if (score == hold_value + 5) { //score_hold_value_5 = 0; score_hold_value_5+=1; //score_hold_value_5++; cout << "hold value 5: " << score_hold_value_5 << endl; } } } } } cout << "Score" << "\t" << "Estimated Probability" < return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started