Created
November 23, 2017 17:27
-
-
Save samueleverett01/32fe8e4b4684c25b288d29cae9703fcf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//This program implements a simple game of craps using some random number functions | |
#include <iostream> | |
#include <cmath> | |
#include <cstdlib> | |
#include <ctime> | |
using namespace std; | |
int rollDice(); | |
int main() | |
{ | |
//enumerate with constants that represent the game status | |
enum Status {CONTINUE, WON, LOST}; | |
int myPoint; | |
Status gameStatus; //can have continue, won, or lost | |
srand(time(0)); | |
int sumOfDice = rollDice(); //first roll of the dice | |
switch (sumOfDice) | |
{ | |
case 7: | |
case 11: | |
gameStatus = WON; | |
break; | |
case 2: | |
case 3: | |
case 12: | |
gameStatus = LOST; | |
break; | |
default: | |
gameStatus = CONTINUE; | |
myPoint = sumOfDice; | |
cout << "Point is: " << myPoint << endl; | |
break; | |
} | |
while (gameStatus == CONTINUE) | |
{ | |
sumOfDice = rollDice(); | |
if (sumOfDice == myPoint){ | |
gameStatus = WON; | |
} | |
else { | |
if (sumOfDice == 7) { | |
gameStatus = LOST; | |
} | |
} | |
} | |
//display if won or lost | |
if (gameStatus == WON) | |
{ | |
cout << "Player wins" << endl; | |
} | |
else { | |
cout << "Player loses" << endl; | |
} | |
} // end main | |
int rollDice() | |
{ | |
//pick random die value | |
int die1 = 1 + rand() % 6; | |
int die2 = 1 + rand() % 6; | |
int sum = die1 + die2; | |
cout << "Player rolled " << die1 << " + " << die2 << | |
" = " << sum << endl; | |
return sum; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment