这两天我一直在尝试修复这个程序,事实证明它很麻烦。这是我的 C++ 入门类(class)的作业,给我带来了麻烦。我搜索了这个板,在 Cplusplus.com 上发帖,并在 Google 上花了几个小时,寻求一些帮助。
这是我的问题。我得到了一个程序,需要为其添加一些功能:
- 我必须保存用户条目。
- 如果用户输入相同的条目两次,我必须显示一条错误消息。
看起来很简单?不适合像我这样的初学者。这是代码,我试图添加一些内容以满足问题的要求。
int main()
{
//declare variables
string origWord = "";
string letter = "";
char dashReplaced = 'N';
char gameOver = 'N';
int numIncorrect = 0;
string displayWord = "-----";
string letterGuess[26];
//get original word
do //begin loop
{
cout << "Enter a 5-letter word in uppercase: ";
getline(cin, origWord);
} while (origWord.length() != 5);
//clear the screen
system("cls");
//start guessing
cout << "Guess this word: " <<
displayWord << endl;
while (gameOver == 'N')
{
cout << "Enter an uppercase letter: ";
cin >> letter;
//Entry Storage and Error Message. This is my problem.
for (int x = 0; x < 26; x++)
{
letterGuess[x] = letter;
for (int i = x; i < 26; i++)
{
if (i != x)
{
if (letterGuess[x] == letterGuess[i])
{
cout << "Letter already entered. Choose another letter."
<< endl;
}
}
}
}
//search for the letter in the original word
for (int x = 0; x < 5; x += 1)
{
//if the current character matches
//the letter, replace the corresponding
//dash in the displayWord variable and then
//set the dashReplaced variable to 'Y'
if (origWord.substr(x, 1) == letter)
{
displayWord.replace(x, 1, letter);
dashReplaced = 'Y';
} //end if
} //end for
//if a dash was replaced, check whether the
//displayWord variable contains any dashes
if (dashReplaced == 'Y')
{
//if the displayWord variable does not
//contain any dashes, the game is over
if (displayWord.find("-", 0) == -1)
{
gameOver = 'Y';
cout << endl << "Yes, the word is "
<< origWord << endl;
cout << "Great guessing!" << endl;
}
else //otherwise, continue guessing
{
cout << endl << "Guess this word: "
<< displayWord << endl;
dashReplaced = 'N';
} //end if
}
else //processed when dashReplaced contains 'N'
{
//add 1 to the number of incorrect guesses
numIncorrect += 1;
//if the number of incorrect guesses is 10,
//the game is over
if (numIncorrect == 10)
{
gameOver = 'Y';
cout << endl << "Sorry, the word is "
<< origWord << endl;
} //end if
} //end if
} //end while
system("pause");
return 0;
} //end of main function
我对程序的唯一编辑直接在条目存储和错误消息的标题下。我尝试了一个 for 循环,但它只是显示了输入的每个字母的错误消息。不仅如此,它还显示了 26 次。添加一个 Break 命令修复了这个问题,它只显示一次。但是,它仍然显示在每个条目上。
Cplusplus 的一位成员指出,我错误地针对同一位置的数组测试了同一变量。这就是为什么它在每个条目上都显示错误。现在有了这个循环,错误只会在一个条目被输入两次时显示。但是,错误消息再次显示全部 26 次。最重要的是,只有一个接一个地输入字母才会出错。
例如,如果我输入 A 然后 X 然后再输入 A,则不会显示任何错误。如果我输入 A,然后再输入 A,错误将显示 26 次。字母变量在导致错误消息显示多次的原因之上输入数组的方式显然有问题。
如有任何帮助,我们将不胜感激。
编辑:我的教授已经回复我并建议使用以下而不是我一直在修补的东西:
for (int x=0; x<5; x++)
if (origWord[x] == letterEntered)
origWord[x] = '-';
这只是我的问题,还是完全没有达到目标?我没有尝试将其转换为我的程序,因为简单的复制和粘贴作业会产生编译错误。但是,我看不出这对我正在尝试做的事情有何影响。
最佳答案
此集合将您的 letterGuess
数组的所有条目设置为最近猜到的字母。
letterGuess[x] = letter;
这不是您想要的。
你需要考虑你需要实现的实际算法:
- 用户输入一个猜测
- 检查他们是否已经猜出那封信
- 如果有,显示错误信息,返回1。
- 如果他们没有,则保存该猜测,继续游戏逻辑。
如果您已经了解了标准容器,这可以通过 std::set
或已排序的 std::vector
轻松完成。
关于c++ - 如何跟踪在 C++ 的 Hangman 游戏中猜出的字母?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20207682/