骰子游戏

时间:2016-10-11 10:06:12

标签: c boolean void

我试图让用户输入y或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;
    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;
}

3 个答案:

答案 0 :(得分:0)

您的主要问题是,如果用户输入的内容不同于'n''N',则使用return指令终止主要内容。移除它并循环可以继续。

如果使用布尔变量退出while循环,则更好:

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

第二大问题是playgame函数的计数器:它们必须是0。

int winCounter = 0, looseCounter = 0;

否则,计数从随机数开始。

如果你想计算所有游戏中的所有胜利和松散,你可以简单地使用静态变量:

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

最后一件事情是将scanf格式说明符更改为" %c",以允许scanf到&#34; flush&#34;在用户每次输入后,换行符'\n'字符留在stdin中。

答案 1 :(得分:0)

不要在while循环中使用return。相反,使用变量并将其用于条件。也没有必要让它成为现实,因为条件为真,直到你按下n或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;
}

答案 2 :(得分:0)

如果要在用户退出后显示总计,则需要将其存储在playGame功能之外。

目前playGame的返回值毫无意义,所以我们用它来表明玩家是否赢了:

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

略微改写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;
}