实际字符的C ++ Hangman交换下划线

时间:2015-09-24 01:23:08

标签: c++ string char

我正在开发一个用C ++进行Uni评估的Hangman游戏,在用户输入字母后我无法显示隐藏的单词。所以我把这个词显示为' _ _ _ _ _ _ _'但是当我输入一封信时,它不会将下划线换成实际的字母。

game::game() {

words[0] = "strongly";
words[1] = "cheese";
words[2] = "computer";
words[3] = "coffee";
words[4] = "potatoes";         //words that can be in the game
words[5] = "zebra";
words[6] = "extinguisher";
words[7] = "solution";
words[8] = "diligent";
words[9] = "flabbergasted";

numGuesses = 0;

hiddenWord = words[rand() % 10]; //pick a random word from array words

completedWord = hiddenWord;

//for loop for changing the word to underscores
for (int i = 0; i < completedWord.length(); i++) {
    completedWord[i] = '_';

}   

//for loop adding a space after underscore
for (int i = 0; i < completedWord.length(); i++) {
    cout << completedWord[i] << " ";


}

cout << endl;
cout << "Please enter a letter: ";


char guessedLetter;
cin >> guessedLetter;

if (guessedLetter = completedWord[0]) {
    completedWord = guessedLetter;
    //cout << guessedLetter << endl;
    cout << completedWord << endl;


} 

}

我的整个程序被分成不同的头文件和cpp文件。所以上面的代码来自我的gameguesses.cpp,其标题如下:

    class game {
public:
    string words[10];
    game();
    string hiddenWord;
    int numGuesses;

    string completedWord;

};

这就是我实际得到的: My code problem

帮助将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:1)

我看到2个问题:

if (guessedLetter = completedWord[0])

该行需要== not =。

其次,您只是将猜测与隐藏单词的第一个字母进行比较。您需要编写一个循环来检查每个字母并替换它与猜测匹配的位置,而不仅仅是在元素[0]中。

答案 1 :(得分:0)

看看这是否有帮助。

#include <iostream>
#include <vector>
#include <string>
#include <stdlib.h>
#include <locale>  
using namespace std;

class Game
{
public:

    Game()
    {
        gameDictionary.push_back("strongly");
        gameDictionary.push_back("cheese");
        gameDictionary.push_back("computer");
        gameDictionary.push_back("coffee");
        gameDictionary.push_back("potatoes");
        gameDictionary.push_back("zebra");
        gameDictionary.push_back("extinguisher");
        gameDictionary.push_back("solution");
        gameDictionary.push_back("diligent");
        gameDictionary.push_back("flabbergasted");
    }

    // Play until winning or losing. Returns true on win, false on loss.
    bool playGame()
    {
        numGoodGuesses = 0;
        numBadGuesses = 0;
        numWordLettersFound = 0;
        hiddenWord = gameDictionary[rand() % gameDictionary.size()]; //pick a random word from array words
        guessedWordTracker = hiddenWord;
        for (string::size_type i = 0; i < hiddenWord.size(); i++)
        {
            completedWord += "_ ";
        }

        for (;;)
        {
            cout << completedWord << endl;
            cout << "Please enter a letter: ";
            char guessedLetter;
            do
            {
                cin >> guessedLetter;
                guessedLetter = tolower(guessedLetter);
                if (!isalpha(guessedLetter))
                {
                    cout << "Invalid letter, try again." << endl;
                }
            } while (!isalpha(guessedLetter));

            string::size_type pos;
            int numMatchesFoundThisTime = 0;
            while ((pos = guessedWordTracker.find_first_of(guessedLetter)) != string::npos)
            {
                completedWord[pos * 2] = guessedWordTracker[pos];
                guessedWordTracker[pos] = '\x01';
                numMatchesFoundThisTime++;
            }

            numWordLettersFound += numMatchesFoundThisTime;

            if (numMatchesFoundThisTime > 0)
            {
                numGoodGuesses++;
                cout << "Wow, you found " << numMatchesFoundThisTime 
                     << (numMatchesFoundThisTime > 1 ? " letters!" : " letter!") 
                     << endl;

                if (numWordLettersFound == hiddenWord.size())
                {
                    cout << "Congrats... the word is '" << hiddenWord << "' ... great job!" << endl;
                    return true;
                }
            }
            else
            {
                numBadGuesses++;
                cout << "Sorry, the letter '" << guessedLetter << "' is not in the word." << endl;
            }

            int totalGuesses = numGoodGuesses + numBadGuesses;
            cout << "You've made " << numGoodGuesses << " good guesses, "
                << numBadGuesses << " bad guesses, "
                << totalGuesses << " total guesses." << endl;

            // Example failure:
            const int BAD_GUESSES_ALLOWED = 30;
            if (numBadGuesses > BAD_GUESSES_ALLOWED)
            {
                cout << "Sorry, you have no more guesses left. You lose." << endl;
                return false;
            }
        }
    }

private:
    vector<string> gameDictionary;
    string hiddenWord;
    string guessedWordTracker;
    string completedWord;
    int numGoodGuesses;
    int numBadGuesses;
    int numWordLettersFound;
};

int _tmain(int argc, _TCHAR* argv[])
{
    Game g;
    g.playGame();
    return 0;
}