// General program shell for Assignment III
// This is a program that simulates the game
// of casino craps.
// #include preprocessor directives
#include <iostream>
#include <cstdlib> // for using rand() function
#include <ctime> // for using time() function
// namespace directives
using namespace std ;
void CrapsIntro() ; // brief description of Craps game
int CrapsRoll() ; // outcome of two six-sided dice rolls
void myTimeDelay(int) ; // time delay between rolls
void CrapsPassLine(); // Function for if chooses passline
void CrapsDontPassLine(); // Function for if chooses dont passline
char goAgain(); // option if user wants to play again
const int DELAY = INT_MAX / 5 ; // manual delay between rolls
int main ()
{
int mainBet;
// Display game instructions
CrapsIntro();
// Ask user to play Pass Line OR Don't Pass Line
cout << "Would you like to play the Passline or Don't Passline?" << endl;
cout << "Enter '1' for Passline or '2' for Don't Passline" << endl;
cin >> mainBet;
while (mainBet){ // Used so that user can play again if they like
if (mainBet == 1){
CrapsPassLine(); // Calls the PassLine function
if(goAgain() == 'N') {
// User can choose to play pass or dont pass again
myTimeDelay(DELAY);
return 1;
}
}
else if (mainBet == 2){
CrapsDontPassLine(); // Calls the DontPassLine
if(goAgain() == 'N') {
// User can choose to play pass or dont pass again
myTimeDelay(DELAY);
return 1;
}
}
else{
cout << "Sorry, invalid entry" << endl;
}
cin >> mainBet;
}
return 0;
}
void CrapsPassLine()
{
int myRoll;
int pointRoll; // If user hits a "point", it will be stored here
int anotherRoll;
myRoll = CrapsRoll(); // Uses function CrapsRoll() to genereate a random number
if (myRoll == 7 || myRoll == 11){
cout << "You win." << endl;
return;
}
else if (myRoll == 2 || myRoll == 3 || myRoll == 12){
cout << "Sorry you lose." << endl;
return;
}
else if (myRoll == 4 || myRoll == 5 || myRoll == 6 ||
myRoll == 8 || myRoll == 9 || myRoll == 10) {
cout << "The number you rolled is a point number." << endl;
pointRoll = myRoll;
}
cout << "Are you ready to roll again? Press '1' when ready" << endl;
cin >> anotherRoll; // Roll again after point is hit
if (anotherRoll == 1){ // If it is true (User entered 1)
while (anotherRoll == 1) // If it doesn't hit point or 7,
{ // loop is used until it does.
myRoll = CrapsRoll(); // dice roll
if (myRoll == pointRoll){
cout << "Congradulations, you Win." << endl;
return;
}
else if (myRoll == 7){
cout << "Sorry, 7 came up. You crapped out." << endl;
return;
}
else {
cout << "You're still in the game. 7 has not yet rolled." << endl;
cout << "Are you ready to roll again? Press '1' when ready" << endl;
}
cin >> anotherRoll; //go back to the loop to roll again
}
} else {
cout << "Invalid entry" << endl; // User did not enter 1.
}
}
void CrapsDontPassLine()
{
int myRoll;
int pointRoll; // If user hits a "point", it will be stored here
int anotherRoll;
myRoll = CrapsRoll(); // Uses function CrapsRoll() to genereate a random number
if (myRoll == 7 || myRoll == 11){
cout << "Sorry. You lose." << endl;
return;
}
else if (myRoll == 2 || myRoll == 3){
cout << "Congradulations, you Win." << endl;
return;
}
else if (myRoll == 12){
cout << "Game is a draw. You do not lose." << endl;
}
else if (myRoll == 4 || myRoll == 5 || myRoll == 6 ||
myRoll == 8 || myRoll == 9 || myRoll == 10) {
cout << "The number you rolled is a point number." << endl;
pointRoll = myRoll;
}
cout << "Are you ready to roll again? Press '1' when ready" << endl;
cin >> anotherRoll; // Roll again after point is hit
if (anotherRoll == 1){ // If it is true (User entered 1)
while (anotherRoll == 1) // If it doesn't hit point or 7,
{ // loop is used until it does.
myRoll = CrapsRoll(); // dice roll
if (myRoll == pointRoll){
cout << "Sorry. You lose." << endl;
return;
}
else if (myRoll == 7){
cout << "Congradulations, you win." << endl;
return;
}
else {
cout << "You're still in the game. pointRoll has not yet hit." << endl;
cout << "Are you ready to roll again? Press '1' when ready" << endl;
}
cin >> anotherRoll; //go back to the loop to roll again
}
} else {
cout << "Invalid entry" << endl; // User did not enter 1.
}
}
void CrapsIntro()
/* This function is only for the user to
understand how the game works. It's only
use is to output multiple strings.
*/
// Pre-Condition: None
// Post-Condition: None
{
cout << " The Pass Line bet" << endl;
cout << "If the come-out roll is a 7 or 11, the player wins." << endl;
cout << "If the come-out roll is 2, 3 or 12, the player loses (crapping out)." << endl;
cout << "If the come-out roll is any other value (4, 5, 6, 8, 9, or 10)," << endl;
cout << "it establishes a point." << endl;
cout << "The player continues to roll, until:" << endl;
cout << "If the established point is rolled again before a seven, the player wins." << endl;
cout << "If, with an established point, a 7 is rolled before the established point" << endl;
cout << "is re-rolled the player loses (seven out)." << endl;
cout << endl;
cout << " The Don't Pass Line bet" << endl;
cout << "If the come-out roll is 7 or 11, the player looses." << endl;
cout << "If the come-out roll is 2 or 3, the player wins." << endl;
cout << "If the come-out roll is 12, the game is a draw and the player does not lose." << endl;
cout << "If the come-out roll is any other value (4, 5, 6, 8, 9, or 10)," << endl;
cout << "it establishes a point." << endl;
cout << "The player continues to roll, until:" << endl;
cout << "If the established point is rolled again before a seven, the player loses." << endl;
cout << "If, with an established point, a 7 is rolled before," << endl;
cout << "the established point is re-rolled the player wins." << endl ;
cout << endl;
}
void myTimeDelay(int delay)
// This function creates a time delay so
// that the computer doesn't generate the
// same outcome number for the dice.
{
int count = 0 ;
while (delay > count)
{
// do nothing delay period
count++;
}
}
int CrapsRoll()
/* The purpose of this function is to create
a random roll for two die and return to the
caller. It has another function inside for
time delay.
*/
// Pre-Condition: None
// Post-Condition: Send back to the caller
// the sum of two die's being randomly rolled
{
int randomNumber ; // a random number
int dieOne ; // a six-sided die roll value
int dieTwo ; // a six-sided die roll value
cout << "Please Wait..." << endl;
myTimeDelay(DELAY);
cout << "Rolling my two dice ..." << endl ;
// die one
srand(int(time(0))) ; // seeding random number generator
randomNumber = rand() ; // generate random number
dieOne = (randomNumber % 6) + 1 ; // a number between 1 and 6
myTimeDelay(DELAY) ;
// die two
srand(int(time(0))) ; // seeding random number generator
randomNumber = rand() ; // generate random number
dieTwo = (randomNumber % 6) + 1 ; // a number between 1 and 6
cout << "You rolled a " << dieOne + dieTwo << endl ;
return dieOne + dieTwo ;
}
char goAgain()
/* This function prompts the user if they would
like to play again. They can either press
'Y' for yes or 'N' for no. They also have a
choice in changing to the passline OR the dont passline
*/
// Pre-Condition: None
// Post-Condition: If 'Y' || 'N' is entered,
// it sends back to the caller and enters a loop.
{
cout << "Would you like to play again?" << endl;
cout << "Enter 'Y' for Yes or 'N' for No." << endl;
char playAgain;
cin >> playAgain;
while (playAgain) {
if (playAgain != 'Y' && playAgain != 'N'){
cout << "Invalid entry. Please select 'Y' or 'N'. (Capitals)" << endl;
}
if (playAgain == 'N'){
cout << "Thank you for playing. Good Bye" << endl;
return playAgain;
}
else if (playAgain == 'Y'){
cout << "Would you like to play the Passline or Don't Passline?" << endl;
cout << "Enter '1' for Passline or '2' for Don't Passline" << endl;
return playAgain;
}
cin >> playAgain;
}
} |