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
|
//Title: craps.cpp
//Name: Norman Gesrtrud
//Date: October 22, 2007
//Description: Plays craps with the user
#include <iostream.h>
#include <string>
#include <cmath>
#include <stdlib.h>
#include <iomanip.h>
#include <ctime>
int rollWhip (void);
int rollBounce (void);
int rollNormal (void);
int houseRoll (void);
int main ()
{
//Declaring Variables
string yn;
int point = 0;
int result = 0;
int cash = 100;
int bet = 0;
int houseResult = 0;
string response;
srand(time(NULL));
enum stat {CON, WIN, LOS};
stat gameStat;
gameStat = CON;
//Idiot proofing - Do/While loop
do
{
cout << " ____________ ________ _________ _________ _______
cout << " / _____ / / ____ \ / ____ | / _____ \ / ____/" << endl;
cout << " / / / / / / / / / / | | / / / / / / " << endl;
cout << " / / / / / /___/ / / /_____| | / /____/ / / /___ " << endl;
cout << " / / /__/ / _____/ / ______ / / ________/ \____ \ " << endl;
cout << " / / __ / _ \ / / / / / / \ \ " << endl;
cout << " / / / / / / \ \ / / / / / / / / " << endl;
cout << " / /_____/ / / / \ \ / / / / / / ______/ / " << endl;
cout << " /___________/ /__/ \__\ /__/ /__/ /__/ /________/ " << endl;
cout << "\fCraps v. 4.01 - Based on the game from \"C++ How to Program\""<< endl;
cout << "\nThis program will play a game of Craps with you." << endl;
cout << "\nYou will be given $100 to begin with. You may bet up to $50 per roll. You can
enter $0 to pass" << endl;
cout << "\nDo you know how to play Craps? [y/n]: ";
cin >> yn;
}
while (yn != "n" && yn != "N" && yn != "y" && yn != "Y");
//Idiot proofing - Do/While loop, If/Else answer verification
if (yn == "n" || yn == "N")
{
do
{
cout << "\fTwo dice are thrown, if the sum of the two dice is equal to 7 or 11 on the
first toss the player wins. If the sum of the dice are equal to 2, 3, or 12 on
the first toss the house wins (these numbers are called the craps). If the sum
is 4, 5, 6, 8, 9, or 10 for the first toss, the sum rolled becomes the plays
\"point.\" To win the game you must roll your \"point\" again before you roll a sum
of 7, in which case the house wins." << endl;
cout << "\nAre you ready to play? [y/n]: ";
cin >> yn;
}
while (yn != "y" && yn != "Y" && yn != "n" && yn != "N");
}
//Idiot proofing - Do/While loop, If/Else answer verification
do
{
if (yn == "y" || yn == "Y")
{
string toss;
string firstToss;
//Idiot proofing - Do/While loop, If/Else answer verification
do
{
cout << "\fPress Ctrl-C to exit this program at anytime." << endl;
do
{
cout << "\nCurrent cash: $" << cash << endl;
cout << "Enter bet (no more than $50, enter 0 to pass): ";
cin >> bet;
}
while (bet > 50 || bet < 0 && bet < 0 && bet > 50);
cout << "\nPlease select a rolling technique [1/2/3]: \n\n1. Whip 2. Bounce 3. Normal" << endl;
cout << "\n[Hint: choose multiple rolling techniques to improve your odds!]" << endl;
cout << "Enter your rolling technique: ";
cin >> toss;
cout << "\f==============================\n\n";
}
while (toss != "1" && toss != "2" && toss != "3");
if (toss == "1")
result = rollWhip();
if (toss == "2")
result = rollBounce();
if (toss == "3")
result = rollNormal();
houseResult = houseRoll();
//Switch statements, first part of quasi-game engine, changes enumerated gameStat value, dependant on first roll
switch (result)
{
case 7:
case 11:
gameStat = WIN;
break;
default:
gameStat = CON;
point = result;
cout << "\nYour point is " << point << endl;
break;
}
switch (houseResult)
{
case 2:
case 3:
case 12:
gameStat = LOS;
break;
default:
break;
}
//While loop to run game, dependant on the the enumerated gameStat value
while (gameStat == CON)
{
string toss;
string firstToss;
int secResult = 0;
//Do/While statement to idiot proof the second and on roll
do
{
cout << "\n\n==============================";
cout << "\nPress Ctrl-C to quit anytime." << endl;
cout << "\nCurrent cash: $" << cash << endl;
cout << "Current bid: $" << bet << endl;
cout << "\nYour point is " << point << endl;
cout << "\nPlease select a rolling technique [1/2/3]: \n\n1. Whip 2. Bounce 3. Normal" << endl;
cout << "\n[Hint: Choose multiple rolling techniques to improve your odds!]" << endl;
cout << "Enter your rolling technique: ";
cin >> toss;
cout << "\f==============================\n\n";
}
while (toss != "1" && toss != "2" && toss != "3");
if (toss == "1")
secResult = rollWhip();
if (toss == "2")
secResult = rollBounce();
if (toss == "3")
secResult = rollNormal();
houseResult = houseRoll();
//If/Else statements, second part of quasi-game engine, changes enumerated gameStat value, dependant on last roll
if (secResult == point)
gameStat = WIN;
else
gameStat = CON;
if (houseResult == 7)
gameStat = LOS;
}
if (gameStat == LOS)
cash -= bet;
if (gameStat == WIN)
cash += bet;
if (gameStat == WIN || gameStat == LOS)
cout << "\nCurrent cash: " << cash << endl;
//If/Else statements, acts if gameStat declares the game a Win or Loss
if (gameStat == WIN)
cout << "Congrats, you win!" << endl;
if (gameStat == LOS)
cout << "Hahaha. Fail." << endl;
}
else
{
cout << "Play again soon.\n" << endl;
return 0;
}
//First part of program Do/While loop to keep playing
cout << "Would you like to keep playing? [y/n] ";
cin >> response;
//Idiot proofing - Do/While loop, If/Else answer verification
if (response != "y" && response != "Y" && response != "n" && response != "N")
do
{
cout << "\n\nIncorrect answer." << endl;
cout << "\nWould you like to keep playing? [y/n] ";
cin >> response;
}
while (response != "y" || response != "Y" || response != "n" || response != "N");
if (response == "n" || response == "N")
{
cout << "Play again soon.\n" << endl;
return 0;
}
//End of program Do/While loop, runs program again if user desires
}
while (response == "y" || response == "Y");
return 0;
}
|