我真的希望我格式化了这一点。我一直在研究这个猜谜游戏,效果很好。我唯一的问题是使用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;
}
答案 0 :(得分: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”时,程序会绕过对“游戏”的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)
您的代码重复相同的基本模式。