わかりましたので、ここで大きな問題が発生しています。これに 2 日間費やした後、思うように機能しない理由がわかりません。
最初の問題: プレイヤーが ay または n を入力して、もう一度プレイしたいかどうかを確認する機能があります。n を押すと、正常に終了しますが、Y または y を押すと、y 以外の他の文字が押されるまで、もう一度再生するかどうかを尋ねるだけです。
私の 2 番目の問題であり、最も厄介なのは、推測として入力した文字が単語で使用されたかどうかを判断するために使用しているループ カウンターです (ハングマン ゲーム)。以前は機能していましたが、いくつかのマイナーな変更を加えた後、意図したとおりに機能しなくなりました。基本的に、ループ カウンターは単語内でユーザーの推測に遭遇するたびに 0 にリセットされますが、カウンターが単語の長さと等しい場合は、ユーザーの推測が見つからなかったことを意味し、表示値を次のように設定する必要があります。一を足す。ただし、現在は常にゼロのままであり、ユーザーが誤った推測を入力した場合でも、表示値が 1 増加するはずですが、ゼロのままです。この部分だけを機能させるために 2 日間費やしましたが、昨日は機能していましたが、今日は機能しませんでした。
void playHangman(string wordArray[], bool usedWords[])
{
string secretWord;
unsigned seed = time(0);
srand(seed);
int wordChoice = (rand()%20);
int counter = 0;
int display = 0;
bool winner = false;
int wordLength = secretWord.length();
int count;
char again;
do
{
while(usedWords[wordChoice])
{
if(wordChoice == 19)
wordChoice = 0;
else if(counter == 20)
{
for(int i = 0; i < SIZE; i++)
usedWords[i] = false;
wordChoice = (rand()%20);
counter = 0;
}
wordChoice++;
counter++;
}
secretWord = wordArray[wordChoice];
const char *word = new char [secretWord.length()];
word = secretWord.c_str();
char *userPrompt = new char [secretWord.length()];
for(int i = 0; i < secretWord.length(); i++)
userPrompt[i] = '_';
userPrompt[secretWord.length()] = '\0';
char userGuess = '\n';
while(!winner)
{
count = 0;
for(int i = 0; i < secretWord.length(); i++)
cout << userPrompt[i] << " ";
cout << "\n" << endl;
displayGallows(display);
if(display == 6)
{
cout << "Sorry, you lost!" << endl;
break;
}
cout << "Enter a letter: ";
cin >> userGuess;
cin.ignore();
for(int i = 0; i < secretWord.length(); i++)
{
if(word[i] == userGuess)
{
userPrompt[i] = userGuess;
count = 0;
}
else if(count == (wordLength - 1))
display++;
count++;
}
winner = checkWin(word, userPrompt, display, secretWord);
}
again = playAgain();
}while(again == 'Y' || again =='y');
}
char playAgain()
{
char playAgain;
cout << "Would you like to play again? Enter y or n: ";
cin >> playAgain;
return playAgain;
}