Thread: Need a little help with hangman

  1. #16
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > while( guess > 0 || !won() );
    Maybe && rather than ||

    As soon as you win, you want to get out of this loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    } while( guess > 0 || !won() );
    I don't see any catch of won's return value. Does the whole function evaluate to a 1 or 0 even though the flag is not caught by a variable?

    Change the || to &&, in the above line of code.

    Edit: Too Slow!!

    Adak

  3. #18
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    So changed it to && and now, when I guess a letter, it runs the won() function no matter what LOL

  4. #19
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It's supposed to, isn't it? Otherwise you can't tell if they've won or not.
    Code:
    int won()
    { 
        int flag = 0;
        
        if(strcmp(word_to_guess, word_in_prog)) {
            printf("\n You've WON! %s is the right answer!", word_in_prog);                     
            flag = 1;
        }
        return flag;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #20
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    from post#4:
    Quote Originally Posted by nadroj
    in your win function:
    Code:
    if(strcmp(word_to_guess, word_in_prog)) {
    strcmp will return 0 if the strings are equal, so change your if condition!

  6. #21
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Yes, ie

    Code:
    if(!strcmp
    Last edited by zacs7; 04-10-2007 at 09:00 PM.

  7. #22
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Use == 0 rather than ! for extra readability.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #23
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    Awesome, thank you so much for the help so far guys. The == 0 thing worked well but it's still taking away a guess when I've guess right. I know it's a big favor, but can someone if they have spare time look over it and maybe help me with a few more changes to make it work. I've honestly looked through it and am not really sure what I've done wrong. I would much appriciate the help if someone could do that

  9. #24
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    Code:
            do {
               printf("\n Number of tries left : %d", guess);
               printf("\n %s", letters_guessed);
               printf("\n Word to guess : ");
               
               for(i = 0; i<strlen(word_in_prog); i++)
                   printf("%c ", word_in_prog[i]);
                   
               printf("\n Please guess a letter : ");
               scanf("%c", &c);
               c = tolower(c);
    You have the same problem here as you had when asking the player if he wants to play again.

    A newline character is left over for each scanf, and consumed by the next iteration of your while loop.

  10. #25
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I'd have to see your current code to find out why you're losing a turn on a correct guess. However, one thing that needs pointing out:
    Code:
    void initialize()
    {
         int i;
         
         guess = 6;
         
         for(i=0; i<26; i++) {
                  letters_guessed[i] = '-';
         }
         for(i=0; i<strlen(word_to_guess); i++) {
                   word_in_prog[i] = '_';
         }
    }
    Using strlen() as the control condition in a loop is a bad idea 99.9&#37; of the time. strlen() itself has to traverse the string to determine the length so you're really traversing it twice. It doesn't make much difference on small strings like you're using here, but the alternative, better solution is easy to implement.

    Just replace:
    Code:
    for(i=0; i<strlen(word_to_guess); i++)
    with:
    Code:
    for(i=0; word_to_guess[i] != '\0'; i++)
    It will iterate through each array element until the nul-terminator is reached. You use the strlen() technique several times throughout your code, and it's best to get into the habit of doing it the right way sooner than later.

    Also, there's a function called memset() that would be handy for use in your initialize() function. You could rewrite the function like this:
    Code:
    void initialize()
    {
         guess = 6;
         
         memset(letters_guessed, '-', 26);
         memset(word_in_prog, '_', strlen(word_to_guess));
    }
    Also, if you made word_in_prog a string instead of an array of chars (word_in_prog[strlen(word_to_guess)] = '\0') then you could use the != '\0' trick in your for() loops on it as well.
    Last edited by itsme86; 04-11-2007 at 02:33 PM.
    If you understand what you're doing, you're not learning anything.

  11. #26
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    I don't understand really. What's wrong with that? The /n is messing it up or is something else wrong?

  12. #27
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    K, I've got what itsme is saying and will replace that. Not sure about before with the scanf's

  13. #28
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    So, I've taken all the advice so far but it's still not running right. Every time I guess a letter, it still minuses from the guesses, even if the letter is in the word. And, it doesn't print out the sentences in the do...while function for some reason. It's supposed to display the # of guesses left and the letters guessed already. Any help with any of this?

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    #define MAX_SIZE = 20;
    #define MAX_LETTERS = 26;
    #define MAX_GUESS = 6;
    
    void instruct();
    int play_again();
    void initialize();
    int letter_check(char c);
    int won();
    
    
    char word_to_guess[20], word_in_prog[20], letters_guessed[26];
    int guess;
    
    int main()
    {
         int i;
         char c;
         FILE *in_file;
         
         in_file = fopen("wordlist.txt","r");
         
         do {
            if( feof(in_file) )
                rewind(in_file);
                
            fscanf(in_file, "%s", word_to_guess);
            
            for(i = 0; i < strlen(word_to_guess); i++) {
                  word_to_guess[i] = tolower(word_to_guess[i]);
            }
            
            initialize();
            instruct();
            do {
               printf("\n Number of tries left : %d", guess);
               printf("\n %s", letters_guessed);
               printf("\n Word to guess : ");
               
               for(i = 0; i<strlen(word_in_prog); i++)
                   printf("%c ", word_in_prog[i]);
                   
               printf("\n Please guess a letter : ");
               scanf("%c", &c);
               c = tolower(c);
               if( !letter_check(c) )
                  guess--;
                  
            } while( guess > 0 || !won() );
            if( guess == 0 ) 
                printf("\n You LOSE!");
         } while(play_again());
         
         fclose(in_file);
    }
    
    void instruct()
    {
     printf("\n Hangman v6.9");
     printf("\n To play, guess a letter.  You get 6 wrong tries, then your man gets hung. Is that a bad thing?");
    }
    
    int play_again()
    {
        char c;
        
        printf("\n Would you like to play again? [y/n]");
        scanf("%c", &c);
        c = tolower(c);
        if(c == 'y') 
             return 1;
        else
            return 0;
    }
    
    void initialize()
    {
    
         guess = 6;
         
         memset(letters_guessed, '-', 26);
         memset(word_in_prog, '_', strlen(word_to_guess));
    }
    
    
    int letter_check(char c)
    {
        int i, flag = 0;
        
        for(i = 0; i<strlen(word_to_guess); i++) {
              if(c == word_to_guess[i]) {
                   flag = 1; 
                   word_in_prog[i] = c;
              }
        }
        return flag;
    }
    
    int won()
    {
        int flag = 0;
        
        if(strcmp(word_to_guess, word_in_prog)==0) {
            printf("\n You've WON! %s is the right answer!", word_in_prog);                     
            flag = 1;
        }
        return flag;
    }
    Please, if include help, can you include code or an example of code with explination. I'm a little confused with the earlier posts and would really enjoy if I could get this please

  14. #29
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    define MAX_SIZE = 20;
    #define MAX_LETTERS = 26;
    #define MAX_GUESS = 6;
    Should be:
    Code:
    define MAX_SIZE 20
    #define MAX_LETTERS 26
    #define MAX_GUESS 6
    Code:
    void initialize()
    {
    
         guess = 6;
         
         memset(letters_guessed, '-', 26);
         memset(word_in_prog, '_', strlen(word_to_guess));
    }
    Should be:
    Code:
    void initialize()
    {
    
         guess = MAX_GUESS;
         
         memset(letters_guessed, '-', MAX_LETTERS);
         memset(word_in_prog, '_', strlen(word_to_guess));
    }
    You're still using scanf:
    Code:
    printf("\n Please guess a letter : ");
    scanf("&#37;c", &c);
    What happens when you press a letter? Nothing. It waits for you to press ENTER. You aren't doing anything with the ENTER key.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #30
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    as suggested afew times, you havent changed your while condition yet
    Code:
            } while( guess > 0 || !won() );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hangman Help - Code included
    By darren78 in forum C Programming
    Replies: 3
    Last Post: 02-17-2009, 09:35 AM
  2. Hangman Game Troubles
    By emerica240 in forum C Programming
    Replies: 9
    Last Post: 11-26-2008, 01:39 AM
  3. Hangman, Help me with the thinking
    By Livijn in forum C# Programming
    Replies: 14
    Last Post: 02-09-2008, 03:16 PM
  4. Help doing Hangman...
    By Kreative in forum C Programming
    Replies: 11
    Last Post: 08-18-2002, 09:22 PM
  5. Using 'if' with char arrays or string objects
    By c++_n00b in forum C++ Programming
    Replies: 36
    Last Post: 06-06-2002, 09:04 PM