Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 掷骰子游戏_C_Boolean_Void - Fatal编程技术网

C 掷骰子游戏

C 掷骰子游戏,c,boolean,void,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(

我试图要求用户输入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;
}
#包括
#包括
#包括
#包括
int rollDice(无效);
布尔游戏(无效);
内部主(空)
{
srand((未签名)(时间(空));
字符用户输入;
while(true)
{
游戏();
printf(“你想再玩一次吗?”);
scanf(“%c”、&userInput);
如果(userInput='n'| | userInput=='n')
{
返回false;
}
其他的
{
返回true;
}
}
返回0;
}
int rollDice(无效)
{
int dice1=rand()%6+1;
int dice2=rand()%6+1;
int totaldice=dice1+dice2;
返回totaldice;
}
布尔游戏(无效)
{
整数点,总计;
int winCounter,松开计数器;
printf(“游戏开始了!\n”);
总计=滚动骰子();
printf(“您滚动了:%d\n”,总计);
如果(总计=7 | |总计=11)
{
printf(“哇,这是你的幸运日!你赢了!\n”);
winCounter++;
}
否则如果(总计=2 | |总计=3 | |总计=12)
{
printf(“不走运!你输了!\n”);
计数器++;
}
否则{
点数=总数;
printf(“您的点是:%d\n”,点);
while(true)
{
总计=滚动骰子();
printf(“您滚动了:%d\n”,总计);
如果(总数==点)
{
printf(“你说到点子上了!你赢了!\n”);
winCounter++;
打破
}
否则,如果(总计==7)
{
printf(“这是一个%d。您松开了!\n”,总计);
计数器++;
打破
}
}
}返回true;
}

您的主要问题是,如果用户输入了不同的内容,那么
'n'
'n'
您可以使用
返回
指令终止main。删除它,循环可以继续

如果使用布尔变量退出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
在用户每次输入后将换行符
'\n'
字符左移为
stdin

不要在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;
}
#包括
#包括
#包括
#包括
int rollDice(无效);
布尔游戏(无效);
内部主(空)
{
srand((未签名)(时间(空));
字符用户输入;
布尔再次=真;
while(再次==true)
{
printf(“你想再玩一次吗?”);
scanf(“%c”、&userInput);
如果(userInput='n'| | userInput=='n')
{
再次=假;
}
}
返回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;
}

scanf(“%c”、&userInput)-->
scanf(“%c”和用户输入)
int winCounter,计数器-->
int winCounter=0,looseCounter=0
返回true
-->
//返回true
Hi@LPs,我是否应该在主体中声明赢/输计数器,而不是在游戏中执行func?“loose”->“loose”。Hi@molbdnilo只是一个简单的问题。在我的课本中,我们需要使用bool(void)。当我们在函数末尾返回false时,我们实际上在做什么?我看不出函数末尾返回false和true的区别。。。。