好的,所以我在这里遇到了一个重大问题,在花了两天时间之后,我无法找到它为什么不能正常工作。
第一个问题:我有一个功能,通过输入y或n来查看玩家是否希望再玩一次。如果他们按n,它会按原样终止,但是如果他们按Y或y,它只是再次询问他们是否想要再次播放,直到推出除y之外的任何其他字符。
我的第二个问题,也是最烦人的,是我用来确定他们输入的字符是否被使用的循环计数器(刽子手游戏)。它之前有效,在我做了一些小改动后,它不再按预期工作了,而它的根本就是我根本没有弄乱那些代码,它在其他地方。本质上,每次循环遇到单词中的用户猜测时,循环计数器都会重置为0,但如果计数器等于单词的长度,则表示未找到用户猜测,并且应将显示值设置为加一。但是,它现在所做的一切都是一直保持为零,即使用户输入了不正确的猜测,这应该将显示值增加1,它仍保持为零。我花了两天的时间试图让这部分工作起来,而且它昨天工作了,但不是今天,我可以用另一套眼睛来看看我是否忽略了什么!
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;
}
答案 0 :(得分:2)
实际上有两个问题:
count
?不知道。为什么您认为count
始终保持0
?但是,似乎条件count == (wordLength - 1)
不太可能变为true
,因为当wordLength
恰好为空时secretWord
设置为secretWord
的大小(即wordLength
设置为0
),之后从未更改过。答案 1 :(得分:1)
要求再次播放可能是因为没有重置变量“赢家”。如下操作可能会纠正它。
do
{
winner = false; //set to false at each iteration
while(usedWords[wordChoice])
{
if(wordChoice == 19)
wordChoice = 0;
else if(counter == 20)