Thread: Hangman game

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    19

    Hangman game

    My program compiles, however, there are 3 problems with it:
    1: A bunch of garbage is print after the asterisks. Example: ****wurywerg - I believe there's something wrong in my fillArray() function..which is right after main ( )
    2: My loop never ends! It displays "You have -1 guesses left. Please enter a letter: " and so forth. Is my logic for the for loop okay in play()?
    3: It keeps telling the user to guess even AFTER they've figured out all the letters! I believe this has more to do with my for loop logic in play()

    Play() is the last function!
    Sorry this is so long!

    Code:
    #include    <stdio.h>
    #include    <string.h>
    #include    <ctype.h>
     
     
    #define MAXWORD    20 //max word length
    #define INCORRECT_GUESSES 9 //max # of guesses allowed
     
     
    /* Prototypes */
    void    fill_array( char *theArray, int howMany, char theLetter ); // Fills theArray with howMany copies of theLetter
    int     get_letter( char *theWord, char *soFar ); // Get char from player, checks the letter, shows progress so far
    int     letter_in_word( char *theWord, char *soFar, char theLetter ); // Check if letter is in word, updates progress so far
    void    lower_string( char *someWord ); // Convert the word to lowercase
    void    play( char *theWord ); // Play one game
     
     
     
     
    /* Function definitions */
     
     
    int main( )
    {
        char theWord[ MAXWORD ]; //theWord is an array of characters with the length of MAXWORD 
     
     
        FILE *ifp = fopen( "guesswords.txt", "r" ); //open myFile to read
     
     
        if (ifp == NULL ) //if file doesn't exist
        {
            printf( "File does not exist\n" );
     
     
            return -1; //return code
        }
     
     
         
        fscanf(ifp, "%s", theWord); //scans word from file
         
        lower_string( theWord ); //invokes the function lower_string
        play( theWord ); //passing theWord into play function
     
     
        fclose(ifp); //closes file
     
     
        return 0; //all done
    }
     
     
    /* *********************************************************************** */
    
    
    void fill_array( char *theArray, int howMany, char theLetter ) // Fills theArray with howMany copies of theLetter
    {
        int i;
        for( i = 0; i < howMany; ++i)
        {
            theArray[i] = theLetter;
        }
    }
    
    
     
    /* *********************************************************************** */
     
     
    // Get char from player, checks the letter, shows progress so far
    int get_letter( char *theWord, char *soFar )
    {
        char theLetter;
        int n;
     
     
        printf("Please enter a letter: \n");
        scanf(" %c", &theLetter);
     
     
        theLetter = tolower(theLetter);
     
     
        n = letter_in_word(theWord, soFar, theLetter);
    
    
        return n;
          
    }
     
     
    /* *********************************************************************** */
     
     
     
    int letter_in_word( char *theWord, char *soFar, char theLetter ) // Check if letter is in word, updates progress so far
    {
        int i;
        int numberOfMatches = 0;
     
        for( i = 0; i < strlen(theWord); ++i )
        {
            if (theWord[i] == theLetter)
            {
                soFar[i] = theLetter;
                ++numberOfMatches;
            }
        }
        
        if (numberOfMatches == 0)
        {
            printf("Oops! The letter you guessed is not in the word!\n");
            return 0; //not found
        }
        
        else
        {
            printf("Yay! The letter you guessed is in the word!\n");
            printf("%s\n",soFar);
            return 1; //1 or more was found
        }
    }
     
     
    /* *********************************************************************** */
     
     
    void lower_string ( char *someWord ) // Convert the word to lowercase
    {
        int i;
        for (i = 0; i < strlen(someWord); ++i)
        {
            someWord[i] = tolower(someWord[i]);
        }
    }
     
     
    /* *********************************************************************** */
     
     
    void play( char *theWord ) // Play one game
    {
        //char theWord[MAXWORD]; 
        int maxGuesses = INCORRECT_GUESSES - 1;
        char soFar[MAXWORD];
        int i;
    
    
        fill_array( soFar, strlen(theWord),'*' );
     
        printf("%s\n",soFar);
     
     
        for (i = 0; i < INCORRECT_GUESSES; --maxGuesses)
        {
            int result = get_letter(theWord, soFar);
     
     
            if ( result == 0 )
            {
                printf("You have %d more letters to guess! Keep guessing! \n", maxGuesses);
            }
     
     
            else if ( soFar[MAXWORD] == *theWord)
            {
                printf("You have guessed all letters! Good job! \n");
            }
        }
    
    
        printf("You're out of guesses! The word was %s \n", theWord);
    }
    Last edited by GaitBait; 12-07-2013 at 01:26 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It looks like code written by your tutor, with some deliberate errors introduced for you to find and fix (as an exercise in finding and fixing).

    It's far too neat and well structured for it to have been written by you, for you to come over all clueless as to how to fix it.

    I see you managed to get one issue fixed here, now you've moved on to another forum
    Beginning C. variable not being initilized - Stack Overflow
    Is this homework by successive approximation?

    > for (i = 0; i < INCORRECT_GUESSES; --maxGuesses)
    Explain what this is doing, and whether i will ever reach condition which will cause the loop to exit.
    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.

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Salem View Post
    It's far too neat and well structured for it to have been written by you, for you to come over all clueless as to how to fix it.
    That's rather presumptuous! When I scroll through the code I see inconsistencies in layout and, therefore, if the tutor wrote this then I'm not sure if I'd trust them as a tutor. I think it's more reasonable to assume the OP did write it themselves. Believe it or not, not all newcomers write horribly messy and unstructured code as is seen on this forum quite regularly. There are enough inconsistencies in "style" (I put that in danger quotes because I don't believe that how code is formatted is anything more than a formatting style; style, in general, is more than superficial layout.)

    Other than that, I'd look at the same loop condition that you pointed out.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > That's rather presumptuous!
    Not really.

    A lot of the code posted here is poorly indented, and even when it is indented to some fashion, it is rarely well documented.
    "simple" login authorization code
    Print Diamond in C program in Decrement loop
    Binary File IO: Also, hello again!
    Merging All .txt Files in a directory
    store data in binary file then use binary file to find inverse of matrix form of data

    An answer to one of the issues has already been ignored on SO - go figure.
    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.

  5. #5
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by Hodor View Post
    Believe it or not, not all newcomers write horribly messy and unstructured code as is seen on this forum quite regularly.
    Quote Originally Posted by Salem View Post
    > That's rather presumptuous!
    Not really.

    A lot of the code posted here is poorly indented, and even when it is indented to some fashion, it is rarely well documented.
    I may have already addressed that

  6. #6
    Registered User
    Join Date
    Sep 2013
    Posts
    19
    Quote Originally Posted by Salem View Post
    It looks like code written by your tutor, with some deliberate errors introduced for you to find and fix (as an exercise in finding and fixing).

    I see you managed to get one issue fixed here, now you've moved on to another forum
    Beginning C. variable not being initilized - Stack Overflow
    Is this homework by successive approximation?

    > for (i = 0; i < INCORRECT_GUESSES; --maxGuesses)
    Explain what this is doing, and whether i will ever reach condition which will cause the loop to exit.
    Lol damn, that's a pretty harsh assumption to make. No, a tutor did not write this. I've spent hours on this program.
    The post on stackoverflow isn't mine either. Probably someone else from my class. We were given the function prototypes. Everything else is our own work.
    So no, this is not "homework by successive approximation" I LOVE coding and wish to actually pursue this as a career in the future.

    As for the loop, I'm trying to get it to keep decrementing until there are no more guesses left, and then END. Not keep going to 0, -1, -2, etc!

    EDIT: I GOT IT! IT WORKS! OMG! I just needed to sleep on it haha
    By the way, our code's indentation is a pretty crucial part of our grade. My class has been trained to write all our codes in a readable fashion.
    Also, I wasn't ignoring your posts, I was sleeping -.-
    Last edited by GaitBait; 12-07-2013 at 12:28 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hangman game
    By GaitBait in forum C Programming
    Replies: 3
    Last Post: 12-06-2013, 01:32 PM
  2. Hangman Game
    By Mcdom34 in forum C# Programming
    Replies: 3
    Last Post: 10-13-2012, 11:21 AM
  3. Hangman game
    By Dontgiveup in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2011, 04:44 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, 02:05 PM