简体   繁体   中英

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

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.

don't use return in while loop. instead, use a variable and use it for condition. also no need to make it true since all the time the condition is true until you pressed n or N

#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;
    bool again = true;
    while (again==true)
    {
        printf("Would you like to play again?");
        scanf("%c", &userInput);
        if (userInput == 'n' || userInput == 'N')
        {
            again = false;
        }
    }
    return 0;
}

If you want to display the totals after the user quits you need to store them outside of the playGame function.

The return value from playGame is pointless at the moment, so let's use it to indicate whether the player won:

bool playGame(void)
{
    int point, total;
    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");
        return true;
    }
    else if (total == 2 || total == 3 || total == 12)
    {
        printf("Unlucky! You Lose!\n");
        return false;
    }
    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");
                return true;
            }
            else if (total == 7)
            {
                printf("Thats a %d. You Lose!\n", total);
                return false;
            }
        }

    }
    return false;
}

And a slight rewrite of main :

int main(void)
{
    srand((unsigned)(time(NULL)));
    int total = 0;
    int wins = 0;
    char userInput;
    while (true)
    {
        total += 1;
        if (playGame())
        {
            wins += 1;
        }
        printf("Would you like to play again?");
        scanf("%c", &userInput);
        if (userInput == 'n' || userInput == 'N')
        {
            break;
        }
    }
    printf("Of %d games, you won %d.", total, wins);
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM