簡體   English   中英

擲骰子游戲

[英]The game of Craps

我試圖要求用戶輸入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;
}

您的主要問題是,如果用戶輸入的內容與'n''N'不同,則使用return指令終止該主程序, 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;
}

第二個大問題是游戲功能的計數器:必須將它們初始化為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' n'char到stdin

不要在while循環中使用return。 而是使用變量並將其用於條件。 也不需要將其設置為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;
}

如果要在用戶退出后顯示總計,則需要將其存儲在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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM