Question


// 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, int lower);   // function prototype

int main()
{
int roll; // The value of the die roll
int total_turn = 0; // Player's score for this turn

// Seed the random number generator
srand(static_cast<int> (time(NULL)));

  
/////////////////////////////////////////
// Start of your code

  
// Use a while loop to roll the die until the total turn number is
// 20 or greater, or until a 1 is rolled. if a 1 is rolled the
// player gets a 0 for the turn.
// Call function randNumGen() to generate a number within the range 1..6

   // to simulate the rolling of the die
  
  
  
  
  
  
//output the number of points the simulated player got for the turn.

  
// End of your code
/////////////////////////////////////////

system("pause");
return 0;

}//close main()

// function definition
int randNumGen(int upper, int lower)   // function heading
{                                       // function body
   return (rand() % (upper - lower + 1)) + lower;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
// 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, int lower);   // function prototype

int main() {
    int roll; // The value of the die roll
    int total_turn = 0; // Player's score for this turn

// Seed the random number generator
    srand(static_cast<int> (time(NULL)));


/////////////////////////////////////////
// Start of your code


// Use a while loop to roll the die until the total turn number is
// 20 or greater, or until a 1 is rolled. if a 1 is rolled the
// player gets a 0 for the turn.
// Call function randNumGen() to generate a number within the range 1..6
// to simulate the rolling of the die
    while (total_turn < 20 && (roll = randNumGen(1, 6)) != 1) {
        total_turn += roll;
    }
    if (roll == 1) {
        total_turn = 0;
    }
//output the number of points the simulated player got for the turn.
    cout << "Total number of points = " << total_turn << endl;
    
// End of your code
/////////////////////////////////////////

    system("pause");
    return 0;

}//close main()

// function definition
int randNumGen(int upper, int lower)   // function heading
{                                       // function body
    return (rand() % (upper - lower + 1)) + lower;
}

Add a comment
Know the answer?
Add Answer to:
// This program creates a simulated player who takes one turn in the // Pig dice...
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
  • 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...

  • 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...

  • 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...

  • 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...

  • 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,...

  • #2 New Turn Rules A frustrating aspect of programming on a project with other programmers is the ...

    #2 New Turn Rules A frustrating aspect of programming on a project with other programmers is the fact that everyone solves problems in different ways. Just when you think you know which way things are headed, someone throws a snag in your design. Objects needed: Die Attributes Current value of the die Actions roll the die DiceSet Attributes An array of six Die The number of dice available to roll. When this number reaches 0, it is reset to 6....

  • In Yahtzee, 5 standard dice are rolled up to three times on a given turn. A...

    In Yahtzee, 5 standard dice are rolled up to three times on a given turn. A player can choose to save anywhere from 0 - 5 of the dice between rolls 1 and 2 and between rolls 2 and 3, meaning that the player can set aside certain dice before rolling the remaining dice if they choose to do so. Suppose that a given player does not take advantage of saving die rolls and instead rolls all 5 dice each...

  • Write a c++ program that simulates a million of games in craps. I am having a...

    Write a c++ program that simulates a million of games in craps. I am having a trouble getting to loop a million of times. I am trying to use a const for a million. This is the code so far. #include <iostream> #include <cstdlib>// contains prototypes for functions srand and rand #include <ctime>// contains prototype for function time #include <iomanip> using namespace std; int rollDice(); // rolls dice, calculates and displays sum void printstats(); int totroll = 0, games, point,...

  • Please i need helpe with this JAVA code. Write a Java program simulates the dice game...

    Please i need helpe with this JAVA code. Write a Java program simulates the dice game called GAMECrap. For this project, assume that the game is being played between two players, and that the rules are as follows: Problem Statement One of the players goes first. That player announces the size of the bet, and rolls the dice. If the player rolls a 7 or 11, it is called a natural. The player who rolled the dice wins. 2, 3...

  • The Dice game of "Pig" can be played with the following rules. 1. Roll two six-sided dice. Add the face values...

    The Dice game of "Pig" can be played with the following rules. 1. Roll two six-sided dice. Add the face values together. 2. Choose whether to roll the dice again or pass the dice to your opponent. 3. If you pass, then you get to bank any points earned on your turn. Those points become permanent. If you roll again, then add your result to your previous score, but you run the risk of losing all points earned since your...

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