【问题标题】:Having trouble running guessing game summary correctly无法正确运行猜谜游戏摘要
【发布时间】:2013-09-26 03:32:22
【问题描述】:

我真的希望我的格式正确。我一直在研究这个猜谜游戏,效果很好。我唯一的问题是gameSummary 函数。它不会累加努力(例如,玩了 3 轮,一轮最多 15 次猜测,另一轮最多 5 次猜测,无论平均值如何),它会发布每场比赛的结果。

例子:

Total number of rounds: 1
The most number of guesses in one round: 10
The least number of guesses in one round: 0
Average number of guesses per round: -1.#IND
Total number of rounds: 1
The most number of guesses in one round: 5
The least number of guesses in one round: 0
Average number of guesses per round: -1.#IND

这也弄乱了平均值,因为只计算了一场比赛。我感觉需要使用gameSummary(rounds, mostGuesses, leastGuesses, averageGuesses);,但我不知道我应该把它放在哪里,以便它计算总游戏的结果。有什么想法吗?

bool isTrue(int guess, int tries, int number, 
    int rounds, int mostGuesses, int leastGuesses, float averageGuesses)
{
    char answer;
    bool inGame = true; // states that the user is currently in the game
    while (inGame == true)
    {
        if (guess < 1 || guess > 99)
        {
            cout << "Invalid guess." << endl;
            cout << "Please take another guess: ";
            cin >> guess;
        }
        else
        {
            if (guess > number)
            {
                cout << "Lower please: ";
                cin >> guess;
                tries++;
            }
            else if (guess < number)
            {
                cout << "Higher please: ";
                cin >> guess;
                tries++;
            }
            else if (guess == number)
            {
                cout << "Congratulations! " << guess << " is the number!!\n";
                cout << "You guessed correctly in " << tries << " tries!!\n";
                inGame = false; // once the game is won, the while loop breaks.
                rounds++;
            }
            if (tries > mostGuesses)
            {
                mostGuesses = tries;
            }
            else if (tries < mostGuesses)
            {
                leastGuesses = tries;
            }
        }
    }
    cout << "do you want to play another round? ";
    cin >> answer;
    if (answer == 'Y' || answer == 'y')
    {
        game(); // replays the game if the user wants.
    }
    gameSummary(rounds, mostGuesses, leastGuesses, averageGuesses);
    return false;
}

void gameSummary(
    int rounds, int mostGuesses, int leastGuesses, float averageGuesses)
{
    cout << "Total number of rounds: " 
        << rounds << endl;
    cout << "The most number of guesses in one round: " 
        << mostGuesses << endl;
    cout << "The least number of guesses in one round: " 
        << leastGuesses << endl;
    cout << "Average number of guesses per round: " 
        << averageGuesses << endl;
}

【问题讨论】:

  • 如果将每一轮的tries 的数量计算为totalTries,那么平均值将为totalTries/rounds。我希望你能用这个最小的提示完成你的作业。 (顺便说一句,您的代码中还有其他错误,但仅向您提供完整的解决方案并不能帮助您学习。)
  • 我对代码做了一些更改,基本上是totalTries的建议,但我不明白为什么,如果我玩两个游戏,它会运行totalTries两次。甚至断点都无法帮助我找出原因。

标签: c++


【解决方案1】:

您的函数似乎在调用自己:

if (answer == 'Y' || answer == 'y')
{
    game(); // replays the game if the user wants.
}
gameSummary(rounds, mostGuesses, leastGuesses, averageGuesses);

我猜“game()”调用“isTrue()”。所以发生的情况是,当你回答“Y”时,“游戏”被调用并播放,总结第二个游戏,然后返回第一次调用 isTrue,然后调用 gameSummary()。

在 ideone 上查看 sscce 现场演示:http://ideone.com/oP5opZ

#include <string>
#include <iostream>

void game(const std::string& from);

class PathTracker {
    std::string m_path;

public:
    PathTracker(const std::string& path_, const char* func_)
        : m_path(path_)
        {
            m_path += ">";
            m_path += func_;
            std::cout << m_path << " +enter+" << std::endl;
        }

    const std::string path() const { return m_path; }

    ~PathTracker() { std::cout << m_path << " -return-" << std::endl; }
};

void gameSummary(const std::string& from)
{
    PathTracker p(from, "gameSummary");
    std::cout << "[game summary]" << std::endl;
}

void isTrue(const std::string& from)
{
    PathTracker p(from, "isTrue");
    std::cout << "Another game? ";
    char answer;
    std::cin >> answer;
    std::cout << "Got " << answer << std::endl;
    if (answer == 'Y' || answer == 'y') {
        game(p.path());
    }
    gameSummary(p.path());
}

void game(const std::string& from)
{
    PathTracker p(from, "game");
    std::cout << "[get user input]" << std::endl;
    isTrue(p.path());
}

int main(int argc, const char* argv[])
{
    game("main");
}

我给它输入了“Y”和“N”,输出是这样的:

main>game +enter+
[get user input]
main>game>isTrue +enter+
Another game? Got Y
main>game>isTrue>game +enter+
[get user input]
main>game>isTrue>game>isTrue +enter+
Another game? Got N
main>game>isTrue>game>isTrue>gameSummary +enter+
[game summary]
main>game>isTrue>game>isTrue>gameSummary -return-
main>game>isTrue>game>isTrue -return-
main>game>isTrue>game -return-
main>game>isTrue>gameSummary +enter+
[game summary]
main>game>isTrue>gameSummary -return-
main>game>isTrue -return-
main>game -return-

当您响应“Y”时,程序会绕道进入对“game”的 INNER 调用,该调用通过执行,调用 gameSummary,然后返回到代码之前的位置,即在对“gameSummary”的原始调用之前即将发生。

递归的原理可以用这个更简单地演示:http://ideone.com/sFk468

#include <iostream>

void foo(int i)
{
    if (i == 0) {
        foo(1);
    }
    std::cout << "foo(" << i << ")" << std::endl;
}

int main(int argc, const char* argv[])
{
    foo(0);
}

哪些输出

foo(1)
foo(0)

您的代码重复了相同的基本模式。

【讨论】:

    猜你喜欢
    • 2018-02-07
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    • 2020-11-22
    • 2014-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多