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
|
// Program Assignment #2
// Programmed by
// Date:
#include <iostream>
#include <cstdlib> // for using rand() function
#include <ctime> // for using time() function
// namespace directives
using namespace std ;
int CrapsRoll() ; // outcome of two six-sided dice rolls
char goAgain(); // option if user wants to play again
int main()
{
void CrapsIntro();
int myRoll;
int pointRoll; // If user hits a "point", it will be stored here
int anotherRoll;
char repeat = 'Y'; // the repeat function to correspond with the 'Y' key
srand(int(time(0))) ; // seeding random number generator
do
{ do_turn();
CrapsIntro();
myRoll = CrapsRoll(); // Uses function CrapsRoll() to genereate a random number
if (myRoll == 7 || myRoll == 11){
cout << "You win." << endl;
goAgain();
}
else if (myRoll == 2 || myRoll == 3 || myRoll == 12){
cout << "Sorry you lose." << endl;
goAgain();
}
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;
goAgain();
}
else if (myRoll == 7){
cout << "Sorry, 7 came up. You crapped out." << endl;
goAgain();
}
else {
cout << "You're still in the game." << 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.
}
while (goAgain() == 'Y');
}
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
// die one
randomNumber = rand() ; // generate random number
dieOne = (randomNumber % 6) + 1 ; // a number between 1 and 6
// die two
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.
*/
// 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 again?'" << endl;
return 0;
}
cin >> playAgain;
}
}
void CrapsIntro()
{
cout<< " Welcome to the Dice game Craps!" << endl;
cout << "A player rolls two dice. Each die has six faces. These faces contain" << endl;
cout << "1, 2, 3, 4, 5 and 6 spots. After the dice have come to rest, the sum" << endl;
cout << "of the spots on the two upward faces is calculated. If the sum is 7" << endl;
cout << "or 11 on the first roll, the player wins. If sum is 2, 3 or 12 on the" << endl;
cout << "first roll (called Craps), the player loses (i.e. the house" << endl;
cout << "wins). If the sum is 4, 5, 6, 8, 9 or 10 on the first roll, then that" << endl;
cout << "sum becomes the player's point. To win, you must continue rolling" << endl;
cout << "the dice until you make your points. The player loses by rolling" << endl;
cout << "a 7 before making the point." << endl;
cout << " " << endl;
}
|