0

我真的希望我的格式正确。我一直在做这个猜谜游戏,效果很好。我唯一的问题是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;
}
4

1 回答 1

0

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

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)

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

于 2013-09-26T06:32:49.920 回答