これを正しくフォーマットしたことを本当に願っています。私はこの推測ゲームに取り組んできましたが、かなりうまく機能します。私が持っている唯一の問題はgameSummary
機能にあります。努力を合計する代わりに (たとえば、3 ラウンドのプレイ、1 ラウンドで最大 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
1 つのゲームのみがカウントされたため、これも平均を台無しにします。使用する必要があると感じてい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;
}