#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
int rollDie() {
return rand() % 6 + 1;
}
int playCraps() {
int die1 = rollDie();
int die2 = rollDie();
int sum = die1 + die2;
printf("You rolled: %d + %d = %d\n", die1, die2, sum);
if (sum == 7 || sum == 11) {
printf("Congratulations! You win!\n");
return 1;
} else if (sum == 2 || sum == 3 || sum == 12) {
printf("Sorry, you lose. Better luck next time!\n");
return -1;
} else {
printf("Point established: %d\n", sum);
printf("Rolling again... \n");
int point = sum;
do {
die1 = rollDie();
die2 = rollDie();
sum = die1 + die2;
printf("You rolled %d + %d = %d\n", die1, die2, sum);
if (sum == point) {
printf("Congratualations! You rolled the point. You win!\n");
} else if (sum == 7) {
printf("Sorry, you rolled a 7. You lose.\n");
return -1;
}
} while (1);
}
}
int main() {
srand(time(NULL));
printf("Welcome to craps!\n\n");
while (1) {
printf("Press Enter to roll the dice. Type 'exit' to end.\n\n");
char input[10];
fgets(input, sizeof(input), stdin);
if (strcmp(input, "exit\n") == 0) {
break;
}
int result = playCraps();
if (result == 1) {
printf("You won!\n\n\n\n");
} else if (result == -1) {
printf("You lost\n\n\n\n");
}
}
printf("Thanks for playing!\n");
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: