/
crapsFixByFourSub.cpp
113 lines (93 loc) · 3.04 KB
/
crapsFixByFourSub.cpp
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
// Fig. 5.14: fig05_14.c
// Simulating the game of craps.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int rollDice( void ); // function prototype
int compareDiceNumber(int sum);
int getStatis(int *myPoint, int status);
void getResult(int gameStatus);
// function main begins program execution
int main( void )
{
int sum; // sum of rolled dice
// enumeration constants represent game status
enum Status { CONTINUE, WON, LOST };
enum Status gameStatus; // can contain CONTINUE, WON, or LOST
sum = rollDice(); // first roll of the dice
int realStatus = compareDiceNumber(sum);
getResult(realStatus);
system("pause");
return 0;
} // end main
// roll dice, calculate sum and display results
int rollDice( void )
{
int die1; // first die
int die2; // second die
int workSum; // sum of dice
srand( time( NULL ) );
die1 = 1 + ( rand() % 6 ); // pick random die1 value
die2 = 1 + ( rand() % 6 ); // pick random die2 value
workSum = die1 + die2; // sum die1 and die2
// display results of this roll
printf( "Player rolled %d + %d = %d\n", die1, die2, workSum );
return workSum; // return sum of dice
} // end function rollRice
int compareDiceNumber(int sum){
// enumeration constants represent game status
enum Status { CONTINUE, WON, LOST };
enum Status gameStatus;
int SecondPoint = 0;
switch( sum ) {
// win on first roll
case 7: // 7 is a winner
case 11: // 11 is a winner
gameStatus = WON; // game has been won
break;
// lose on first roll
case 2: // 2 is a loser
case 3: // 3 is a loser
case 12: // 12 is a loser
gameStatus = LOST; // game has been lost
break;
// remember point
default:
gameStatus = CONTINUE; // player should keep rolling
SecondPoint = sum; // remember the point
printf( "Point is %d\n", SecondPoint );
break; // optional
} // end switch
int getStatus = getStatis(&SecondPoint, gameStatus);
// while game not complete
return getStatus;
}
int getStatis(int *SecondPoint, int gameStatus){
int sum;
int myPoint = *SecondPoint;
enum Status { CONTINUE, WON, LOST };
while ( CONTINUE == gameStatus ) { // player should keep rolling
sum = rollDice(); // roll dice again
// determine game status
if ( sum == myPoint ) { // win by making point
gameStatus = WON; // game over, player won
return gameStatus;
} // end if
else {
if ( 7 == sum ) { // lose by rolling 7
gameStatus = LOST; // game over, player lost
return gameStatus;
} // end if
} // end else
} // end while
}
void getResult(int gameStatus){
enum Status { CONTINUE, WON, LOST };
// display won or lost message
if ( WON == gameStatus ) { // did player win?
puts( "Player wins" );
} // end if
else { // player lost
puts( "Player loses" );
} // end else
}