Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design a C++ program that A player rolls two dice. Each die has 6 faces. These f

ID: 3767922 • Letter: D

Question

Design a C++ program that A player rolls two dice. Each die has 6 faces. These faces contain 1, 2, 3, 4, 5, and 6 spots. After the dice have come to rest, the sum of the spots on the two upward faces are calculated. If the sum is 7 or 11 on the first roll, the player wins. If the sum is 2, 3, or 12 on the first roll, the player loses. If the sum is 4, 5, 6, 8, 9 or 10 on the first roll, the sum becomes the player’s “point.” To win, you must continue rolling the dice until you “make your point.” The player loses by rolling a 7 before making the point. Try playing this game. Start off with $1000.00 and each time you win you get back twice your bet. If you lose, you lose the amount you bet. See if you can set this up so that the program asks you if you want to stop playing. At the end print out if you are a winner or a loser.

Explanation / Answer

C++ Code:

#include <iostream>
#include <iomanip>
#include <cstdlib> // needed for functions srand() and rand()
#include <ctime>   // needed for function time()

using namespace std;

int rollDice(int diceVals[], int numberToRoll = 2);

void gameCraps(int sum, int bankBalance, int wager);

enum Status {CONTINUE, WON, LOST};

int main()
{
   int sum;
   int diceVals[2];
   int bankBalance = 1000;
   int wager;

   srand(time(0));

   sum = rollDice(diceVals);

   cout << "Your bank balance is: " << "€" << bankBalance << " ";
   cout << "Please enter a wager - ";

   cin >> wager;
   if (wager > 1000)
   {
       cout << "Not valid" << endl;
       cout << "Please enter a wager - " << endl;
       cin >> wager;
   }
   cout << " " << "Your wager: " << "€" << wager << " ";
   cout << "Player rolled: " << diceVals[0] << " + " << diceVals[1] << endl;

   gameCraps(sum, bankBalance, wager);

   return 0;
}

int rollDice(int diceVals[], int numberToRoll)
{
   int dicevalues = 0;

   for(int i = 0; i < numberToRoll; i++)
   {
       diceVals[i] = 1 + rand()%6;
   }
   for(int i = 0; i < numberToRoll; i++)
   {
       dicevalues = dicevalues + diceVals[i];
   }
   return dicevalues;
}

void gameCraps(int sum, int bankBalance, int wager)
{
   int myPoint;
   int diceVals[2];
   char userchoice;
   Status gameStatus;

   switch(sum)
   {
       case 7:
           gameStatus = WON;
       case 11:
           gameStatus = WON;
           break;
       case 2:
           gameStatus = LOST;
       case 3:
           gameStatus = LOST;
       case 12:
           gameStatus = LOST;
           break;
       default:
           gameStatus = CONTINUE;
           myPoint = sum;
           cout << "Point is: " << myPoint << endl;
           break;
   }

   while (gameStatus == CONTINUE)
   {
       sum = rollDice(diceVals);
       cout << "Player rolled: " << diceVals[0] << " + " << diceVals[1] << endl;

       if (sum == myPoint)
       {
           gameStatus = WON;
       }
       else if (sum == 7)
           {
               gameStatus = LOST;
           }

       if (gameStatus == WON)
       {
           cout << " ***Player wins*** " << endl;
           bankBalance = bankBalance + wager;
           cout << "Your bank balance is now: " << "€" << bankBalance << " ";
           cout << "Would you like to cash-in or continue? (Select 'Y', then return to continue... "
                       "or 'N' and return to cash-in!) ";
           cin >> userchoice;

           if (userchoice == 'y')
           {
               cout << "You're up big. Now's the time to cash in your chips! ";
               gameStatus = CONTINUE;
           }
           while (gameStatus == CONTINUE)
           {
               sum = rollDice(diceVals);

               cout << "Your bank balance is: " << "€" << bankBalance << " ";
               cout << "Please enter a wager - ";

               cin >> wager;

               cout << " " << "Your wager: " << "€" << wager << " ";
               cout << "Player rolled: " << diceVals[0] << " + " << diceVals[1] << endl;

               gameCraps(sum, bankBalance, wager);
           }
           if (userchoice == 'n')
           {
               cout << "Ah c'mon, take a chance! You go away with a bank balance of " << "€" << bankBalance << " ";
               exit(0);
           }
           if (userchoice !='n' || userchoice !='y')
           {
               cout << "Please enter a valid choice ('Y' or 'N') ";
               exit(-1);
           }
       }
       else
       {
           cout << " ***Player loses*** " << endl;
           bankBalance = bankBalance - wager;
           if (bankBalance <= 0)
           {
               cout << "Sorry, you don't have enough cash to continue! Please try again. ";
               cout << "Your bank balance is now " << "€" << bankBalance << " ";
               exit(-1);
           }
           if (bankBalance > 0 )
           {
               cout << "Would you like to cash-in or continue? (Select 'Y', then return to continue... "
                           "or 'N' and return to cash-in!) ";
               cin >> userchoice;
               if (userchoice == 'y')
               {
                   cout << "Oh, you're going for broke now?";
                   gameStatus = CONTINUE;
               }
               while (gameStatus == CONTINUE)
               {
                   sum = rollDice(diceVals);

                   cout << "Your bank balance is: " << "€" << bankBalance << " ";
                   cout << "Please enter a wager - ";

                   cin >> wager;

                   cout << " " << "Your wager: " << "€" << wager << " ";
                   cout << "Player rolled: " << diceVals[0] << " + " << diceVals[1] << endl;

                   gameCraps(sum, bankBalance, wager);
               }
               if (userchoice == 'n')
               {
                   cout << "Thanks for playing! You go away with a bank balance of " << "€" << bankBalance;
                   exit(0);
               }
               if (userchoice !='y' || userchoice !='n')
               {
                   cout << "Please enter a valid choice to continue ";
                   exit(-1);
               }
           }
       }
   }
}