Walter
Walter

Reputation: 125

Craps game and function

I'm creating a sort of craps game using C.

if sum=7,11 win; if sum=2,3,12 lose; else continue.

I have some problems with the function play_game() because the return of the function isn't correct. For example, if the sum is 3 sometimes doesn't return 1 but 2.

int roll_dice(void);
int play_game(void);
int main(){
    int sum;
    char play;

    do{
        roll_dice();
        play_game();

        if (play_game()==0){
            printf ("\nYou win!");  
            printf ("\nPlay again? ");
            play=getchar(); 
        }
        else {
            if (play_game()==1) {
                printf ("\nYou lose!"); 
                printf ("\nPlay again? ");
                play=getchar(); 
            }   
            else {
                if (play_game()==2) play='y';
                else system ("pause");
            }   
        }                   
    }
    while (play=='y');
    return 0;       
}

int roll_dice(void){
    int sum = rand()%6 + rand()%6;
    return sum;
}


int play_game(void){
    int sum = roll_dice();
    printf ("\nYou rolled: %d", sum);
    if ((sum==7)||(sum==11)) return 0;
    else {
        if ((sum==2)||(sum==3)||(sum==12)) return 1;    
        else {
            printf ("\nYour point is %d", sum);
            return 2;   
        }   
    }   
}

Upvotes: 2

Views: 433

Answers (2)

chux
chux

Reputation: 153498

In addition to the algorithm problems pointed out in @dbush fine answer, the dice rolls are not simulated correctly. @Klas Lindbäck

// better name: roll_2dice()
int roll_dice(void){
    // int sum = rand()%6 + rand()%6;  // wrong simulation
    int sum = (rand()%6 + 1) + (rand()%6 + 1); // corrected.
    return sum;
}

Further, a good rand() tends to be expensive in computational time. Alternative that only uses 1 rand() call

int roll_2dice(void){
    int r = rand()%36;  // 36 combinations
    int die1 = r/6 + 1;
    int die2 = r%6 + 1;
    return die1 + die2;
}

Upvotes: 0

dbush
dbush

Reputation: 223927

You're calling play_game too often.

You call it once at the start of the loop but discard the result. You call it again and check if the result is 0. If it is not, you call it again and check if the result is 1, if not you call it again and check if the result is 2.

You need to call play_game only once in each loop and store the return value so you can check it later without calling the function again.

do{
    const int result = play_game();   // save result

    if (result ==0){      // use saved result
        printf ("\nYou win!");  
        printf ("\nPlay again? ");
        play=getchar(); 
    }
    else {
        if (result ==1) {     // use saved result
            printf ("\nYou lose!"); 
            printf ("\nPlay again? ");
            play=getchar(); 
        }   
        else {
            if (result ==2) play='y';    // use saved result
            else system ("pause");
        }   
    }                   
}
while (play=='y');

Upvotes: 5

Related Questions