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