0

所以我现在只是在学习编码。这是一个在 linux 终端上运行的程序。

游戏应该生成一个介于 2-14 之间的数字(一副牌),让用户猜测下一个数字是更高还是更低。

我遇到的问题是游戏运行正常,但游戏评估了上一回合的数字。如果我在第一轮得到 6,第二轮得到 7,第三轮得到 3,我猜第三轮的数字会更高,它会评估为正确的猜测,因为 7 高于 6,而不是应考虑到3。

此外,当输入不正确时,创建新随机数的代码也有问题。在用户输入有效选择之前,它应该保持相同的数字。

示例输出:

当前牌是 9。下一个数字是大(1)还是小(2)?2

你猜错了。你目前的分数是-1!当前数字是 9。下一个数字会更高(1)还是更低(2)?2

卡片是一样的。你目前的分数是-1!当前数字是 3 下一个数字会更高(1)还是更低(2)?

这是我的整个代码

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() 
{ 
    /*declare variables*/ 
    int pastCard; 
    int currentCard; 
    int score; 
    int userChoice; 
    int playGame = 1; 


    /*set up random*/ 
    int range; 
    srand(time(NULL)); 
    range = (13 - 1) + 1; 

    pastCard = rand() % range + 2; 
    currentCard = rand() % range + 2; 

    while (playGame == 1) 
    { 
        /*change the current card to the past card before creating new current card*/   
        pastCard = currentCard; 

        /*generate a random int for card*/ 

        currentCard = rand() % range + 2; 

        if (currentCard < 11) 
        { 
            printf("The current card is a %d.\n", currentCard); 
        } 
        else if (currentCard == 11) 
        { 
            printf("The current card is a jack.\n", currentCard); 
        } 
        else if (currentCard == 12) 
        { 
            printf("The current card is a queen.\n", currentCard); 
        } 
        else if (currentCard == 13) 
        { 
            printf("The current card is a king.\n", currentCard); 
        } 
        else if (currentCard == 14) 
        { 
            printf("The current card is an ace.\n", currentCard); 
        } 

        printf("Will the next card be higher(1) or lower(2)? (press 0 to quit)\n"); 
        scanf("%d", &userChoice); 
        printf("\n"); 


        if (userChoice == 1) 
        { 
            if (currentCard > pastCard) 
            { 
                score++; 

                printf("You have guessed correctly.\n"); 
                printf("Your current score is %d!\n", score); 
            } 
            else if (currentCard < pastCard) 
            { 
                score--; 

                printf("You have guessed incorrectly.\n"); 
                printf("Your current score is %d!\n", score); 
            } 
            else if (currentCard == pastCard) 
            { 
                printf("The cards are the same.\n"); 
                printf("Your current score is %d!\n", score); 
            } 
        } 
        else if (userChoice == 2) 
        { 
            if (currentCard < pastCard) 
            { 
                score++; 

                printf("You have guessed correctly.\n"); 
                printf("Your current score is %d!\n", score); 
            } 
            else if (currentCard > pastCard) 
            { 
                score--; 

                printf("You have guessed incorrectly.\n"); 
                printf("Your current score is %d!\n", score); 
            } 
            else if (currentCard == pastCard) 
            { 
                printf("The cards are the same.\n"); 
                printf("Your current score is %d!\n", score); 
            } 
        } 
        else if (userChoice == 0) 
        { 
            playGame = 0; 
            printf("Final score: %d\n", score); 

            score = 0; 

            printf("Play again? (press 1 for yes, 0 for no)\n"); 
            scanf("%d", &playGame); 
            printf("\n"); 
        } 
        else 
        { 
             printf("Please enter a valid choice.\n"); 
        } 

    } 

    return 0; 
}

请帮忙!这让我非常沮丧!

4

2 回答 2

1

您可以通过更改来解决您的主要问题:

printf("The current number is a %d.\n", currentCard);

至:

printf("The current number is a %d.\n", pastCard);

在所有地方,因为正如你所写的那样,currentCard是他们试图猜测的那个,而不是你想要展示给他们的那个。您可以通过将整个程序重命名pastCardcurrentCardcurrentCard来改进您的程序。nextCard

或者,使用您更新的代码,更改为:

if (pastCard < 11) {
    printf("The current card is a %d.\n", pastCard); 
} else if (pastCard == 11) {
    printf("The current card is a jack.\n", pastCard); 
} else if (pastCard == 12) {
    printf("The current card is a queen.\n", pastCard); 
} else if (pastCard == 13) {
    printf("The current card is a king.\n", pastCard); 
} else if (pastCard == 14) {
    printf("The current card is an ace.\n", pastCard); 
}
于 2013-10-21T02:21:56.403 回答
0

您在跟踪卡片时遇到了逻辑错误。您向用户展示了错误的卡。您想显示当前卡,等待输入,然后选择一张新卡。

此外,在验证用户输入时,您可以执行以下循环:

while user input not valid
    prompt for input
    scan input
    if input not valid then
         print error message
end while

只有在此循环之后,您才能继续生成新卡并进行逻辑检查。

于 2013-10-21T02:18:57.393 回答