Create a program to allow a user to play a modified “craps” games. The rules wil
ID: 3734875 • Letter: C
Question
Create a program to allow a user to play a modified “craps” games. The rules will be as
follows:
1)User starts with $50.00 as their total.
2) Ask the user for their bet (maximum bet is their current total).
3)Throw a pair of dice.
a)If the total value is 7 or 11, the user is an instant winner.
The user has the amount bet added back into their total. Ask the user if they wish to play again and if so
they start at step two above.
b) If the total value is 2, 3, or 12, the user is an instant loser. The user has the amount bet deducted from their total.
Ask the user if they wish to play again and if so they start at step two above.
c)If the total is anything else, remember this total as the “point” and roll again.
i)If the new total is equal to the “point” , the user wins and the process is the same as winning in (a) above
ii) If the new total is a 7, the user loses. And the process is the same as losing in (b) above.
iii) If the new total is anything else the user must roll again and again try to match the “point” of the first roll.
This is my main
#include <iostream>
using namespace std;
#include "Dice.h"
void main ()
{
int i;
int ThrownValues [11];
InitializeDice ();
// for (i = 0; i < 20; i++)
// cout << ThrowDie () << endl;
// memset (ThrownValues, 0, 6 * sizeof (int));
// for (i = 0; i < 10000; i++)
// ThrownValues [ThrowDie () - 1]++;
// for (i = 0; i < 6; i++)
// cout << (i + 1) << " was thrown " << ThrownValues [i] << " times " << endl;
// for (i = 0; i < 20; i++)
// cout << ThrowPair () << endl;
memset (ThrownValues, 0, 11 * sizeof (int));
for (i = 0; i < 10000; i++)
ThrownValues [ThrowPair () - 2]++;
for (i = 0; i < 11; i++)
cout << (i + 2) << " was thrown " << ThrownValues [i] << " times " << endl;
}
This is my header file
#include "Dice.h"
#include <stdlib.h> // need this for random numbers
#include <time.h>
void InitializeDice ()
{
srand (time (0)); // use current time to start my random number sequence
}
int ThrowDie ()
{
int Value;
Value = rand (); // returns a pseudo random number from 0 to 32767
// RAND_MAX is a constant of value 32767
Value = (Value % 6) + 1; // gives back a number from 1 thru 6
return Value;
}
int ThrowPair ()
{
return ThrowDie () + ThrowDie ();
}
I need to create this projects with this main and header
Explanation / Answer
Please rate my answer:
Here ya go, hope this helps!
#include <iostream>
#include <random>
#include <time.h>
#include <iomanip>
using namespace std;
//Returns the value of two rolled dice
int rollDice()
{
//Returns a value between 2 and 12
return (( rand() % 6) + 1 ) + (( rand() % 6) + 1 );
}
void playAgain(bool& answer)
{
char ch = 'a';
while(toupper(ch) != 'Y' && toupper(ch) != 'N')
{
cout << "Would you like to play again? (Y) or (N) >>";
cin >> ch;
}
if(toupper(ch) == 'Y') { answer = false; }
if(toupper(ch) == 'N') { answer = true; }
}
int main()
{
srand((unsigned int)time(NULL)); //Seed the random number generator
//The starting money and the bet
float money = 50.00;
float bet = 0.0;
bool donePlaying = false;
int roll1 = 0, roll2 = 0;
//Start the game loop
while(!donePlaying)
{
while(bet <= 0.0 || bet > money)
{
cout << "Please enter a bet between $0.01 and $" << fixed << setprecision(2) << money << " >>";
cin >> bet;
}
roll1 = rollDice();
cout << "You rolled a " << roll1 << "! ";
if(roll1 == 7 || roll1 == 11)
{
money += bet;
cout << "You win! Your new balance is: $" << fixed << setprecision(2) << money << endl;
}
else if(roll1 == 2 || roll1 == 3 || roll1 == 12)
{
money -= bet;
cout << "You lose! Your new balance is: $" << fixed << setprecision(2) << money << endl;
}
else
{
//Continue rolling until a 7 is hit, or we get roll1 again
while(roll2 != 7 && roll2 != roll1)
{
roll2 = rollDice();
cout << "Your next roll was a " << roll2 << "! ";
}
if(roll2 == roll1)
{
money += bet;
cout << "You win! Your new balance is: $" << fixed << setprecision(2) << money << endl;
}
else if(roll2 == 7)
{
money -= bet;
cout << "You lose! Your new balance is: $" << fixed << setprecision(2) << money << endl;
}
}
bet = 0.0;
//If thety still have money, they can play again
if(money > 0)
playAgain(donePlaying);
else
donePlaying = true;
}
cout << " Thanks for playing! Your final balance was : $" << fixed << setprecision(2) << money << endl;
//Pause the program
cin.sync();
cin.get();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.