0

嗯,我先自我介绍一下。我是 Ben,一位来自荷兰的 17 岁“游戏程序员”,他刚刚开始使用 C++ 编程(大约一个月前开始,但现在已经编程了一年)(我正在使用 Microsoft Visual Studio 2012 作为编译器)。现在,我正在“自己学习”,但我仍然使用一本书,那本书名为 Michael Dawson 的“通过游戏编程开始 C++,第三版”。

我刚刚完成了第二章,最后的练习是:“编写一个新版本的猜我的号码程序,玩家和计算机在其中切换角色。也就是说,玩家选择一个数字,计算机必须猜它是什么。”

以下是“猜我的号码”程序的代码:

// Guess My Number
// The classic number guessing game

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    srand(static_cast<unsigned int>(time(0)));  //seed random number generator

    int secretNumber = rand() % 100 + 1;    // random number between 1 and 100
    int tries = 0;
    int guess;

    cout << "\tWelcome to Guess My Number\n\n";
    do
    {
        cout << "Enter a guess: ";
        cin >> guess;
        ++tries;

        if (guess > secretNumber)
        {
            cout << "Too high!\n\n";
        }
        else if (guess < secretNumber)
        {
            cout << "Too low!\n\n";
        }
        else
        {
            cout << "\nThat's it! You got it in " << tries << " guesses!\n";
        }
    } while (guess != secretNumber);

    return 0;
}

现在,我忙于思考、编程测试,但它根本行不通。看来我陷入了这样的无限循环。但我找不到问题。

这是代码,欢迎使用其他方法来解决此问题,请记住,我不太了解该语言。;)

// Guess My Number 2
// The classic number guessing game with a twist

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    srand(static_cast<unsigned int>(time(0)));

    int secretNumberComputer = rand() % 100 + 1;
    int secretNumberPlayer;
    int triesPlayer = 0;
    int triesComputer = 0;
    int guessPlayer;
    int guessComputer;
    int tooHighPlayer;
    int tooLowPlayer;
    int correctPlayer;
    int tooHighComputer;
    int tooLowComputer;
    int correctComputer;
    int selectNumberIncorrect;
    int lowerGuessComputer = 101;
    int higherGuessComputer = 0;


    cout << "Welcome to Guess My Number\n\n";   

    do
    {
        cout << "Enter a guess: ";
        cin >> guessPlayer;
        ++triesPlayer;

        tooHighPlayer = (guessPlayer > secretNumberComputer);
        tooLowPlayer = (guessPlayer < secretNumberComputer);
        correctPlayer = (guessPlayer == secretNumberComputer);

        if (tooHighPlayer)
        {
            cout << "Too high!\n\n";
        }
        else if (tooLowPlayer)
        {
            cout << "Too low!\n\n";
        }
        else if (correctPlayer)
        {
            cout << "\nThat's it! You got it in " << triesPlayer << " guesses!\n\n";
            break;
        }
        else
        {
            cout << "Error, check code!\n\n";
            break;
        }
    } while (!correctPlayer);


    cout << "Now it's time for you to pick a number and then the computer will guess.\nEnter a number between 1 and 100: ";


    do
    {
        cin >> secretNumberPlayer;
        selectNumberIncorrect = (secretNumberPlayer > 100 || secretNumberPlayer < 1);
        if (selectNumberIncorrect)
        {
            cout << "\nHey, that isn't a number between 1 and 100! Please pick a number that is: ";
        }
        else
        {
            break;
        }
    } while (selectNumberIncorrect);

    guessComputer = (rand() < lowerGuessComputer && rand() > higherGuessComputer);
    cout << "\n\nNow the computer is going to try to guess your number:" << endl;
    cout << "Computer, take a guess: " << guessComputer << endl;
    ++triesComputer;

    tooHighComputer = (guessComputer > secretNumberPlayer);
    tooLowComputer = (guessComputer < secretNumberPlayer);
    correctComputer = (guessComputer == secretNumberPlayer);
    lowerGuessComputer = (rand() % 100 + 1 && rand() < guessComputer);
    higherGuessComputer = (rand() % 100 + 1 && rand() > guessComputer);

    if (tooHighComputer)
    {
        cout << "Too High!\n\n";
        guessComputer = lowerGuessComputer;
    }
    else if (tooLowComputer)
    {
        cout << "Too Low!\n\n";
        guessComputer = higherGuessComputer;
    }
    else if (correctComputer)
    {
        cout << "\nThat's it! You got it in " << triesComputer << " guesses!\n\n";
    }
    else
    {
        cout << "Error, check code!\n\n";
    }


    do
    {
        cout << "Computer, take a guess: " << guessComputer << endl;
        ++triesComputer;


        if (tooHighComputer)
        {
            cout << "Too High!\n\n";
            guessComputer = lowerGuessComputer;
        }
        else if (tooLowComputer)
        {
            cout << "Too Low!\n\n";
            guessComputer = higherGuessComputer;
        }
        else if (correctComputer)
        {
            cout << "\nThat's it! You got it in " << triesComputer << " guesses!\n\n";
            break;
        }
        else
        {
            cout << "Error, check code!\n\n";
            break;
        }
    } while (!correctComputer);


    if (triesComputer < triesPlayer)
    {
        cout << "You lost against the computer!\n\n";
    }
    else if (triesComputer > triesPlayer)
    {
        cout << "You won!\n\n";
    }
    else
    {
        cout << "It's a tie!\n\n";
    }

    cout << "Thank you for playing! Goodbye!" << endl;

    return 0;
}
4

