在Hang子手游戏中检查任意大小的字母
问题描述:
当前我正在研究一个hang子手游戏,我之前编码它只能用于5个字母的单词,但现在想要处理任何长度的单词,我怎么能改变这个代码,使其工作如何我想要它?在Hang子手游戏中检查任意大小的字母
#include "stdafx.h"
#include <iostream>
#include <string>
#include <stdlib.h>
#include <cstdlib>
using namespace std;
int main()
{
string word;
int tries;
string guess;
string wordguess;
string output;
cout << "Enter a word for player two to guess: ";
cin >> word;
system("CLS");
cout.flush();
cout << "Guess the word!" << endl;
for (int i = 0; i < word.length(); i++)
{
cout << "_ ";
}
cout << "Enter a letter: ";
cin >> guess;
for (int tries = 5; tries > 0; tries--)
{
if (guess[0] == word[0]) {
output[0] = word[0];
cout << "You guessed the first letter! Good job!" << endl;
}
if (guess[0] == word[1]) {
output[2] = word[1];
cout << "You guessed the second letter! Good job!" << endl;
}
if (guess[0] == word[2]) {
output[4] = word[2];
cout << "You guessed the third letter! Good job!" << endl;
}
if (guess[0] == word[3]) {
output[6] = word[3];
cout << "You guessed the fourth letter! Good job!" << endl;
}
if (guess[0] == word[4]) {
output[8] = word[4];
cout << "You guessed the fifth letter! Good job!" << endl;
}
cout << output << endl;
cout << "You have " << tries << " tries left. Take a guess at the word: " << endl;
cin >> wordguess;
if (wordguess == word)
{
cout << "Congratulations, you guessed the word correctly!" << endl;
break;
}
}
system("pause");
return 0;
}
正如你可以告诉我检查从0到4(第一到第五个字母)的每个位置。我知道有很多方法可以让我更好地编码,但正如你所猜测的,我是编码新手,这是我想到的方式。请注意,这仍然是一项正在进行的工作,因此尚未完全完成。任何帮助将是伟大的!
答
在设计算法时,请想想如何在没有计算机的情况下手动完成此操作。然后让代码执行相同的操作。
如果你检查你的朋友的反对写在沙滩上的字猜测,你可能会去像这样:
- 去通过字符写入图案的性格,在内存 叨念着你的字
- 每个字母,检查它是否等于猜测
-
如果是
- 更换PLACEHOLD呃与它
- 记住你的朋友猜对了。
- 还要注意是否有任何占位符左
- 如果没有,你的朋友赢得
最后,如果你的朋友没有猜对,他们进球点球点并检查它们是否丢失
现在,所有的结果都是将它放在C++中。该语言提供各种实体 - 让我们检查哪些适合我们需要最好的:
- 字和当前的模式 - 一个固定大小的字符串
-
位记忆:
- 目前的猜测是否正确 - 布尔
- 占位符左 - 诠释
- 点球点(或等价地,左尝试) - 诠释
-
部分的算法:
- 遍历字符串 -
for
loop of one of a few kinds- 我们需要相同的索引单词中猜到信在替换模式的字符。所以,we need to have the index when looping。因此,索引变量
for(std::string::size_type i = 0; i < str.size(); ++i)
的风格可能最适合。
- 我们需要相同的索引单词中猜到信在替换模式的字符。所以,we need to have the index when looping。因此,索引变量
- 遍历字符串 -
答
// Example program
#include <iostream>
#include <string>
using namespace std;
class my_game
{
private:
string congrats_array[15] = {"first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "nineth", "tenth", "eleventh", "twelfth", "thirteenth", "fourteenth", "fifteenth"};
string word_to_guess;
int tries_left;
int word_length;
int letters_guessed_count;
string guessed_letters;
void check_letter(char letter);
void print_current_word_state();
public:
my_game();
void begin_the_game();
void play_the_game();
};
my_game::my_game()
{
}
void my_game::begin_the_game()
{
cout << "Enter a word for player to guess: " << endl;
cin >> word_to_guess;
system("CLS");
cout.flush();
cout << "Enter the tries amount!\n" << endl;
cin >> tries_left;
word_length = word_to_guess.size();
guessed_letters = "_";
letters_guessed_count = 0;
for(int i = 0; i < word_length - 1; i++){
guessed_letters += "_";
}
}
void my_game::play_the_game()
{
cout << "Guess the word!" << endl;
char letter;
for(int i = 0; i < tries_left; i++)
{
cout << guessed_letters << endl;
cout << "Enter a letter: " << endl;
cin >> letter;
check_letter(letter);
if(letters_guessed_count == word_length){
cout << "Congrats! You won!" << endl;
return;
}
}
cout << "You lose" << endl;
}
void my_game::check_letter(char letter)
{
for(int i = 0; i < word_length; i++)
{
if(word_to_guess[i] == letter && guessed_letters[i] != letter)
{
guessed_letters[i] = letter;
letters_guessed_count++;
cout << "You guessed the" << congrats_array[i] <<"letter! Good job!" << endl;
}
}
}
int main()
{
my_game game;
game.begin_the_game();
game.play_the_game();
}
答
因此,简而言之,你需要用任意长度的话要做到这一点的是用字符串的.substr()函数和字符串流库的名为.str()和< <和>>运营商。此版本的代码使用一个函数,可以在正确的索引位置插入正确的猜测字符。这将在正确的地方用字母逐渐取代“_________”。在Java中这样做更容易,但stringstream是一个很好的库,我强烈建议熟悉它。我会留下怎样处理推测的字符的多个实例取决于你的问题(即“我”在“书目”)
#include <string>
using std::string;
#include <sstream>
using std::stringstream;
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
string newString(string, int, string);
int main()
{
string word;
string guess;
int tries;
string output;
string input;
cout << "Enter word for player 2 to guess: ";
cin >> word;
stringstream ss;
//---------- fills the stream with "_"s matching the length of word
for(int i = 0; i < word.length(); i++)
ss << "_";
//----------- assigns the initial value of "___..." to output
ss >> output;
//----------- sets up the loop
tries = 5;
bool found = false;
for(int i = 0; i < 5; i++)
{
cout << "\nTry " << i << " of 5: Enter a letter or guess the word: ";
cin >> input;
if(input == word)
{
cout << "Congratulations, you guessed the word correctly!" << endl;
break;
}
//------------------ else, proceed with replacing letters
if(word.find(input) != std::string::npos)
{
size_t position = word.find(input); // finds index of first instance of the guessed letter
cout << "You guessed the " << position+1 << " letter! Good job!" << endl; // since strings start at index 0, position+1
//------- replaces appropriate "_" with the guessed letter
output = newString(input, position, output);
cout << "\n" << output;
// Around here you'll want to set up a way to deal with multiple instances
// of the same letter
}
else
cout << "Incorrect guess" << endl;
}
return 0;
}
//---------------------------------------------------
string newString(string guess, int index, string word)
{
string NewString;
stringstream temp;
//---------- hack up the string into sections before and after the index
string before = word.substr(0, index);
string after = word.substr(index+1, word.length() - index+1);
//---------------- populates the new stringstream and assigns it to the result
temp << before << guess << after;
NewString = temp.str();
return NewString;
}
我认为你正在寻找一个字母'的std :: set'那就是这个词。 –
听起来像它会起作用,所以如果这个词是“玩”,它会认为它是一组p,l,a和y? – zhodges10
'output [x]'是未定义的行为,因为'output'是空的(它不会自动增长字符串)。你可以在'word'中的字符上使用循环,但是你需要一些通用的方法来获得英文单词“first”,“second”等。 – aschepler