For a class in C programming I have been asked to create a dice game. Here are the requirements.
A player rolls two dice. Each die has six faces. These faces contain 1,2,3,4,5, and 6 spots. After the dice have come to rest, the sum of the spots on the two upward faces is calculated. If the sum is 7 or 11 on the first throw, the player wins. If the sum is 2,3, or 12 on the first throw (called “craps”), the player loses (i.e., the “house” wins). If the sum is 4,5,6,8,9, or 10 on the first throw, then that sum becomes the player’s “points.” To win, you must continue rolling the dice until you “make your points.” The player loses by rolling a 7 before making the points.
Make a program to simulate the game of craps for 100 times and print number of the wins in first roll, lost in first roll, wins with points, and lost with points.

Here is my code so far, but I am having a difficult time to have the program save the and print at the end of the game the number of wins in the first roll, lost in first roll, wins with points, and lost with points. Any help would be appreciated.

``````#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

int main ()
{
int i,d1,d2,sumd;
double winf = 0, lostf = 0, winp = 0, lostp = 0;
printf("This program will simulate the game of craps for 100 times. ");

for (i=0; i<100; i++) {
d1 = rand()%6+1;
d2 = rand()%6+1;
sumd = d1 + d2;

if (sumd=7,11) {
printf("You rolled a 7 or an 11, you win. ");
winf++;
}
if (sumd=2,3,12) {
printf("You rolled a 12, a 3, or a 2, you lose. ");
lostf++;
}
if (sumd=4,5,6,8,9,10) {
d1 = rand()%6+1;
d2 = rand()%6+1;
sumd = d1 + d2;
if (sumd=7) {
printf("You rolled a 7, you lose. ");
lostp++;
}
if (sumd=4,5,6,8,9,10) {
printf("You rolled your points, you win. ");
winp++;
}
}
}

printf("First roll wins: %d, First roll loses: %d, Second roll wins: %d, Second roll loses: %d. ", winf, lostf, winp, lostp);
}``````

## Recommended Answers

``````if (sumd==4 || sumd==5 || sumd==6 || sumd==8 || sumd==9 || sumd==10) {
d1 = rand()%6+1;
d2 = rand()%6+1;
sumd = d1 + d2;
if (sumd==7) {
printf("You rolled a 7, you lose.\n");
lostp++;
}
if (sumd==4 || sumd==5 || sumd==6 || sumd==8 || sumd==9 || sumd==10) …``````

I have tried doing a while loop, but it does not seem to work...

Why? Since we can't see your computer, you have to tell us what you see. Are your braces in the wrong place?

Try

## All 10 Replies

if (sumd=7,11)

line 18,22,26,34, for or operation the condition will be like this:-

``if(sumd==7 || sumd==11)``

line 18,22,26,34, for or operation the condition will be like this:-

``if(sumd==7 || sumd==11)``

Ok, I'll try that. Thanks. :)

I made the changes but the result after I run the game does not seem to be random, and the number of wins and loses at the end do not add up to 100. But they are closer than before. Here is the code after I changed it.

``````#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

int main ()
{
int i,d1,d2,sumd;
double winf = 0, lostf = 0, winp = 0, lostp = 0;
printf("This program will simulate the game of craps for 100 times.\n");

for (i=0; i<100; i++) {
d1 = rand()%6+1;
d2 = rand()%6+1;
sumd = d1 + d2;

if (sumd==7 || sumd==11) {
printf("You rolled a 7 or an 11, you win.\n");
winf++;
}
if (sumd==2 || sumd==3 || sumd==12) {
printf("You rolled a 12, a 3, or a 2, you lose.\n");
lostf++;
}
if (sumd==4 || sumd==5 || sumd==6 || sumd==8 || sumd==9 || sumd==10) {
d1 = rand()%6+1;
d2 = rand()%6+1;
sumd = d1 + d2;
if (sumd==7) {
printf("You rolled a 7, you lose.\n");
lostp++;
}
if (sumd==4 || sumd==5 || sumd==6 || sumd==8 || sumd==9 || sumd==10) {
printf("You rolled your points, you win.\n");
winp++;
}
}
}

printf("First roll wins: %lf, First roll loses: %lf, Second roll wins: %lf, Second roll loses: %lf. ", winf, lostf, winp, lostp);
}``````

The two statements are confusing :--

If the sum is 7 or 11 on the first throw, the player wins.

and

The player loses by rolling a 7 before making the points.

the code is working as per your logic

``````#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

int main ()
{
int i,d1,d2,sumd;
int winf = 0, lostf = 0, winp = 0, lostp = 0;
//  printf("This program will simulate the game of craps for 100 times.\n");

for (i=0; i<10; i++) {
d1 = rand()%6+1;
d2 = rand()%6+1;
sumd = d1 + d2;
printf("%d  %d  sum:-%d\n",d1,d2,sumd);
if (sumd==7 || sumd==11) {
printf("You rolled a 7 or an 11, you win.\n");
winf++;
}
if (sumd==2 || sumd==3 || sumd==12) {
printf("You rolled a 12, a 3, or a 2, you lose.\n");
lostf++;
}
if (sumd==4 || sumd==5 || sumd==6 || sumd==8 || sumd==9 || sumd==10) {
d1 = rand()%6+1;
d2 = rand()%6+1;
sumd = d1 + d2;
if (sumd==7) {
printf("You rolled a 7, you lose.\n");
lostp++;
}
if (sumd==4 || sumd==5 || sumd==6 || sumd==8 || sumd==9 || sumd==10) {
printf("You rolled your points, you win.\n");
winp++;
}
}
}

printf("First roll wins: %d, First roll loses: %d, Second roll wins: %d, Second roll loses: %d. ", winf, lostf, winp, lostp);
}``````
commented: Helpful +1

It seems to be working when I run it. The problem is counting the number of wins and loses.

Sorry I just counted the number of times that it runs, and it seems to be stuck on 88 runs.

``````if (sumd==4 || sumd==5 || sumd==6 || sumd==8 || sumd==9 || sumd==10) {
d1 = rand()%6+1;
d2 = rand()%6+1;
sumd = d1 + d2;
if (sumd==7) {
printf("You rolled a 7, you lose.\n");
lostp++;
}
if (sumd==4 || sumd==5 || sumd==6 || sumd==8 || sumd==9 || sumd==10) {
printf("You rolled your points, you win.\n");
winp++;
}
}
}
}``````

The logic above is wrong. If you first rolled a 5 (sumd = 5), you get into this section. Then 3 possibilities exist:
1) You now roll a new value (sumd = 7) and loose, which you count.
2) You now roll a new value (sumd = 8) and your next big IF is true. But you did not roll your point. You rolled an 8. 8 != 5.
3) You now roll a new value (sumd = 12) and neither IF is true. So you did not even count this turn.
So if on your first roll you roll a point, you make 1 more roll and you're done.

Look at the instructions again. What are you supposed to do when you roll a point?

I have tried doing a while loop, but it does not seem to work and I do not have many other ideas how to do this.

``````if (sumd==4 || sumd==5 || sumd==6 || sumd==8 || sumd==9 || sumd==10) {
while (1) {
d1 = rand()%6+1;
d2 = rand()%6+1;
sumd2 = d1 + d2;

if (sumd2==sumd){
printf("You rolled your points, you win.\n");
winp++;}
break;
if (sumd==7){
printf("You rolled a 7, you lose.\n");
lostp++;}
break;
}``````

I have tried doing a while loop, but it does not seem to work...

Why? Since we can't see your computer, you have to tell us what you see. Are your braces in the wrong place?

Try formatting your code better. You are getting confused because you can't see your blocks correctly.

Shouldn't you display each roll? That's what happens at the casino -- you get to see each throw of the dice, not just the winning/losing roll.

I tried formatting my code better and found a mistake. I think that it works now, thanks so much for all your help.

``````#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

int main ()
{
int i,d1,d2,sumd,sumd2;
double winf = 0, lostf = 0, winp = 0, lostp = 0;
printf("This program will simulate the game of craps for 100 times.\n");

for (i=0; i<100; i++) {
d1 = rand()%6+1;
d2 = rand()%6+1;
sumd = d1 + d2;

if (sumd==7 || sumd==11) {
printf("You rolled a 7 or an 11, you win.\n");
winf++;
}
if (sumd==2 || sumd==3 || sumd==12) {
printf("You rolled a 12, a 3, or a 2, you lose.\n");
lostf++;
}
if (sumd==4 || sumd==5 || sumd==6 || sumd==8 || sumd==9 || sumd==10) {
while (1) {
d1 = rand()%6+1;
d2 = rand()%6+1;
sumd2 = d1 + d2;

if (sumd2==sumd){
printf("You rolled your points, you win.\n");
winp++;
break;}
if (sumd==7){
printf("You rolled a 7, you lose.\n");
lostp++;
break;}
}
}
}

printf("First roll wins: %lf, First roll loses: %lf, Second roll wins: %lf, Second roll loses: %lf. ", winf, lostf, winp, lostp);
}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.