0

I am developing a Hangman game for an Uni assessment in C++ and I am having trouble displaying my hidden words after the user types a letter. So I have got the word being displayed as '_ _ _ _ _ _ _' but when I type a letter it doesn't swap the underscore for the actual letter.

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;


} 

}

My whole program is separated into different header files and cpp files. So the code above is from my gameguesses.cpp and the header for that is below:

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

    string completedWord;

};

And this is what I actually get: My code problem

A help would be appreciated. Thank you!

Gui
  • 65
  • 1
  • 14
  • take a look at http://stackoverflow.com/questions/1508490/how-can-i-erase-the-current-line-printed-on-console-in-c-i-am-working-on-a-lin also google "ncurses" is you want to get that fancy – Dmitri Sep 24 '15 at 01:25
  • alternatively, you could just assume a fixed screen size and re-print the whole thing every update (keep a "screen-buffer" array) – Dmitri Sep 24 '15 at 01:26

2 Answers2

1

I see 2 issues:

if (guessedLetter = completedWord[0])

That line needs == not =.

Secondly, you are comparing the guess only to the first letter of the hidden word. You need to write a loop to check each letter and substitute where it matches the guess, not just in element [0].

WDS
  • 966
  • 1
  • 9
  • 17
  • something like this? for (int i = 0; i < completedWord.length(); i++) with an if statement inside if (guessedLetter == completedWord.length()) making the length == guessedLetter but it still only displays ______ – Gui Sep 24 '15 at 01:43
  • if (guessedLetter == hiddenWord[i]) And if the condition is met, then make the displayed letter at position [i] of completedWord equal the guessedLetter – WDS Sep 24 '15 at 01:44
  • still it displays only underscores, eg cheese if I type E it displays ______ :s – Gui Sep 24 '15 at 01:47
  • Note that I made an edit to look for a match in hiddenWord, not in completedWord. As completedWord is all underscores it would never match except with an underscore. Look for any position in hiddenWord you have a match, then make the same position in completedWord equal to that matching character – WDS Sep 24 '15 at 01:50
  • And actually, instead of guessedLetter, use guessedLetter[0] – WDS Sep 24 '15 at 01:51
0

See if this helps.

#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;
}
Ashley
  • 575
  • 5
  • 12