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
|
#include "stdafx.h"
#include "Craps.h"
int main()
{
//the user signs in, and plays craps until they quit or run out of money
char random;
bool playerQuit = false;
// points to the logged in user's profile
UserAccount * thePlayer = NULL;
srand(time(NULL));
// sign the player in
thePlayer = SignInPlayer();
// play craps till player loses, wins, or quits
PlaySession(thePlayer);
cout << "\nThanks for playing Craps\n";
system("PAUSE");
return 0;
}
UserAccount* SignInPlayer()
{
// To create an account for asking the user name and balance
UserAccount* tempAccount = new UserAccount();
string tempName;
int tempBalance;
// ask for user name and balance
cout << "Welcome to Craps!!\n";
cout << "What is your first name?\n";
cin >> tempName;
cout << "What is your balance, " << tempName << "?\n";
cin >> tempBalance;
// set up the account
tempAccount->userName = tempName;
tempAccount->userBalance = tempBalance;
// return account
return tempAccount;
}
void PlaySession(UserAccount* player)
{
bool playerQuit = false;
bool playerWon;
int playerWinsCounter, gamesPlayedCounter;
playerWinsCounter = gamesPlayedCounter = 0;
// To play craps, the user places a bet and the dice are rolled until the player wins or loses.
do
{
// check if the player has funds to play
if (player->userBalance <= 0)
playerQuit = true;
else
// Ask if they want to play
playerQuit = AskToPlay(player->userBalance);
if(!playerQuit)
// play a game
{
playerWon = PlayOneGame(player);
gamesPlayedCounter++;
if(playerWon)
playerWinsCounter++;
}
}while(playerQuit == false && player->userBalance > 0);
}
// this function asks the player if they want to play?"
bool AskToPlay(int balance)
{
char choice;
cout << "Your balance stands at " << balance << "\n\nDo you want to roll the dice? Y or N" << endl;
cin >> choice;
cout << endl;
if (choice == 'Y' || choice == 'y')
{
return false;
}
else
{
return true;
}
}
bool PlayOneGame(UserAccount* player)
{
int betAmount,
dieTotal,
playerWinnings,
pointNumber;
bool isGameOver = false;
bool isPointSet = false;
betAmount = PlaceBet(player->userBalance);
// To play craps, the user places a bet and the dice are rolled until the player wins or loses.
do
{
// roll the dice
dieTotal = RollDice();
// if the point isn't set (first roll)
if(!isPointSet)
{
switch(dieTotal)
{
// losing cases
case 2:
case 3:
case 12:
{
isGameOver = true;
player->userBalance -= betAmount;
cout << "\nOh snap son! You lost!!\n";
return false;
break; // never executed, included as best practice
}
// winning cases
case 7:
case 11:
{
isGameOver = true;
player->userBalance += betAmount;
cout << "\nYou won " << betAmount << ". What's your secret?\n";
return true;
break; // never executed, included as best practice
}
default:
{
cout << "point set at " << dieTotal << ", rolling again.\n";
isGameOver = false;
isPointSet = true;
pointNumber = dieTotal;
break;
}
}
}
// the point is already set
else
{
// losing case
if(dieTotal == 7)
{
isGameOver = true;
player->userBalance -= betAmount;
cout << "\nOh snap! You lost!!\n";
return false;
}
else if(dieTotal == pointNumber)
{
isGameOver = true;
player->userBalance += betAmount;
cout << "\nYou won " << betAmount << " What's your secret? \n";
return true;
}
}
}while(isGameOver == false);
}
int PlaceBet(int balance)
{
int betAmount = balance +1;
//To place a bet, the user sets an amount not above their account balance
cout << "How much do you want to bet? You may bet up to your balance of " << balance << "\n";
while ( betAmount > balance)
{
cout << "Enter bet amount: ";
cin >> betAmount;
cout << endl;
if(betAmount > balance)
cout << betAmount << " is too high, please enter a number below " << balance << endl;
}
return betAmount;
}
int RollDice()
{
int dieOne = 0,
dieTwo = 0;
// generate random numbers and restrict to 1-6
dieOne = 1 + rand() % 6;
dieTwo = 1 + rand() % 6;
// tell the user what they rolled
cout << "\nYou rolled a " << dieOne << " and a " << dieTwo << ", totalling " << dieOne + dieTwo << ".\n";
system("PAUSE");
// return die total
return dieOne + dieTwo;
}
|