4 回答 4

2

在此块中,您不会检查计算机的猜测是否正确(分配正确的计算机),因此循环将永远持续下去,除非它第一次猜测正确。

do
{
    cout << "Computer, take a guess: " << guessComputer << endl;
    ++triesComputer;


    if (tooHighComputer)
    {
        cout << "Too High!\n\n";
        guessComputer = lowerGuessComputer;
    }
    else if (tooLowComputer)
    {
        cout << "Too Low!\n\n";
        guessComputer = higherGuessComputer;
    }
    else if (correctComputer)
    {
        cout << "\nThat's it! You got it in " << triesComputer << " guesses!\n\n";
        break;
    }
    else
    {
        cout << "Error, check code!\n\n";
        break;
    }
} while (!correctComputer);
于 2013-01-09T22:21:00.823 回答
1

您的第二个do循环永远不会重新计算计算机的猜测。

即你让计算机在循环之前猜测一个数字然后在do循环中你不断检查那个猜测太高还是太低,从不重新计算它的值。它显然永远不会结束。

您需要在第二个循环内进行计算机的猜测计算。

编辑

另外,这个逻辑是不正确的:

lowerGuessComputer = (rand() % 100 + 1 && rand() < guessComputer);
higherGuessComputer = (rand() % 100 + 1 && rand() > guessComputer);

猜测将始终为 0 或 1,因为右侧运算的结果是布尔值。事实上,我不知道你想在那里做什么。您在&&整数和布尔值之间执行。我也不明白你为什么要计算两种不同的猜测——你应该在给定的较高/较低参数范围内计算一个数字。

于 2013-01-09T22:18:28.000 回答
1

除了 Kevin Tran 写的内容,请检查 cin 的有效输入类型。

想象一下有人输入字符而不是整数。

所以

cin >> guessPlayer;

可以写成

if (cin >> guessPlayer) {
        // Do you logic here
    }
else {
 cout<<"Enter numbers only. :)";endl;
 continue;
}

希望这可以帮助。

于 2013-01-09T23:17:33.603 回答
0

与其分析您发布的有许多缺陷的代码,不如考虑一下您的程序必须做什么:用户将选择一个随机数,计算机将尝试猜测该数字。

所以,你的程序流程应该是这样的:

计算机选择一个随机数。它打印出来并要求用户选择数字是否太高、太低或正确。(即要求用户输入“1”如果太高,“2”如果太低,或者“3”如果正确)。

如果用户键入“3”,那么显然你已经完成了。

如果它太高,计算机会选择一个新的随机数(小于它最后的猜测)并再次尝试上述逻辑。

如果它太低,计算机会选择一个新的随机数(大于它最后的猜测)并再次尝试上述逻辑。

现在让我们尝试实现一些实现上述内容的代码:

using namespace std;

int main()
{
    int range_low = 0;    // The number the user picked is greater than this
    int range_high = 100; // The number the user picked is smaller than this

    srand(static_cast<unsigned int>(time(0)));

    do
    {
        // We want to generate a random number between range_low and range_high. We do this
        // by generating a random number between zero and the difference of "low" and "high"
        // adding it to low and adding one more.           
        int guess = range_low + ((rand() % (range_high - range_low)) + 1);

        cout << "I'm guessing your number is " << guess << "... how did I do?" << endl
             << "    [1: too high, 2: too low, 3: you got it!] ";

        // Now let's see how we did...

        int choice;

        cin >> choice;

        if(choice == 3)
        {
            cout << "Be amazed at my psychic powers! For I am a computer!" << endl;
            break;
        }

        if(choice == 2)
        { 
            cout << "Hmm, ok. I was sure I had it. Let's try again!" << endl;
            range_low = guess;
        }

        if(choice == 1)
        {
            cout << "Really? Ok, ok, one more try!" << endl;
            range_high = guess;
        }
    } while(true);

    return 0;
}

这里有两个练习可以帮助您改善上述情况:

首先,尝试将这段代码的逻辑与您的代码的逻辑进行比较,看看您的代码有何不同——尝试理解为什么它是错误的。尝试使用笔和纸执行程序会有所帮助,就像您是一台理解 C++ 的计算机一样。

其次,尝试添加代码以确保计算机永远不会将相同的数字猜两次。

于 2013-01-09T22:58:51.730 回答