Question

Use CPP This question is about providing game logic for the game of craps we developed...

Use CPP

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.

craps.cpp

/***
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;
}

Please provide the screenshot of final result too.

Thank you

Homework Answers

Answer #1

SOLUTION

#include <cstdlib>

#include <ctime>

#include <iostream>

#include <random>

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))); //randomizes rand() function.

    int rollDice;

    char repeat = 'y';

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

   while (repeat == 'y' || repeat == 'Y')

   {                                                   

    rollDice = rollDiceAdvanced();

    if (rollDice == 7 || rollDice == 11)

    {

        cout << ". Winner !" << endl ;

    }

    else if (rollDice == 2 || rollDice == 3 || rollDice == 12)

    {

        cout  << ". You lose!" << endl;

    }

    else if (rollDice == 4 || rollDice == 5 ||rollDice == 6 ||rollDice == 8 || rollDice == 9 || rollDice == 10)

    {     

        int sum2;

        while (1)

        {

            sum2 = rollDiceAdvanced();

               if( sum2 == rollDice )

            {

                cout << ". Winner !" << endl;

                break;

            }

        else if( sum2 == 7 )

            {

                cout << ". You Lose!" << endl;

                break;

            }

        }

     

    }

    cout <<"Another game? Y(es) or N(o)"  << endl;

    cin >> repeat;

    while (repeat == 'n' || repeat == 'N')

    {

      cout <<  "Thank you for playing!"<< endl;

      return 0;

    }

  }

  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;

}

RESULT

Know the answer?
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
Similar Questions
One of the most popular games of chance is a dice game known as “craps”, played...
One of the most popular games of chance is a dice game known as “craps”, played in casinos around the world. Here are the rules of the game: A player rolls two six-sided die, which means he can roll a 1, 2, 3, 4, 5 or 6 on either die. After the dice come to rest they are added together and their sum determines the outcome. If the sum is 7 or 11 on the first roll, the player wins....
JAVA: MUST BE DONE IN JAVA Assignment: Write algorithms and programs to play “non-betting” Craps. Craps...
JAVA: MUST BE DONE IN JAVA Assignment: Write algorithms and programs to play “non-betting” Craps. Craps is a game played with a pair of dice. In the game, the shooter (the player with the dice) rolls a pair of dice and the number of spots showing on the two upward faces are added up. If the opening roll (called the “coming out” roll) is a 7 (“natural”) or 11 (“yo-leven”), the shooter immediately wins the game. If the coming out...
Craps is a dice game in which the players make wagers on the outcome of the...
Craps is a dice game in which the players make wagers on the outcome of the roll, or a series of rolls, of a pair of dice. Most outcomes depend on the sum of the up faces of two, fair, six-sided dice. A) Describe the sample space for all possible outcomes of rolling two dice. How many ways are there to roll a 5? b)Determine all possible random variable values and the probability of those outcomes. Find the probability of...
Convert this C++ to JavaScript and run in browser. #include <cstdlib> #include <ctime> #include <sstream> #include...
Convert this C++ to JavaScript and run in browser. #include <cstdlib> #include <ctime> #include <sstream> #include <iostream> using namespace std; /* * */ class Dice{ private: static const int MAXDICE=6; static const int MINDICE=1; int faceVal; public: Dice(int); void setFace(int); int getFace(); string toString(); }; Dice::Dice(int faceVal) { if(faceVal<MINDICE&&faceVal>MAXDICE) { setFace(1); } else { this->faceVal=faceVal; } } void Dice::setFace(int faceVal) { this->faceVal=faceVal; } int Dice::getFace() { return faceVal; } string Dice::toString() { stringstream ss; ss<<faceVal; string str="face value is ";...
Modify the sum_thread.cpp program to compute sum = 1 + 1/2 + 1/3 + 1/4 +...
Modify the sum_thread.cpp program to compute sum = 1 + 1/2 + 1/3 + 1/4 + … 1/n. Let’s estimate natural using n = 20. sum_thread.cpp #include <chrono> #include <iostream> #include <mutex> #include <random> #include <utility> #include <vector> #include <thread> using namespace std; constexpr long long size= 1000000; mutex myMutex; void sumUp(unsigned long long& sum, const vector<int>& val, unsigned long long beg, unsigned long long end){ long long localSum = 0; for (auto it= beg; it < end; ++it){ localSum+=...
Modify the sum_thread.cpp program to compute sum = 1 + 1/2 + 1/3 + 1/4 +...
Modify the sum_thread.cpp program to compute sum = 1 + 1/2 + 1/3 + 1/4 + … 1/n. Let’s estimate natural using n = 20. sum_thread.cpp #include <chrono> #include <iostream> #include <mutex> #include <random> #include <utility> #include <vector> #include <thread> using namespace std; constexpr long long size= 1000000; mutex myMutex; void sumUp(unsigned long long& sum, const vector<int>& val, unsigned long long beg, unsigned long long end){ long long localSum = 0; for (auto it= beg; it < end; ++it){ localSum+=...
Modify the sum_thread.cpp program to compute harmonic sum = 1 + 1/2 + 1/3 + 1/4...
Modify the sum_thread.cpp program to compute harmonic sum = 1 + 1/2 + 1/3 + 1/4 + … 1/n. Let’s estimate natural using n = 20. sum_thread.cpp #include <chrono> #include <iostream> #include <mutex> #include <random> #include <utility> #include <vector> #include <thread> using namespace std; constexpr long long size= 1000000; mutex myMutex; void sumUp(unsigned long long& sum, const vector<int>& val, unsigned long long beg, unsigned long long end){ long long localSum = 0; for (auto it= beg; it < end; ++it){...
C++ question. Please explain the code and how it resulted in the output. Explain each line...
C++ question. Please explain the code and how it resulted in the output. Explain each line along with the aspects of "buffers" if possible. Everything is listed below. Code below: #include <iostream> #include <string> using namespace std; int main() {    cout << boolalpha;    cout << static_cast<bool>(1) << endl;    cout << static_cast<bool>(0) << endl;    string s = "AAARGH!!!";    if (s.find("AAA")) { cout << 1 << endl; }    if (s.find("RGH")) { cout << 2 << endl;...
In java create a dice game called sequences also known as straight shooter. Each player in...
In java create a dice game called sequences also known as straight shooter. Each player in turn rolls SIX dice and scores points for any sequence of CONSECUTIVE numbers thrown beginning with 1. In the event of two or more of the same number being rolled only one counts. However, a throw that contains three 1's cancels out player's score and they mst start from 0. A total of scores is kept and the first player to reach 100 points,...
The game requires $5 to play, once the player is admitted, he or she has the...
The game requires $5 to play, once the player is admitted, he or she has the opportunity to take a chance with luck and pick from the bag. If the player receives a M&M, the player loses. If the player wins a Reese’s Pieces candy, the player wins. If the player wins they may roll a dice for a second turn, if the die rolls on a even number, they may pick from the bag once again with no extra...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT