如何保持跟踪字母已经猜到了在猜字游戏为c++
How to keep track of letters already guessed in Hangman game for C++?
这次给我的作业是抄袭课本上的一个猜字游戏,并按照书上的具体说明修改它。我花了很多时间来研究这个问题,并找到了为什么我总是得到相同的错误信息的原因。在我所看到的任何地方,每个试图修改这段代码的人都尝试过使用数组,并且都有和我一样的运气。我目前正在做字符串中的一章,并计划做我的大部分语句,这是由字符串中的指令请求的。
在这段代码中我需要修改的是:
- 记录用户输入的所有字母
- 如果用户输入已经输入的字母,则向用户发送错误消息
抛出'std::out of range'实例后调用终止(): basic_string: substr
#include <iostream>
#include <string>
using namespace std;
int main()
{
//declare variables
string origWord = " ";
string letter = " ";
char dashReplaced = 'N';
char gameOver = 'N';
int numIncorrect = 10;
string displayWord = "-----";
string usedLetter = " ";
string letterNotGuessedYet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int letterPlace = 0;
//get original word
do //begin loop
{
cout << "Enter a 5-letter word in uppercase: ";
getline(cin,origWord);
}while(origWord.length() != 5); //end do loop
//clear the screen
system("cls");
//start guessing
cout <<"Guess this word: " << displayWord << endl;
while(gameOver =='N')
{
cout << "Enter an uppercase letter: ";
cin >> letter;
//This provides the letterPlace value with the possition of the
//input letter by the user within letterNotGuessedYet
letterPlace = letterNotGuessedYet.find(letter,0);
//This statement determines if the letter input by the user is
//still in the letterNotGuessYet string.
if(letterNotGuessedYet.substr(letterPlace,1)== letter)
{
letterNotGuessedYet.replace(letterPlace, 1, "*");
//cout << endl << letterNotGuessedYet; //This tests that a letter is being replaced with an *
}
else
{
cout << "The letter " << letter << " has already been used. Choose another letter." << endl << endl;
}// end if
//search for the letter in the original word
for(int x = 0; x < 5; x++)
{
//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'
{
//minus 1 to the number of incorrect gueses left
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
我相信我收到这个错误的原因与下面的代码有关:
//This provides the letterPlace value with the possition of the
//input letter by the user within letterNotGuessedYet
letterPlace = letterNotGuessedYet.find(letter,0);
//This statement determines if the letter input by the user is
//still in the letterNotGuessYet string.
if(letterNotGuessedYet.substr(letterPlace,1)== letter)
{
letterNotGuessedYet.replace(letterPlace, 1, "*");
//cout << endl << letterNotGuessedYet; //This tests that a letter is being replaced with an *
}
else
{
cout << "The letter " << letter << " has already been used. Choose another letter." << endl << endl;
}// end if
我很感激你给我带来的任何帮助。
您得到错误的原因是因为您在测试之前使用了"letterPlace"值,以查看是否确实找到了它。如果"string::find"没有找到任何东西,它将返回一个值"string::npos"。在尝试使用这个值之前,您需要测试letterPlace变量。
letterPlace = letterNotGuessedYet.find(letter,0);
// Check to see if this letter is still in the "letterNotGuessedYet string
if(letterPlace != string::npos)
{
// At this point the letter is in the letterNotGuessedYet string so let it go
letterNotGuessedYet.replace(letterPlace, 1, "*");
}
else
{
// The letter was not found which means it has already been guessed.
// Show error to the user here
cout << "The letter " << letter << " has already been used. Choose another letter." << endl << endl;
}
相关文章:
- 在C++游戏中与库存系统作斗争
- 换位表导致测试失败(但在游戏中运行良好)
- Craps游戏问题,忽略if语句
- 矢量下标超出SFML游戏中的范围
- 我是 c++ 的新手.学习基本知识后,我想做井字游戏.对于印刷板,我在下面写代码,但它显示错误
- 我在贪吃蛇游戏中收到了错误代码 -1073741571
- 我应该在简单的策略游戏中为各个派系使用类吗 - C++
- 游戏体验和升级
- 我的代码运行良好,但在游戏循环中中断
- 如何使用 SFML 在贪吃蛇游戏中定义游戏结束?
- 我可以使用任何好的逻辑来阻止计算机将 O 放在井字游戏中的现有 X 上
- 将鼠标和键盘输入发送到 unity3d 游戏 (Rust)
- 在猜谜游戏 c++ 中不计算尝试次数
- 在C++不适用于猜数字游戏的情况下再次播放选项
- 在为我的基于文本的 RPG 游戏制作库存时遇到困难
- 在定义字符数组(井字游戏)的 for 循环中应用输入限制
- Frank Luna 在他的书"使用 DirectX12 进行 3D 游戏编程"的介绍中盒子示例的问题
- 如何在Linux上正确发布C++软件(游戏)
- 提高基于组件的游戏引擎的效率
- 文本冒险游戏 - 如何区分一种项目类型与另一种项目类型以及如何构建项目类/子类