0

Ok so I am having a major issue here, and after spending two days on this, I can not find out why it is not working as I think it should.

First problem: I have a function to see if the player wished to play again by inputting a y or n. If they press n, it terminates as it should, but if they press Y or y it just asks them again if they want to play again, until any other char other than y is pushed that is.

My second issue, and the most annoying, is the loop counter I am using to determine if the char they entered as a guess was used or not in the word (hangman game). It worked before, after I made some minor changes, it no longer works as intended, and the butt of it is that I didn't mess with that code at all, it was elsewhere. Essentially, the loop counter is reset to 0 every time the loop encounters the user guess within the word, but if the counter equals the length of the word, that means that the user guess was not found, and it should set the display value to plus one. However, all it does NOW is stay at zero all the time, even if the user enters an incorrect guess, which should increment the display value by one, it remains at zero. I have spent two days trying to get JUST this part to work, and it WAS working yesterday, but not today, and I can use another set of eyes on it to see if I overlooked something!

void playHangman(string wordArray[], bool usedWords[])
{
    string secretWord;
    unsigned seed = time(0);
    srand(seed);
    int wordChoice = (rand()%20);
    int counter = 0;
    int display = 0;
    bool winner = false;
    int wordLength = secretWord.length();
    int count;
    char again;

    do
    {
        while(usedWords[wordChoice])
        {
            if(wordChoice == 19)
                wordChoice = 0;
            else if(counter == 20)
            {
                for(int i = 0; i < SIZE; i++)
                    usedWords[i] = false;
                wordChoice = (rand()%20);
                counter = 0;
            }
            wordChoice++;
            counter++;
        }
        secretWord = wordArray[wordChoice];
        const char *word = new char [secretWord.length()];
        word = secretWord.c_str();
        char *userPrompt = new char [secretWord.length()];
        for(int i = 0; i < secretWord.length(); i++)
            userPrompt[i] = '_';
        userPrompt[secretWord.length()] = '\0';
        char userGuess = '\n';

        while(!winner)
        {
            count = 0;
            for(int i = 0; i < secretWord.length(); i++)
                cout << userPrompt[i] << " ";
            cout << "\n" << endl;
            displayGallows(display);

            if(display == 6)
            {
                cout << "Sorry, you lost!" << endl;
                break;
            }

            cout << "Enter a letter: ";
            cin >> userGuess;
            cin.ignore();
            for(int i = 0; i < secretWord.length(); i++)
            {
                if(word[i] == userGuess)
                {
                    userPrompt[i] = userGuess;
                    count = 0;
                }
                else if(count == (wordLength - 1))
                    display++;
                count++;
            }
            winner = checkWin(word, userPrompt, display, secretWord);       
        }
        again = playAgain();
    }while(again == 'Y' || again =='y');
}

char playAgain()
{
    char playAgain;
    cout << "Would you like to play again? Enter y or n: ";
    cin >> playAgain;
    return playAgain;

}
Fiddling Bits
  • 8,712
  • 3
  • 28
  • 46
Dragon Wolf
  • 61
  • 1
  • 2
  • 11
  • 3
    Lesson learned: use a source repository and store, e.g., working intermediate state so you can compare what changed and find out what you broke or just go back and continue from there. [git](http://git-scm.com/) seems to be the current fashionable choice for a source repository. – Dietmar Kühl Dec 15 '13 at 18:45

2 Answers2

2

There are really two questions:

  1. Why doesn't it restart a game? Answer: because the program thinks that the game was successfully played. You set up your variables in front of the loop and you don't reset them to play another game. Recommendation: create a function which actually plays the game and just call it from the outer loop. Play the game in that function.
  2. Why doesn't it increment count? Dunno. Why you think count always stays at 0? However, it seems the condition count == (wordLength - 1) is unlikely to every become true because wordLength is set to the size of secretWord when secretWord happens to be empty (i.e. wordLength is set to 0) and never changed afterwards.
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • actually, it worked before, with that same code. Essentially, wordLength - 1 is how many times the for loop loops through the array. IF it finds the char in the word, it resets the counter to 0, and nothing should happen. IF it loops through the whole char array and the user guess is NOT found, count SHOULD equal exactly wordLength - 1 (since I am using the STRING version of the word to determine the wordLength, it has to be length minus 1 since only C-strings and strlen return an int value WITHOUT the null terminator counted in the length). – Dragon Wolf Dec 15 '13 at 19:06
  • and the way my program is set up, secretWord.length will always have a value because the game won't even begin until AFTER the file was successfully loaded AND the contents of the file are passed successfully into a String array. From here secretWord picks a random word from the array, and THEN this code runs....thus the length always has at least a value of 4 (including null terminator) . – Dragon Wolf Dec 15 '13 at 19:08
  • @DragonWolf: if you don't want an answer, don't ask! You stated the program doesn't work and I told you why. I'm not saying that `wordLength` doesn't have value but has the value `0` because you only set it when `secretWord` is empty And not changed afterwards. I'd guess your change was moving the initializations around breaking your assumption. – Dietmar Kühl Dec 15 '13 at 19:17
  • AH! Now I understand what you were saying lol! I didn't see that in the code, I am going back now to change that! Hopefully that is all it was! Yes, actually the change I made when it broke was setting up the do-while loop, and I set wordLength to the secretWord before the secretword is grabbed (outside the do-while loop in other words) – Dragon Wolf Dec 15 '13 at 19:23
  • ....update now IT WORKS! that solved that issue.... I needed to have it get that length AFTER the secretWord was chosen, not before. I hate overlooking those small things.....guess that is what happens when you work on something for days at a time lol.....thank you all for the help – Dragon Wolf Dec 15 '13 at 19:31
  • @DragonWolf: I have that sometimes, too. What helps is explaining the program to someone or something. I call it _teddybear debugging_ as I like to explain my programs to my teddybear (the approach also has a proper name). It is a lot less embarressing to explain wrong programs to my teddybear than to any of my colleagues. I had once tried to explain a program to our assistent but she was embarressed about a C++ expert asking her to help with debugging a C++ program (she had no idea of C++ at all) and thought I made fun of her when I suddenly thanked her for helping with finding the problem! – Dietmar Kühl Dec 15 '13 at 19:50
1

Asking to play again may be due to not resetting variable "winner". Doing as follows may correct it.

do
{
    winner = false; //set to false at each iteration
    while(usedWords[wordChoice])
    {
        if(wordChoice == 19)
            wordChoice = 0;
        else if(counter == 20)
pulasthi7
  • 901
  • 1
  • 13
  • 15
  • Ok so this took care of the play again issue, it at least works by adding that line in the do-while loop. I also had to place the word choice inside here as well, otherwise it picks the same word again! Still need to solve the issue when they enter and incorrect choice...the loop should increase display by one if the user guess is not in the word..... – Dragon Wolf Dec 15 '13 at 18:58