please help me finish my Hangman program (C)

This is a discussion on please help me finish my Hangman program (C) within the C Programming forums, part of the General Programming Boards category; Can somebody please help me finish this Hangman game. This is what I got so far in .c file: Code: ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    1

    Unhappy please help me finish my Hangman program (C)

    Can somebody please help me finish this Hangman game.
    This is what I got so far in .c file:

    Code:
    /*
    
        Finish
          initGuessStr() function
          takeGuess(0 function
          
          the main loop
          
        
       
       
        
    
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>
    #include <string.h>
    
    #define MAX_WORD_LENGTH 80
    
    void drawHangman(int level);
    void getWord( char targetStr[MAX_WORD_LENGTH]);
    int getRand(int first, int last);
    void initGuessStr(int length, char guessStr[MAX_WORD_LENGTH]);
    
    void takeGuess(char targetStr[MAX_WORD_LENGTH],
                    char guessStr[MAX_WORD_LENGTH],
                    char badStr[MAX_WORD_LENGTH],
                    char guessChar);
    
    int main()
    {
        int  len, i;
        char ch;
        
        char targetStr[MAX_WORD_LENGTH];
        char guessStr[MAX_WORD_LENGTH];
        char badStr[MAX_WORD_LENGTH] = "";
      
        getWord(targetStr);
        initGuessStr(strlen(targetStr),guessStr); 
        
        drawHangman(10);
        puts(targetStr);
        puts(guessStr);
        puts(badStr);
        
        printf("enter guess\n");
        scanf("%c", &ch);
        takeGuess(targetStr, guessStr, badStr, ch);
         puts(guessStr);
        puts(badStr);
    
         system("pause");
    
        
        return 0;
    }
    
    int getRand(int first, int last)
    {
       static int firstTime = 1;
       int amountOfNumbers;
       if (firstTime == 1){
          //first time in this function, seed the random number generator
           firstTime = 0;
    	   srand(time(NULL));
       }
       amountOfNumbers = last - first + 1;
       return(rand() % amountOfNumbers + first);
    }
    
    
    
    void drawHangman(int level)
    {
    
        switch(level){
        case 0:
        printf("   ---\n");
        printf("  |   |\n");
        printf("  |   \n");
        printf("  |   \n");
        printf("  |   \n");
        printf("  |   \n");
        printf("  |   \n");
        printf("  |\n");
        printf("---------\n");
        break;
        
        case 1:
        printf("   ---\n");
        printf("  |   |\n");
        printf("  |   O\n");
        printf("  |   \n");
        printf("  |   \n");
        printf("  |   \n");
        printf("  |   \n");
        printf("  |\n");
        printf("---------\n");
        break;
        
        case 2:
        printf("   ---\n");
        printf("  |   |\n");
        printf("  |   O\n");
        printf("  |   |\n");
        printf("  |   \n");
        printf("  |   \n");
        printf("  |   \n");
        printf("  |\n");
        printf("---------\n");
        break;
        case 3:
        printf("   ---\n");
        printf("  |   |\n");
        printf("  |   O\n");
        printf("  |   |\n");
        printf("  |   |\n");
        printf("  |   \n");
        printf("  |   \n");
        printf("  |\n");
        printf("---------\n");
        break;
        case 4:
        printf("   ---\n");
        printf("  |   |\n");
        printf("  |   O\n");
        printf("  |   |\n");
        printf("  |  /|\n");
        printf("  |   \n");
        printf("  |   \n");
        printf("  |\n");
        printf("---------\n");
        break;
        
        case 5:
        printf("   ---\n");
        printf("  |   |\n");
        printf("  |   O\n");
        printf("  |   |\n");
        printf("  |  /|\\\n");
        printf("  |   \n");
        printf("  |   \n");
        printf("  |\n");
        printf("---------\n");
        break;
        
        case 6:
        printf("   ---\n");
        printf("  |   |\n");
        printf("  |   O\n");
        printf("  |   |\n");
        printf("  |  /|\\\n");
        printf("  |   |\n");
        printf("  |   \n");
        printf("  |\n");
        printf("---------\n");
        break;
        
        case 7:
        printf("   ---\n");
        printf("  |   |\n");
        printf("  |   O\n");
        printf("  |   |\n");
        printf("  |  /|\\\n");
        printf("  |   |\n");
        printf("  |  / \n");
        printf("  |\n");
        printf("---------\n");
        break;
        
        default:
        printf("   ---\n");
        printf("  |   |\n");
        printf("  |   O\n");
        printf("  |   |\n");
        printf("  |  /|\\\n");
        printf("  |   |\n");
        printf("  |  / \\\n");
        printf("  |\n");
        printf("---------\n");
        }
    }
    
    
    void getWord( char targetStr[])
    {
        FILE *fp;
        int n, i, which;
        
        fp = fopen("words.txt", "r");
        if(fp == NULL){
            printf("unable to open the word file\n");
            return;
        }
        fscanf(fp, "%d", &n);
        fgetc(fp);//eat the new line
        
        which = getRand(0,n-1);
        for(i = 0; i <= which; ++i)
        {
            fgets(targetStr, MAX_WORD_LENGTH - 1, fp);
        }
    
        fclose(fp);
        
        i = strlen(targetStr);
        targetStr[i-1] = '\0'; //strip out the new line
    }
    
    void initGuessStr(int length, char guessStr[MAX_WORD_LENGTH])
    {
         //fix
         
         guessStr[0] = '\0';   
    }
    
    void takeGuess(char targetStr[MAX_WORD_LENGTH],
                    char guessStr[MAX_WORD_LENGTH],
                    char badStr[MAX_WORD_LENGTH],
                    char guessChar)
    {
        //fix
        int len = strlen( badStr);
        //just add it to the bad string
         badStr[len] =  guessChar;
          badStr[len + 1] = '\0';       
                    
    }

    and I got a seperate file with Words wich contains:

    16
    wolf
    donkey
    elephant
    monkey
    coyote
    rabbit
    iguana
    shark
    boll weevil
    tiger
    lion
    llama
    camel
    kangaroo
    mouse
    walleye
    bug in get word


    PLEASE HELP ME

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program help :>
    By n2134 in forum C Programming
    Replies: 9
    Last Post: 02-06-2010, 11:12 AM
  2. Replies: 40
    Last Post: 09-01-2006, 12:09 AM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

Tags for this Thread


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