Some help with a hangman program in C

Discussion in 'C' started by Z3RatuL, Mar 11, 2007.

  1. Z3RatuL

    Z3RatuL New Member

    Joined:
    Mar 11, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Hey guys,

    A friend of mine wrote this code to aid me in a hangman program. Thing is he's doing his service at the army now and I can't finish it myself.

    He made it work without any problems what so ever but I would like to add 2 more functions.

    Those two functions will check to see if the user entered lower case letters (it will accept only UPPER CASE chars, no numbers) and if the word is between 4 to 20 chars...

    Here's the code so far:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <stdlib.h>
    #include <string.h>
    
    /* character set */
    #define MIN_LENGTH 4
    #define MAX_LENGTH 20
    #define MAX_TRIES 3
    #define ASTERISK '*'
    
    
    /* Function declarations. */
    static int guess(const char *word, char letter, char *display);
    static void hangman(const char *word);
    
    
    static int guess(const char *word, char letter, char *display)
    {
        int blanks, i;
    
        blanks = 0;
        for (i = 0; word[i] != '\0'; i++) {
            if (word[i] == letter) {
                display[i] = letter;
            }
            if (display[i] == ASTERISK) {
                blanks++;
            }
        }
        return blanks;
    }
    
    /*
     * Function which plays hangman with user.
     */
    static void hangman(const char *word)
    {
        int count, blanks, oldblanks, len, i, count2;
        char display[MAX_LENGTH+1];
        char letter;
    
        len = strlen(word);
        for (i = 0; i < len; i++) {
            display[i] = ASTERISK;
        }
        display[i] = '\0';
    
        count = 0;
        count2 = MAX_TRIES; /* max tries number */
        oldblanks = len;
        while ((oldblanks > 0) && (count < MAX_TRIES)) {
    		printf("You have %d tries\n", count2);
            printf("%s\n", display);
            printf("Type a letter:\n");
            scanf(" %c", &letter);
            blanks = guess(word, letter, display);
            if (oldblanks == blanks) {
                /* No new chars found, so the letter was incorrect */
                count++;
                count2--;
            }
            oldblanks = blanks;
        }
        if (blanks == 0) {
            printf("YOU WON!\n");
        } else {
            printf("YOU LOST!\n");
        }
        printf("THE WORD WAS \"%s\".\n", word);
    }
    
    
    int main(void)
    {
        int i;
        char word[MAX_LENGTH+1];
    
        /* Give the secret word. */
        printf("Type the word here:\n");
    	scanf(" %s", word);
    	/* Play hangman. */
    	hangman(word);
    	return 0;
        
    }
    Thanks in advance!
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    I didn't read the code, but I'll offer some observations and leave it to you to research and advance your familiarity with the language. There are char manipulators called "tolower ()" and "toupper ()". There is a "strlen ()" function. If the length of an input string is outside of one's desired bounds, one merely detects that and rejects the input (being careful to clear any other extraneous trash from the input buffer). There are additional library functions which allow one to characterize the text: functions such as isalpha (); isdigit (); isprint (); isspace (). Try incorporating some of that into your game.
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    I would suggest you to go through [thread=1142]Hangman game source code[/thread] as well.
     
  4. Z3RatuL

    Z3RatuL New Member

    Joined:
    Mar 11, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Thanks guys, I'll look into it...
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice