user1145538
user1145538

Reputation: 647

C: Craps / Dice Game

Purpose of this code: simulate 100 games of CRAPS and record the # of first round losses, first round wins, second round losses PLUS points, and second round wins PLUS points.

Those of you not familiar with the rules of CRAPS; you basically roll two die, if the result is anyhting other than a total of 2, 3, or 12, you get to roll again (the number you rolled that turn is preserved and added to your points). If you roll a 7 or 11 you automatically win.

This is where i'm at so far:

#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);
}

All i ask of you is that you give me options on how i can preserve those points to be printed at the end??

Moreover, i feel my code could be written better and less redundantly, suggestions?

#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. Press any key to continue.\n");
//getchar();

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

switch(sumd){
    case 7:
    case 11:
        printf("You rolled %d, you win.\n", sumd);
        winf++;
        break;
    case 2:
    case 3:
    case 12:
        printf("You rolled %d, you lose.\n", sumd);
        lostf++;
        break;
    default:
        while (1) {
            d1 = rand()%6+1;
            d2 = rand()%6+1;
            sumd2 = d1 + d2;

            if (sumd2==sumd){ 
                printf("You rolled your points(%d), you win.\n",sumd);
                winp++;
            break;}
            if (sumd2==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. \n", winf, lostf, winp, lostp);
}

Upvotes: 0

Views: 4161

Answers (2)

Dave
Dave

Reputation: 11162

You could rather easily condense both occurances of

d1 = rand()%6+1;
d2 = rand()%6+1;
sumd2 = d1 + d2;

into a a function:

int rolldice(){
    int d1,d2;
    d1 = rand()%6+1;
    d2 = rand()%6+1;
    return d1 + d2;
}

Or in one-liner form:

int rolldice(){
    return (rand()%6)+(rand()%6)+2;
}

Then you would be writing

sumd = rolldice();

Upvotes: 2

Free Monica Cellio
Free Monica Cellio

Reputation: 2290

Your solution of putting the results in ints to print at the end looks reasonable. If I'm understanding the question right, it seems winp and lostp should add sumd2 instead of just incrementing. Or is it already working ok and I'm misreading the question?

You might also want to look at the switch statement:

switch(sumd){
    case 7:
    case 11:
        //existing code goes here
        break;

    case 2:
    case 3:
    case 12:
        //more existing code
        break;

    default:
        //code for games that don't end on the first turn
        break;
}

Upvotes: 1

Related Questions