-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcraps_game.cpp
More file actions
67 lines (54 loc) · 1.73 KB
/
craps_game.cpp
File metadata and controls
67 lines (54 loc) · 1.73 KB
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
// This simulates the game of "craps"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int rollDice(); // rolls dice, calculates and displays the sum
int main(){
// enumeration with constants that represent the game status
enum Status { CONTINUE, WON, LOST};
int myPoint; // points when there is no win/loss on first roll
Status gameStatus; // can have CONTINUE, WIN or LOST
// randomize the random number generator using current time
srand(time(0));
int sumOfDice = rollDice();
switch (sumOfDice){
case 7: //win with 7 on first roll
case 11: // win with 11 on first roll
gameStatus = WON;
break;
case 2: // lose with 2 on first roll
case 3: // lose with 3 on first roll
case 12: // lose with 12 on first roll
gameStatus = LOST;
break;
default: // did no win or lose, remember the point
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;
}
}
if (gameStatus == WON){
cout << "Player wins" << endl;
}else{
cout << "Player loses" << endl;
}
}
// roll dice, calculate sum and display results
int rollDice(){
// pick random die values
int die1 = 1 + rand() % 6;
int die2 = 1 + rand() % 6;
int sum = die1 + die2;
// display result of rolls
cout << "Player rolled " << die1 << " + " << die2 << " = " << sum << endl;
return sum;
}