In: Computer Science
// USE GOTO STATEMENT TO ROLL AGAIN
// OR USE WHILE LOOP TO ASK WHETHER U WANT TO ROLL AGAIN OR NOT
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
#include <ctime>
using namespace std;
void cls(void);
void cls(void)
{
system("cls||clear");
return;
}
int main()
{
int BankBalance = 0;
char quit;
int wager = 0;
int inputWager = 0;
int sum = 0;
int diceRoll = 0;
int rollPoint = 0;
int point = 0;
int dice1 = 0;
int dice2 = 0;
char playerResponse;
srand(time(0));
cout << "Hey there buddy. Welcome to the
game of Craps!!! Glad you can make it :D\n\n";
cout << "Let's start with the boring
rules:\n\n ";
cout << "A player rolls two dice. each die
has six faces." << endl;
cout << "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 is calculated." << endl;
cout << "If the sum is 7 or 11 on the
first throw, the player wins. if the sum is 2, 3, or 12 on the
first throw (called 'craps'), the player loses (i.e. the 'house'
wins)." << endl;
cout << "If the sum is 4, 5, 6, 8, 9, or
10 on the first throw, then the sum becomes the player's 'point'."
<< endl;
cout << "To win, you must continue rolling
the dice until you 'make your point'. The player loses by rolling a
7 before making the point.\n\n";
cout << "Please enter your initial Bank
Balance: $";
cin >> BankBalance;
while (cin.fail())
{
cin.clear();
cin.ignore(256,
'\n');
cout << "Invalid
input detected, please enter a valid amount: $";
cin >>
BankBalance;
}
cout << "\nCurrent bank balance: $"
<< BankBalance << endl;
cout << "Total wager so far: $" <<
wager << endl;
cout << "Please enter your wager:
$";
cin >> inputWager;
while (inputWager < 0 || inputWager >
BankBalance)
{
cout << "Not a
valid wager!!" << endl;
cout << "Current
bank balance: $" << BankBalance << endl;
cout << "Total
wager so far: $" << wager << endl;
cout << "Please
enter your wager: $";
cin >>
inputWager;
}
wager = wager + inputWager;
//BankBalance = BankBalance - inputWager;
//sum = diceRoll;
start:
// i used goto statement to loop
again
if (sum == 7)
{
cout << "\nYou lose the game!!!" << endl;
BankBalance = BankBalance - inputWager;
cout << "\nBank Balance: $" << BankBalance <<
endl;
cout << "Roll again? (y/n)";
cin >> playerResponse;
if (playerResponse == 'n' || playerResponse == 'N')
{
cout << "PEACE!!" << endl;
return -1;
}
}
else if (sum ==
point)
{
cout << "\nYou won the game!!" << endl;
BankBalance = BankBalance + 2*wager;
cout << "\nBank Balance: $" << BankBalance <<
endl;
}
dice1 = rand() % 6 +
1;
dice2 = rand() % 6 +
1;
sum = dice1 +
dice2;
cout << "Sum: "
<< sum << " (Die 1: " << dice1 << " Die 2:
" << dice2 << ")" << endl;
cout <<
"\n\nCurrent bank balance: $" << BankBalance <<
endl;
cout << "Please
enter your initial wager: $";
cin >>
wager;
while (wager <= 0 ||
wager > BankBalance)
{
cout << "This is not a valid wager!!" << endl;
cout << "current bank balance: $" << BankBalance
<< endl;
cout << "Please enter your initial wager: $";
cin >> wager;
}
//BankBalance =
BankBalance - wager;
//sum = diceRoll;
if (sum == 7 || sum ==
11)
{
cout << "You won the game!" << endl;
BankBalance = BankBalance + 2*wager;
cout << "\nBank Balance: $" << BankBalance <<
endl;
cout << "\nDo you wish to continue? (y/n)";
cin >> playerResponse;
if (playerResponse == 'n' || playerResponse == 'N')
{
cout << "See ya!!" << endl;
return -1;
}
else
{
goto start;
}
}
else if (sum == 2 || sum
== 3 || sum == 12)
{
cout << "Craps!!!" << endl;
BankBalance = BankBalance - wager;
cout << "\nBank Balance: $" << BankBalance <<
endl;
cout << "\nCONTINUE? (y/n)";
cin >> playerResponse;
if (playerResponse == 'n' || playerResponse == 'N') //here i had
corrected
{
cout << "Peace!!" << endl;
return -1;
}
else
{
goto start;
}
}
else
{
cout << "\nYou need to roll to make your point (" <<
sum << ")..." << endl;
//system("pause");
//point = sum;
//rollPoint(point, wager);
goto start;
}
return 0;
}