for
このコードの終わり近くのループに問題があります。
ループは絞首刑執行人ゲーム用であり、本質的に、ループは秘密の単語を使用して配列を循環しchar
、ユーザーの推測を配列内の各要素と比較しますchar
。見つかった場合は、出力を更新して文字を表示し、カウンターを 0 にリセットします。または、カウンターが秘密の単語の長さと等しい場合は、ループが見つからなかっchar
たことを意味し、両方とも表示に 1 を追加する必要があります。 (ハングマン絞首台の場合)間違った推測を間違った推測配列に配置します。
ここで私が抱えている問題は、ほとんどの場合、ループは意図したとおりに機能しますが、正しい答えを正しいものとして読み取り、間違った推測配列内に配置し、絞首台の表示値をインクリメントすることがあります。
これは一見ランダムに行われ、2 番目のゲームをプレイするときにのみ行われます。カウンターの問題だと思ったのですが、ループの開始時に0にリセットし、正しい答えが見つかった場合はループwhile
でゼロにリセットします。for
そのため、その時点でwrongChoice
のカウンターはwordLength - 1
. コードは次のとおりです。
while(!winner)
{
count = 0;
for(int i = 0; i < secretWord.length(); i++)
cout << userPrompt[i] << " ";
cout << "\n" << endl;
cout << "Wrong Guess: ";
for(int i = 0; i < (secretWord.length() + 6); i++)
{
cout << userChoice[i] << " ";
}
cout << "\n" << endl;
displayGallows(display);
if(display == 6)
{
cout << "Sorry, you lost!" << endl;
break;
}
cout << "Enter a letter: ";
cin >> userGuess;
while(!cin)
{
cin.ignore();
cin.clear();
cin.sync();
cout << "Error reading input character! Try Again!" << endl;
cout << "Enter a letter: ";
cin >> userGuess;
}
guessCount++;
cin.ignore();
for(int i = 0; i < secretWord.length(); i++)
{
if(word[i] == tolower(userGuess))
{
userPrompt[i] = tolower(userGuess);
count = 0;
}
else if(count == (wordLength - 1))
{
display++;
userChoice[guessCount - 1] = toupper(userGuess);
}
count++;
}
winner = checkWin(word, userPrompt, display, secretWord);
}
again = playAgain();