1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
void CrapsGame(); //1//
// Will declare all variables and excute the game until donePlaying is true.
void StartGame(int&, int&); //2//
// This function will control the input and ask the player to start -
// the game and will get the bank balance and wager from the player.
int CheckWager(int, int&); //3//
// This function will take the wager and the bankBalance and make sure they are valid.
void GenerateRandomChatter(); //4//
// Generates a random number and then outputs the phrase corresponding to that number.
int GenerateRandomRoll(); //5//
// Generates two random numbers for rolling dice and then adds them to get the point.
char CheckDice(int, char&); //6//
// Checks the point to see if the player won, lost, or has to keep rolling.
bool CheckBalance(int, bool&); //7//
// Checks to see if the new bank balance is 0 and outputs message if it is.
char CheckRoll(int, int, char&); //8//
// Checks the players new roll to see if they won, lost, or has to keep rolling.
int NewBalance(int, int, char&); //9//
// This function will take the original bankBalance and add or
// subtract the wager - chosen based on whether or not the player won.
void AskToPlayAgain(bool); //10//
// Asks the player to enter y or n to keep playing or to quit playing.
int main ()
{
CrapsGame(); // Function called and will only fall out when donePlaying is true.
return 0;
}
void CrapsGame() //1//
{
int bankBalance = 0;
int wager = 0;
int point = 0;
string chatter;
bool donePlaying = false;
char win = ' ';
int newRoll;
int randomInt;
srand(time(NULL));
cout << "Welcome to the Craps Simulation" << endl;
while(!donePlaying) // Will repeat game until donePlaying is true.
{
StartGame(bankBalance, wager);
// This function will control the input and ask the player to start -
// the game and will get the bank balance and wager from the player.
CheckWager(bankBalance, wager);
// This function will take the wager and the bankBalance and make sure they are valid.
GenerateRandomChatter();
// Generates a random number and then outputs the phrase corresponding to that number.
point = GenerateRandomRoll();
// Generates two random numbers for rolling dice and then adds them to get the point.
cout << point << endl;
win = CheckDice(point, win);
// Checks the point to see if the player won, lost, or has to keep rolling.
int rollCount = 0;
// Declares rollCount as an int and starts at 0.
while(win != 'Y' || win != 'N') // Repeats steps until the user won or lost.
{
++rollCount;
cout << "Roll " << rollCount << " -> ";
newRoll = GenerateRandomRoll();
// Generates two random numbers for rolling dice and then adds them to get the point.
cout << newRoll << endl;
win = CheckRoll(point, newRoll, win);
// Checks the players new roll to see if they won, lost, or has to keep rolling.
}
int newBalance = NewBalance(bankBalance, wager, win);
// This function will take the original bankBalance and add or subtract the wager -
// chosen based on whether or not the player won.
CheckBalance(newBalance, donePlaying);
// Checks to see if the new bank balance is 0 and outputs a message and ends the game if it is.
AskToPlayAgain(donePlaying);
// Asks the player to enter y or n to keep playing or to quit playing.
}
}
void StartGame(int& bankBalance, int& wager) //2//
{
cout << "Press return to roll your dice...";
cin.get();
cout << endl;
cout << endl;
cout << endl;
cout << "Please enter your Bank Balance -> ";
cin >> bankBalance;
cout << endl;
cout << "Enter your wager -> ";
cin >> wager;
cout << endl;
}
int CheckWager(int bankBalance, int& wager) //3//
{
while(wager > bankBalance || wager == 0)
{
cout << "Invalid wager - must be between 0 and " << bankBalance << endl;
cout << "Please reenter -> ";
cin >> wager;
}
return wager;
}
void GenerateRandomChatter() //4//
{
int randomInt = (rand()% 5 + 1);
if(randomInt == 1)
{
cout << "Oh, you're going for broke, huh?" << endl;
}
else
if(randomInt == 2)
{
cout << "Aw cmon, take a chance!" << endl;
}
else
if(randomInt == 3)
{
cout << "You are up big. Now's the time to cash your chips!" << endl;
}
else
if(randomInt == 4)
{
cout << "Aren't you a big spender!" << endl;
}
else
if(randomInt == 5)
{
cout << "The dice may get you this time!" << endl;
}
}
int GenerateRandomRoll() //5//
{
int die1 = (rand()% 6 + 1);
int die2 = (rand()% 6 + 1);
cout << die1 << " + " << die2 << " -> ";
int point = die1 + die2;
return point;
}
char CheckDice(int point, char& win) //6//
{
if(point == 7 || point == 11)
{
cout << "You win!" << endl;
win = 'Y';
}
else
if(point == 2 || point == 3 || point == 12)
{
cout << "You lose!" << endl;
win = 'N';
}
else
{
cout << "Your point is -> " << point << endl;
}
return win;
}
bool CheckBalance(int newBalance, bool& donePlaying) //7//
{
if(newBalance == 0)
{
cout << "Sorry, You busted!" << endl;
donePlaying = true;
}
else
{
donePlaying = false;
}
return donePlaying;
}
char CheckRoll(int point, int newRoll, char& win) //8//
{
if(newRoll == 7)
{
cout << "You lose!" << endl;
win = 'N';
}
else
if(newRoll == point)
{
cout << "You win!" << endl;
win = 'Y';
}
return win;
}
int NewBalance(int bankBalance, int wager, char& win) //9//
{
int newBalance;
if(win == 'Y')
newBalance = bankBalance + wager;
else
if(win == 'N')
newBalance = bankBalance - wager;
return newBalance;
}
void AskToPlayAgain(bool donePlaying) //10//
{
char ans;
cout << "Would you like to play again (y or n)? ";
cin >> ans;
while(ans != 'y' and ans != 'n')
{
cout << "Invalid responce -- please reenter ";
cin >> ans;
}
if(ans == 'n')
{
donePlaying = true;
}
else
if(ans == 'y')
{
donePlaying = false;
}
}
|