简体   繁体   English

掷骰子游戏

[英]The game of Craps

Im trying to ask the user to input y or n and the game will either quit or continue. 我试图要求用户输入y或n,游戏将退出或继续。 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. 您的主要问题是,如果用户输入的内容与'n''N'不同,则使用return指令终止该主程序, return其删除并继续循环。

Better if you use a boolean variable to exit the while loop: 如果您使用布尔变量退出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;
}

The second big problem are counters of playgame function: they must be inited to 0. 第二个大问题是游戏功能的计数器:必须将它们初始化为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. 最后,将scanf格式说明符更改为" %c"以允许scanf在用户每次输入后“刷新”换行符'\\n' n'char到stdin

don't use return in while loop. 不要在while循环中使用return。 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 也不需要将其设置为true,因为直到您按下n或N时,条件一直为true

#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. 如果要在用户退出后显示总计,则需要将其存储在playGame函数之外。

The return value from playGame is pointless at the moment, so let's use it to indicate whether the player won: 目前, 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;
}

And a slight rewrite of main : 并稍微重写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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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