-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcraps.c
More file actions
59 lines (53 loc) · 1.45 KB
/
craps.c
File metadata and controls
59 lines (53 loc) · 1.45 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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/*
TODO:
Craps:
A player rolls two 6 sided dice, the sum is then calculated.
if the sum is 7 or 11 on the first roll the player wins
if the sum is 2, 3 or 12 the player looses (craps)
else the sum becomes the 'point'
the player then continues to roll until they 'make their point'
if the player rolls a 7 before doing so, they loose
*/
enum Status {CONTINUE, WIN, LOOSE};
int rollDice(void);
int main(void){
int sum, myPoint;
enum Status gameStatus;
srand(time(NULL));
sum = rollDice();
if (sum == 7 || sum == 11) {
gameStatus = WIN;
} else if (sum == 2 || sum == 3 || sum == 12) {
gameStatus = LOOSE;
} else {
myPoint = sum;
printf("%s%d\n", "Point is: ", myPoint);
gameStatus = CONTINUE;
}
while (gameStatus == CONTINUE) {
sum = rollDice();
if(sum == 7){
gameStatus = LOOSE;
} else {
if (sum == myPoint) {
gameStatus = WIN;
}
}
}
if(gameStatus == WIN){
printf("%s\n", "Player wins!");
} else {
printf("%s\n", "Player looses!");
}
}
int rollDice(void){
int die1, die2, workSum;
die1 = 1 + rand() % 6;
die2 = 1 + rand() % 6;
workSum = die1 + die2;
printf("%s%d%s%d%s%d\n", "Player rolled ", die1, " + ", die2, " = ", workSum);
return workSum;
}