在Hangman游戏中检查任意大小的字母

时间:2017-10-21 01:54:13

标签: c++ algorithm

目前我正在制作一个刽子手游戏,我之前将其编码为仅用于5个字母的单词,但现在想让它处理任意长度的单词,我怎么能改变这个代码使其工作如何我想要它吗?

#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(第一到第五个字母)。我知道有很多方法可以让我更好地编码,但正如你可以猜到的,我是编码的新手,这就是我想到的方式。请注意,这仍然是一项正在进行的工作,因此尚未完全完成。任何帮助都会很棒!

3 个答案:

答案 0 :(得分:2)

在设计算法时,请考虑如何在没有计算机的情况下手动执行此操作。然后让代码执行相同的操作。

如果您正在检查您的朋友对沙子上写的字的猜测,您可能会这样做:

  • 逐个字符地逐字逐句,在内存中发音你的话
  • 对于每个字母,检查它是否等于猜测
  • 如果是

    • 用它替换占位符
    • 记住你的朋友猜对了。
    • 另请注意是否有任何占位符
      • 如果没有,你的朋友获胜
  • 最后,如果你的朋友没有猜对,给他们一个点数并检查他们是否输了

现在,剩下的就是把它放在C ++中。该语言提供各种实体 - 让我们检查哪些实体最适合我们:

  • 单词和当前模式 - 固定大小的字符串
  • 要记住的位:

    • 目前的猜测是否正确 - bool
    • 占位符left - int
    • 罚分(或等同地,试图留下) - int
  • 算法的一部分:

答案 1 :(得分:0)

// 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();
}

答案 2 :(得分:0)

因此,简而言之,您需要使用任意长度的单词来使用字符串的.substr()函数和字符串流库.str()和&lt;&lt;和&gt;&gt;运营商。此版本的代码使用一个函数,在适当的索引位置插入一个正确猜到的字符。这将逐渐用正确位置的字母替换“_________”。这在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;
}