-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_of_chance.c
64 lines (57 loc) · 1.27 KB
/
game_of_chance.c
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
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
enum Status { CONTINUE, WON, LOST};
int rollDice(void);
int main(void)
{
// randomize random number generator using current time
srand(time(NULL));
int myPoint; // player must make this point to win
enum Status gameStatus; // can contain CONTINUE, WON, or LOST
int sum = rollDice(); // first roll of dice
switch (sum) {
//win on first roll
case 7:
case 11:
gameStatus = WON;
break;
case 2:
case 3:
case 12:
gameStatus = LOST;
break;
//remember point
default:
gameStatus = CONTINUE; // player should keep rolling
myPoint = sum;
printf_s("Point is %d\n", myPoint);
//break; // optional
}
while (CONTINUE == gameStatus) {
sum = rollDice(); // roll dice again
// determine game status
if (sum == myPoint) {
gameStatus = WON;
}
else {
if (7 == sum) {
gameStatus = LOST;
}
}
}
// display won or lost message
if (WON == gameStatus) {
puts("Player wins");
}
else {
puts("Player Loses");
}
}
int rollDice(void) {
int die1 = 1 + (rand() % 6); // pick random die1 value
int die2 = 1 + (rand() % 6); // pick random die2 value
// display result of this roll
printf_s("Player rolled %d + %d = %d\n", die1, die2, die1 + die2);
return die1 + die2;
}