Like Tree1Likes
  • 1 Post By Rodaxoleaux

Hangman game - unmasking the letters.

This is a discussion on Hangman game - unmasking the letters. within the C++ Programming forums, part of the General Programming Boards category; Hello again, I am trying to design a hangman game but I'm having an issue with trying to get to ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    23

    Hangman game - unmasking the letters.

    Hello again,

    I am trying to design a hangman game but I'm having an issue with trying to get to the next step from where I am.

    So far I am able to generate random words, get the program to chose a word from this random list so the user can try to guess its letters.

    I can also tell the user how many letters there are in the word. I've also used a for loop to display several _ (underscores) in accordance with the amount of letters in the word.

    However all I've done is outputted underscores. I don't know how I'll be able to unmask these underscores so that if the user guesses the right letter then the _ will change into the correct letter in the word.

    At the moment I'm just able to tell the user that the letter they have entered does indeed exist in the word.

  2. #2
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    post code please.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    23
    Since its an actual assignment I won't be able to post all of it, I hope you understand..

    This is the actual part that I can't move from. There isn't a way that I know of to go back to the _'s to change them:

    Code:
                for (int a=0; a<theword.length();a++)  //print _'s for letters in the word
            {
                cout << "_";
            }
    
    
            for (int b=0;b<theword.length();b++)    //check if the letter is in the word
            {
                if (theword[b] == user)
                cout << "this letter is in the word";
                cin >> user;
            }

  4. #4
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    I would use a map<char,bit> to keep track of it.
    do something like this:
    1. push each letter in the string to the map, and initialize the bit to false.
    2. each time a letter is guessed right, turn the bit to true.
    3. If you want to display the letter, check if the bit is true, if it is, cout it.

    something like this
    Code:
     man<char,bool> map_name_you_use;
    
    for (int b=0;b<theword.length();b++)    //check if the letter is in the word
    {
        if (theword[b] == user)
         {
           cout << "this letter is in the word";
           map_name_you_use[theword[b]]=true;
          }
        cin >> user;
    }
    Last edited by nimitzhunter; 04-28-2012 at 11:47 AM.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  5. #5
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    If you don't know map, then you can write a struct yourself. Something like this
    Code:
    struct keepingtrackofsomething{
      int sizeof_Array; 
      char * array;
      bool * booleanarraytoreference;
    };
    "All that we see or seem
    Is but a dream within a dream." - Poe

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    23
    I'm having difficulty to understand that.

    You say to use map<char,bool> but in the code presented it says man<char,bool> what does man stand for?

    "
    map_name_you_use" is this a name that I come up with and use as some sort of variable name?

    Where about will I put the struct? Somewhere in the main function?

    Also what do you mean by "booleanarraytoreference"?

  7. #7
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    the idea that nimitzhunter tried to express was that you should create some kind of structure that holds the complete state of your game.

    If you create one such structure in main() you could pass a pointer or a reference to that struct as a single parameter to the various functions of your game.

    A definition of that struct for you program could be

    Code:
    struct hangman_state {
      int word_length; 
      char * secret_word;
      bool * uncovered_positions;
    }
    Hope it's clearer now.

    Kurt

  8. #8
    and the hat of Marriage Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    178
    Quote Originally Posted by hencherz View Post
    I'm having difficulty to understand that.

    You say to use map<char,bool> but in the code presented it says man<char,bool> what does man stand for?

    "
    map_name_you_use" is this a name that I come up with and use as some sort of variable name?

    Where about will I put the struct? Somewhere in the main function?

    Also what do you mean by "booleanarraytoreference"?
    That was obviously a typo.

    Structs go outside of the main initialisation. It's like a class. You should practice your object oriented programming before trying to make something like a game. I made that mistake before and had to go back.
    nimitzhunter likes this.
    How to ask smart questions

    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesWritten);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hangman game
    By Dontgiveup in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2011, 04:44 PM
  2. Help with hangman game.
    By astarialexi in forum C Programming
    Replies: 8
    Last Post: 03-13-2011, 04:04 AM
  3. !Help! Similar to hangman game
    By xxdevillxx in forum C Programming
    Replies: 2
    Last Post: 03-02-2011, 06:14 PM
  4. Hangman Game - Need Help!!
    By krobort in forum C++ Programming
    Replies: 3
    Last Post: 10-12-2006, 04:15 PM
  5. New game: Hangman!
    By abrege in forum Game Programming
    Replies: 6
    Last Post: 12-05-2002, 01:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21