In: Computer Science
Hello, this is for C#.
Initialize variable balance to 1000 dollars. Prompt the player to enter a wager. Check that wager is less than or equal to balance, and if it’s not, have the user reenter wager until a valid wager is entered. After a correct wager is entered, run one game of craps. If the player wins, increase balance by wager and display the new balance. If the player loses, decrease balance by wager, display the new balance, check whether balance has become zero and, if so, display the message “Sorry. You busted!”
#include <iostream>
#include <cstdlib>
srand and rand
#include <ctime>
using std::cout;
using std::endl;
using std::cin;
enum Status { CONTINUE, WON, LOST };
int rollDice( void ); // function prototype
void playGame ( Status & gameStatus );
void displayWonOrLost ( Status, int &, int & );
int main()
{
int BankBalance = 1000;
int Wager;
char Response;
Status gameStatus;
cout << "Please enter a wager (You start with 1000 USD!)" << endl;
cin >> Wager;
while ( Wager <= BankBalance )
{
cout << "Continue with current wager (Y), enter a new wager (E) or
end game (N)?" << endl;
cin >> Response;
switch ( Response ) {
case 'Y':
case 'y':
playGame( gameStatus );
displayWonOrLost ( gameStatus, BankBalance, Wager );
break;
case 'N':
case 'n':
cout << "Your final balance is " << BankBalance << endl;
cout << "Thank you, come again!" << endl;
return 0;
break;
case 'E':
case 'e':
cout << "Please enter the new wager!" << endl;
cin >> Wager;
break;
default:
cout << "Incorrect Response. Please try again!" << endl;
break;
}
}
/*if ( Wager > BankBalance )
{
cout << "Re-enter wager!" << endl;
cin >> Wager;
}
if ( BankBalance == 0 )
cout << "Sorry, you busted!" << endl;
return 0;
}
void displayWonOrLost ( Status gameStatus, int & passedBalance, int &
passedWager )
{
if ( gameStatus == WON ) {
passedBalance += passedWager;
cout << "Player wins, new balance is " << passedBalance << endl;
cout << "You are awesome! Keep Going!" << endl;
}
else {
passedBalance -= passedWager;
cout << "Player loses, new balance is " << passedBalance << endl;
cout << "You suck! Get out while you can!" << endl;
}
}
void playGame (Status & gameStatus)
{
int sum;
int myPoint;
srand( time( 0 ) );
sum = rollDice();
switch ( sum )
{
case 7:
case 11:
gameStatus = WON;
break;
case 2:
case 3:
case 12:
gameStatus = LOST;
break;
default:
gameStatus = CONTINUE;
myPoint = sum;
cout << "Point is " << myPoint << endl;
break;
}
while ( gameStatus == CONTINUE )
{
sum = rollDice();
if ( sum == myPoint )
gameStatus = WON;
else
if ( sum == 7 )
gameStatus = LOST;
}
}
int rollDice( void )
{
int die1;
int die2;
int workSum;
die1 = 1 + rand() % 6;
die2 = 1 + rand() % 6;
workSum = die1 + die2;
cout << "Player rolled " << die1 << " + " << die2
<< " = " << workSum << endl;
return workSum;
}