1
我正在創建一個hang子手遊戲。我有一個問題,要求用戶重新輸入一個新的猜測(char),如果他們已經輸入了同一個(這些包含在字母[]數組中)。我已經開始了一個while循環,要求用戶重新輸入,如果「猜測」匹配存儲在數組中的一個字符。謝謝。Hang子手遊戲 - 同時循環條件「包含在陣列中」
void play(string word)
{
string copy = word;
for(int i = 0; i < word.length(); i++)
{
copy[i] = ' ';
}
bool match = false;
bool valid = false;
int chance = 5;
char letters[26];
int counter = 0;
char guess;
while (chance > 0 && match == false)
{
int blanks = 0;
for (int i = 0; i < word.length(); i++)
{
if(copy[i] == ' ')
blanks++;
}
cout << '|';
for(int i = 0; i < word.length(); i++)
{
cout << copy[i] << '|';
}
cout << "\t(There are '" << blanks << "' blanks)" << endl;
cout << "Incorrect letters guessed: ";
for (int i = 0; i<counter; i++)
{
cout << "'" << letters[i] << "' ";
}
cout << "\n\nEnter guess: ";
cin >> guess;
for(int i = 0; i < counter; i++)
{
if(guess == letters[i])
valid = true;
}
while (valid == true)
{
for(int i = 0; i < counter; i++)
{
if(guess == letters[i])
valid = true;
else
valid = false;
}
if (valid)
{
cout << "\n\nAlready tried that one. Enter guess: ";
cin >> guess;
}
}
long find = word.find(guess);
if (find != string::npos)
{
copy[find] = word[find];
for (int i = 0; i < word.length(); i++)
{
if (word[i] == word[find])
{
copy[i] = word[find];
}
}
}
else
{
chance--;
letters[counter] = guess;
counter++;
printMan(chance);
}
if (copy == word)
{
match = true;
}
}
if (match == true)
cout << "\nYou saved a life! You managed to win! Congrats, cheater." << endl;
else
cout << "\nThe man has been hung! You LOSE! The word was '" << word << "', you idiot." << endl;
}