Assignment Specifications We are going to use the Monte Carlo Method to determin
ID: 3756057 • Letter: A
Question
Assignment Specifications We are going to use the Monte Carlo Method to determine the probability of scoring outcomes of a single turn in a game called Pig. Your Task 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 o. I 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 O); turn ends hold- The turn ends as the the hold option is invoked for one reason or another. You can play the game yourself a few times before you start to think about the assignment. It can be useful to visualize and understand how a turn works. Play the game here.Explanation / Answer
C++ Code with Explanation
#include <iostream>
#include <time.h>
#include <iomanip>
using namespace std;
//function prototypes
int genRandInt(int min, int max);
int singleTurn(int holdValue);
//main function
int main()
{
//seed the random
srand(time(0));
//declare the required variables
int toHold=0, num_turns=0;
int count = 0;
double probability = 0;
//prompt the user to enter the value to hold the value at
cout<<"What value should we hold at? ";
cin>>toHold;
//prompt the user to enter the value of number of turns
cout<<"Hold-at-N turn simulations? ";
cin>>num_turns;
cout<<endl;
//fix the decimal point to 6 digits
cout<<setprecision(6)<<fixed;
//print the header
cout<<"Score Estimeated Probability"<<endl;
//loop for 7 times
for(int i=0; i<7; i++)
{
//initialise the count = 0 for all the 7 times
count = 0;
//if it is first iteration then do the following
if(i==0)
{
//if first iteration then loop till number
//of turns entered by the user.
for(int j=1;j<=num_turns;j++)
{
//call the singleTurn function by passing
//the toHold value
//if the function returns 0 then
//increment the counter
if(singleTurn(toHold)==0)
{
count++;
}
}
//after the turns completion, find the probability
probability = (double)count/num_turns;
//print the score and probability
cout<<"0 "<<setw(20)<< probability<<endl;
}
//if it is not the first iteration do the following
else
{
//loop till the number of turns reached starting from
//1.
for(int j=1;j<=num_turns;j++)
{
//call the singleTurn function by passing
//the toHold value
//if the function returns greater than equal to
// toHold value then increment the counter
if(singleTurn(toHold)>=toHold)
{
count++;
}
}
//after the turns completion, find the probability
probability = (double)count/num_turns;
//print the score and probability
cout<<toHold<<setw(20)<< probability<<endl;
toHold++;
}
}
cout<<endl;
system("pause");
return 0;
}
//genRandInt() function that takes two integers parameters
//and returns a random number
int genRandInt(int min, int max)
{
return rand()%(max - min + 1) + min;
}
//singleTurn() function that takes one integer parameter
//and returns the sum value.
int singleTurn(int holdValue)
{
//declare the required variables
int i = 0;
int sum = 0, rand = 0;
//loop till the sum value is less than or equal to holdValue
while(sum <= holdValue)
{
//call the genRandInt() function, and store the value
//in rand variable
rand = genRandInt(1, 6);
//if the obtained rand value is 1, score becomes zero
//returns the 0 value
if(rand == 1)
{
sum = 0;
break;
}
//if rand is not equal to 1, and then add the rand value to
//the previous value score.
else
{
sum +=rand;
}
}
//return sum value
return sum;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.