Question

In: Computer Science

2) This question is about providing game logic for the game of craps we developed its...

2) This question is about providing game logic for the game of craps we developed its shell in class. THe cpp of the class is attached to this project. At the end of the game,
you should ask user if wants to play another game, if so, make it happen. Otherwise quit.

Here is the attached cpp file for que2:-

/***
This is an implementation of the famous 'Game of Chance' called 'craps'.
It is a dice game. A player rolls 2 dice. Each die has sixe faces: 1, 2, 3, 4, 5, 6.
Based on the values on the dice we play the game until a player wins or loose.
You add the values of the dice's face.
1) If the sum is 7 or 11 on the first roll, the player wins.
2) If the sum is 2, 3, or 12 on the first roll, the player loses ( or the 'house' wins)
3) If the sum is 4, 5, 6, 8, 9, or 10 on the first roll, then that sum becomes the player's 'point'.
4)To win, player must continue rolling the dice until he/she 'make the point'.
5)If the player rolls a 7 he/she loses.
***/

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <random>

enum class Status { CONTINUE, WON, LOST};

unsigned int rollDice(); // forward declaration of rollDice() function. It rolls 2 dice, displays the sum and return it.
unsigned int rollDiceAdvanced(); // same as above, but used advance c++ Random Number generation.

   // global variable to use in the advanced version of RNG (Random number Generator)
std::default_random_engine engine{ static_cast<unsigned int>(time(0)) };
std::uniform_int_distribution<unsigned int> randomInt{ 1, 6 };

int main()
{
   Status gameStatus; // can be CONTINUE, WON, or LOST
   unsigned int myPoint{ 0 };
   // either use the base rand() seeding
   srand(static_cast<unsigned int>(time(0))); //dandomizes rand() function.

   // but here a sample of using the random number generation:   // just testing:
   for (int i = 0; i < 20; ++i)
   {
       rollDice();
       rollDiceAdvanced();
   }

   unsigned int sumOfDice = rollDiceAdvanced(); // first roll of dice.

   // bellow the game logic: Student going to implement this.

   return 0;
}

// here is the simple version of rollDice(); throws 2 dice, each having faces 1 to 6, and return the sum.
unsigned int rollDice()
{
   unsigned int firstRoll{ 1 + static_cast<unsigned int>(rand()) % 6 }; // could be 0, 1, 2, 3, 4, 5, because 8 % 6 == 2, 7 %6 1, 9%6 == 3, 17%6 == 5, 18%6 ==0
   unsigned int secondRoll{ 1 + static_cast<unsigned int>(rand()) % 6 };
   unsigned int sum{ firstRoll + secondRoll };
   std::cout << "rollDice: " << sum << std::endl;
   return sum;
}


// this is distribution based random number generation in c++
unsigned int rollDiceAdvanced()
{
   unsigned int firstRoll{ randomInt(engine) };
   unsigned int secondRoll{ randomInt(engine) };
   unsigned int sum{ firstRoll + secondRoll };
   std::cout << "rollDiceAdvance: " << sum << std::endl;
   return sum;
}

Thanks for help

Solutions

Expert Solution

Cpp code (code change highlighted in bold):

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <random>
#include <cstring>
using namespace std;

enum class Status { CONTINUE, WON, LOST};

unsigned int rollDice(); // forward declaration of rollDice() function. It rolls 2 dice, displays the sum and return it.
unsigned int rollDiceAdvanced(); // same as above, but used advance c++ Random Number generation.

// global variable to use in the advanced version of RNG (Random number Generator)
std::default_random_engine engine{ static_cast<unsigned int>(time(0)) };
std::uniform_int_distribution<unsigned int> randomInt{ 1, 6 };

int main()
{
Status gameStatus; // can be CONTINUE, WON, or LOST
unsigned int myPoint{ 0 };
// either use the base rand() seeding
srand(static_cast<unsigned int>(time(0))); //dandomizes rand() function.

// but here a sample of using the random number generation: // just testing:
// for (int i = 0; i < 20; ++i)
// {
// rollDice();
// rollDiceAdvanced();
// }

unsigned int sumOfDice = rollDiceAdvanced(); // first roll of dice.

// bellow the game logic: Student going to implement this.
// continue playing the game until player says yes to replay
// each execution of of code inside while loop simulates one gameplay

while(true){
unsigned int point;
// case 1
if((sumOfDice==7) or (sumOfDice==11)){
cout<<"You win!"<<endl;
gameStatus=Status::WON;
}
// case 2
else if((sumOfDice==2) or (sumOfDice==3) or (sumOfDice==3)){
cout<<"You lose!"<<endl;
gameStatus=Status::LOST;
}
// case 3
else if((sumOfDice>=4) and (sumOfDice<=10) and (sumOfDice!=7)){
// assign point to sumOfDice
point=sumOfDice;
cout<<"Your point is : "<<point<<endl;
gameStatus=Status::CONTINUE;
}

// if case 3 happened on first roll, then continue rolling
while(gameStatus==Status::CONTINUE){
// roll again
sumOfDice = rollDiceAdvanced();
// case 4
if(sumOfDice==point){
cout<<"You made the point!"<<endl;
gameStatus=Status::WON;
}
// case 5
else if(sumOfDice==7){
cout<<"You lose!"<<endl;
gameStatus=Status::LOST;
}
// else contimue rolling
}

// ask player if he/she want to replay
char replay[10];
cout<<"Do you want to replay? (type 'yes' or 'no')"<<endl;
// take player input
cin>>replay;
// check if he/she prompts yes
// if yes then continue playing the game
if(strcmp(replay, "yes") == 0){
// this is first roll of next game
sumOfDice = rollDiceAdvanced();
}
// else exit from while loop
else break;
}

return 0;
}

