Question

Assignment Specifications We are going to use the Monte Carlo Method to determine the probability of...

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 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:

Roll1:2?currentturnscore=2

Roll2:5?currentturnscore=7

Roll3:6?currentturnscore=13

Roll4:2?currentturnscore=15

Roll5:4?currentturnscore=19

Roll6:3?currentturnscore=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:

Roll1:6?currentturnscore=6

Roll2:5?currentturnscore=11

Roll3:5?currentturnscore=16

Roll4:3?currentturnscore=19

Roll5:1?currentturnscore=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);

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 values hould 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 Assignment Specifications We are going to use the next line, print "Score" and "EstimatedProbability" 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. ? Print the table to a file named “output.txt”

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!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program Code:

#include<iostream>

#include<cstdlib>

using namespace std;

int main()

{

     int N,turns, count, turnSum=0,Value, intCount=0;

     double estimation=0.0;

     srand(333);

     cout << "At what value should we hold at?" << endl;

     cin >> turns;

     cout << "Enter the value of N to hold the turn simulations" << endl;

     cin >> N;

     count=0;

     do

     {

           intCount=0;

           turnSum=0;

           do

           {

           Value=rand()%6+1;         

           turnSum=turnSum+Value;

           intCount++;

           if(Value==1)

           {

                turnSum=0;

           }

           cout << "Roll " << intCount << ": " << Value<<" - current turn score = "<<turnSum << endl;

           }while(turnSum<N && Value!=1);

           count++;

           estimation=turnSum/turns;

           cout << "Estimated value: " << estimation << endl;

           cout << endl;

     }while(count<turns);

     system("pause");

     return 0;

}

Add a comment
Know the answer?
Add Answer to:
Assignment Specifications We are going to use the Monte Carlo Method to determine the probability of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The Rules of Pig Pig is a folk jeopardy dice game with simple rules: Two players...

    The Rules of Pig Pig is a folk jeopardy dice game with simple rules: Two players race to reach 100 points. Each turn, a player repeatedly rolls a die until either a 1 (”pig”) is rolled or the player holds and scores the sum of the rolls (i.e. the turn total). At any time during a player’s turn, the player is faced with two decisions: roll If the player rolls a 1: the player scores nothing and it becomes the...

  • // This program creates a simulated player who takes one turn in the // Pig dice...

    // This program creates a simulated player who takes one turn in the // Pig dice game. The simulated player keeps rolling the die until // the total for the turn is 20 or greater, or until a 1 is rolled. // If a 1 is rolled, the player's score for the turn is 0. Otherwise // the player's score is the sum of the rolls for the turn. // ///////////////////////////////////////////////////////////////////// #include<iostream> #include<cstdlib> #include<time.h> using namespace std; int randNumGen(int upper,...

  • The game of Pig is a simple two-player dice game in which the first player to...

    The game of Pig is a simple two-player dice game in which the first player to reach 100 or more points wins. Players take turns. On each turn, a player rolls a six-sided die: If the player rolls a 1, then the player gets no new points and it becomes the other player’s turn. If the player rolls 2 through 6, then he or she can either ROLL AGAIN or HOLD:      At this point, the sum of all rolls...

  • please write the following program in Java PIG is one of a family of games called...

    please write the following program in Java PIG is one of a family of games called jeopardy dice games, since a player's main decision after each roll is whether to jeopardize previous gains by trying for potentially even greater gains. In PIG, all players start with ZERO points, and each turn involves rolling a die to earn points. The first player to earn 100 points or more total shouts "PIG!" and wins the game. On their turn, a player does...

  • For your final project you will incorporate all your knowledge of Java that you learned in this class by programming the game of PIG. The game of Pig is a very simple jeopardy dice game in which two p...

    For your final project you will incorporate all your knowledge of Java that you learned in this class by programming the game of PIG. The game of Pig is a very simple jeopardy dice game in which two players race to reach 100 points. Each turn, a player repeatedly rolls a die until either a 1 is rolled or the player holds and scores the sum of the rolls (i.e. the turn total). At any time during a player's turn,...

  • Requirements: 1. You are not allowed to use global variables. 2. Must declare the following two...

    Requirements: 1. You are not allowed to use global variables. 2. Must declare the following two constants public static final int POINTS = 30; public static final int FORFEIT_POINTS = 20; 3. You can implement your own solution but you are required to break down the problem into smaller pieces. 4. You must provide the output for two rounds of game minimum. 5. Since we are using the Random number generator your output will not be exactly like mine. However,...

  • can someone help me with this C++ problem write your game() function and 3+ functions that...

    can someone help me with this C++ problem write your game() function and 3+ functions that simulate the game of Jeopardy Dice according to the following game mechanics and specifications. Game Mechanics There are two players: the user and the computer, who alternate turns until one of them reaches 80 points or higher. The user is always the first player and starts the game. If any player reaches 80 points or more at the end of their turn, the game...

  • Two player Dice game In C++ The game of ancient game of horse, not the basketball...

    Two player Dice game In C++ The game of ancient game of horse, not the basketball version, is a two player game in which the first player to reach a score of 100 wins. Players take alternating turns. On each player’s turn he/she rolls a six-sided dice. After each roll: a. If the player rolls a 3-6 then he/she can either Roll again or Hold. If the player holds then the player gets all of the points summed up during...

  • java thank you! Pig is a traditional dice jeopardy game. Players take turns, starting with a...

    java thank you! Pig is a traditional dice jeopardy game. Players take turns, starting with a randomly-chosen player. On their turn a player rolls a single 6-sided die 1 or more times. After each roll, the player has a choice: hold Stop rolling and add the total of al numbers rolled this turn to their score, or roll Roll the die again. If the player rolls a 1, their turn ends and they score 0 for the turn. Points scored...

  • Hello, Could you please help me code this program in Java? It is important that all...

    Hello, Could you please help me code this program in Java? It is important that all rules are carefully followed. Using the PairOfDice class from PP 4.9, design and implement a class to play a game called Pig. In this game, the user competes against the computer. On each turn, the current player rolls a pair of dice and accumulates points. The goal is to reach 100 points before your opponent does. If, on any turn, the player rolls a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT