Alex Alex - 1 month ago 6
C Question

The game of Craps

Im trying to ask the user to input y or n and the game will either quit or continue. I also want to display the total win and loose and the user quits. Maybe i'm not getting the real hand of boolean and returning stuff in functions?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>

int rollDice(void);
bool playGame(void);

int main(void)
{
srand((unsigned)(time(NULL)));
char userInput;
while (true)
{
playGame();
printf("Would you like to play again?");
scanf("%c", &userInput);
if (userInput == 'n' || userInput == 'N')
{
return false;
}
else
{
return true;
}
}
return 0;
}
int rollDice(void)
{

int dice1 = rand()%6+1;
int dice2 = rand()%6+1;
int totaldice = dice1 + dice2;

return totaldice;
}
bool playGame(void)
{
int point, total;
int winCounter, looseCounter;
printf("The game is starting!\n");
total = rollDice();
printf("You rolled: %d\n", total);
if (total == 7 || total == 11)
{

printf("Wow it's your lucky day! You Win!\n");
winCounter++;

}
else if (total == 2 || total == 3 || total == 12)
{
printf("Unlucky! You Loose!\n");
looseCounter++;
}
else {
point = total;
printf("Your Point is: %d\n", point);
while (true)
{
total = rollDice();
printf("You rolled: %d\n", total);
if (total == point)
{
printf("You made your point! You Win!\n");
winCounter++;
break;
}
else if (total == 7)
{
printf("Thats a %d. You Loose!\n", total);
looseCounter++;
break;
}
}

}return true;
}

LPs LPs
Answer

Your main problem is that in case of user input something different then 'n' or 'N' you terminate the main with return instruction.Remove it and the loop can continue.

Better if you use a boolean variable to exit the while loop:

int main(void)
{
    srand((unsigned)(time(NULL)));
    char userInput;

    bool paygame = true;

    while (paygame)
    {
        playGame();
        printf("Would you like to play again?");
        scanf(" %c", &userInput);

        printf ("Test: %c\n", userInput);
        if (userInput == 'n' || userInput == 'N')
        {
            paygame = false;
        }
    }
    return 0;
}

The second big problem are counters of playgame function: they must be inited to 0.

int winCounter = 0, looseCounter = 0;

Otherwise the count start from a random number.

If you want to count all win and loose of all played games you can simply use static vars:

bool playGame(void)
{
    int point, total;
    static int winCounter = 0, looseCounter = 0;
    printf("The game is starting!\n");
    total = rollDice();
    printf("You rolled: %d\n", total);
    if (total == 7 || total == 11)
    {

        printf("Wow it's your lucky day! You Win!\n");
        winCounter++;

    }
    else if (total == 2 || total == 3 || total == 12)
    {
        printf("Unlucky! You Loose!\n");
        looseCounter++;
    }
    else {
        point = total;
        printf("Your Point is: %d\n", point);
        while (true)
        {
            total = rollDice();
            printf("You rolled: %d\n", total);
            if (total == point)
            {
                printf("You made your point! You Win!\n");
                winCounter++;
                break;
            }
            else if (total == 7)
            {
                printf("Thats a %d. You Loose!\n", total);
                looseCounter++;
                break;
            }
        }

    }

    printf ("Won: %d - Lose: %d\n", winCounter, looseCounter);

    return true;
}

Last thing change the scanf format specifier to " %c" to allow scanf to "flush" newline '\n' char left into stdin after each input by user.