// here is the simple version of rollDice(); throws 2 dice, each having faces 1 to 6, and return the sum.
unsigned int rollDice()
{
unsigned int firstRoll{ 1 + static_cast<unsigned int>(rand()) % 6 }; // could be 0, 1, 2, 3, 4, 5, because 8 % 6 == 2, 7 %6 1, 9%6 == 3, 17%6 == 5, 18%6 ==0
unsigned int secondRoll{ 1 + static_cast<unsigned int>(rand()) % 6 };
unsigned int sum{ firstRoll + secondRoll };
std::cout << "rollDice: " << sum << std::endl;
return sum;
}


// this is distribution based random number generation in c++
unsigned int rollDiceAdvanced()
{
unsigned int firstRoll{ randomInt(engine) };
unsigned int secondRoll{ randomInt(engine) };
unsigned int sum{ firstRoll + secondRoll };
std::cout << "rollDiceAdvance: " << sum << std::endl;
return sum;
}

Sample execution:


Related Solutions

Question 2 Baden Company is a diversified company which has developed the following information about its...
Question 2 Baden Company is a diversified company which has developed the following information about its five segments:                                       SEGMENTS                                               A                         B                   C                     D                     E         Total sales                      $   800,000      $1,700,000      $   300,000      $   320,000      $   580,000 Operating profit (loss)        (270,000)          480,000             40,000          (300,000)           (10,000) Identifiable assets            2,600,000        5,800,000        1,200,000        3,900,000        5,600,000 Instructions Identify which segments are significant enough to warrant disclosure in accordance with FASB No. 131, "Reporting Disaggregated...
Question 4: Jar Game Consider the following game: Players: 2 - We designate player #1 to...
Question 4: Jar Game Consider the following game: Players: 2 - We designate player #1 to be the one that starts with the jar. Actions: - Each round at the same time both players deposit between 1 to 4 pennies into the jar. - Then both players are able to count the pennies in the jar. - If there are 21 or more pennies, the person with the jar is the winner. - If there are 20 or less pennies,...
can someone answer question 2 please??? 1. In class, we developed a PPF for an economy...
can someone answer question 2 please??? 1. In class, we developed a PPF for an economy that produced two goods with no factor substitution. This PPF gives some intuition for why the PPF in the Heckscher-Ohlin Model is curved. a. Imagine an economy makes only clothes (QC) and food (QF) and has two inputs of production: Labor (L) and Capital (K). It takes 4 units of capital and 1 unit of labor to make one unit of clothing. It takes...
Which of the following is FALSE about the balanced scorecard and its underlying logic? A. The...
Which of the following is FALSE about the balanced scorecard and its underlying logic? A. The balanced scorecard enables top management to translate its strategy into performance measures that employees can understand and influence. B. The balanced scorecard consists of an integrated set of performance measures that are derived from and support a company’s strategy. C. A company’s ability to change and improve determines its ability to have efficient internal business processes. D. A company’s financial results determine the level...
This week, we are learning about categorical logic. Provide an example of each type of categorical...
This week, we are learning about categorical logic. Provide an example of each type of categorical proposition. Why is it important to understand categorical logic? Provide some examples of how you could apply these lessons to your personal and professional life.
Please write 2-23 paragraphs providing a scientific and thoughtful opinion about this Behavioral Finance question. One...
Please write 2-23 paragraphs providing a scientific and thoughtful opinion about this Behavioral Finance question. One could argue that neither the classical finance assumption of rationality of investors is correct nor are the markets entirely irrational. Discuss the merits and demerits of these alternative philosophies about asset pricing.
This question is for a project about providing an executive mangement plan of a major publc...
This question is for a project about providing an executive mangement plan of a major publc company. The company we picked is General Motors. Should General Motors be in Canada? If yes, why? What can they do to continue growth and success? What problems and opportunities can be identified in the executive management of General Motors in Canada?
Logic/ Game theoryLet f(n) count the different perfect covers of a 2-by-n chessboard by dominoes. Evaluate...
Logic/ Game theoryLet f(n) count the different perfect covers of a 2-by-n chessboard by dominoes. Evaluate f(1),f(2),f(3),f(4), and f(5). Try and find (and verify) a simple relation that the counting function f satisfies. Compute f(12) using the relation. Here is a solution it is titled exercise 4a.) in the packet on page 3: http://jade-cheng.com/uh/coursework/math-475/homework-01.pdf   Not sure on how to follow the logic.
Question 2: In 400 words discuss about this issue, It has been said that if we...
Question 2: In 400 words discuss about this issue, It has been said that if we define death as ceasing to be alive, we must first define what it means to be alive. Discuss what you learned this week from the Jahi McMath case about the ethical challenges of determining death? Choose one of the definitions of death and make an argument to support that definition using ethical principles and concepts. Discuss how the use of this definition might impact...
Question 2 A text-based adventure game has several types of player. The code for the game...
Question 2 A text-based adventure game has several types of player. The code for the game uses the following class, Character to define all the shared elements for different types of player characters (Warrior, Wizard etc). 1. public class Character { 2. private String name; 3. private int life; 4. protected int hitPoints; 5. public Character(String name, int life, int hitPoints) { 6. this.name = name; 7. this.life = life; 8. this.hitPoints = hitPoints; 9. } 10. public void setHitPoints(int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT