Thread: HANG MAN i need deperate help

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    31

    HANG MAN i need deperate help

    Help i cant figure out what is wrong with this program i cannot get it to run properly!
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define MAXNUMINCORRECT 5
    #define MAXWORDS 1000
    #define MAXSIZE 30
    
    int readWords(char allWords[][MAXSIZE],FILE *fin);
    int loadWord(char allWords[][MAXSIZE], char word[MAXSIZE], int numWords);
    void printBlank(char emptyword[MAXSIZE], int word_len);
    char getMove(int letters_used[26]);
    int doMove(int len, char emptyword[MAXSIZE], char word[MAXSIZE],  char attempt);
    int gameSolved(char emptyword[MAXSIZE], char word[MAXSIZE]);
    void drawHangman(int tries);
    
    int main(void) 
    {
        char filename[MAXSIZE];
        int numWords =0;
        
        char choice[8];
        char allWords[MAXWORDS][MAXSIZE];
    
        
        int valid_move;
        int letters_used[26];
        char emptyword[MAXSIZE];
        int already_used;
        int tries;
        int number_words;
        char letter_guess;
        int word_len;
        
        char word[MAXSIZE];
        int length_of_word  = 0;
        char guessed_letter;
        char attempt;
        
        int lengthWord;
        
        // Seed the random generator
        srand(time(0));
    
        // Ask the user for the input file.
        printf("What file stores the puzzle words?\n\n");
        scanf("%s", filename);  
        
        FILE* fin;
        
        fin = fopen(filename, "r");
          
        if(fin == (0))
        {
            printf("Please enter a valid filename. (\"%s\") Press Enter to exit \n\n", filename);
            system("PAUSE");
            return 0;
        }
       //Ask the user if they would like to play the game
        printf("1.) Would you like to play hangman? (yea/no)\n\n");
        scanf("%s ", choice);
        
        numWords = readWords(allWords, fin);
        
        fclose(fin);
      
        do{
                tries = 0;
                int j;
                int k;
    
                lengthWord = loadWord(allWords, word, numWords);
        
                for(j = 0; j < 26; j++)
                {
                    letters_used[j];
                }
                for(k = 0; k < lengthWord; k++)
                {
                    emptyword[k] = '_';
                }
        
                    while(tries != MAXNUMINCORRECT)
                    {
                        guessed_letter = getMove(letters_used);
                        printBlank(emptyword, word_len);
                        valid_move = doMove(word_len, emptyword, word, attempt);
                
                        if(gameSolved(word, emptyword))
                        {
                            printf("Congratulations, You got the correct word, %s.\n");
                            break;
                        }
                        if(valid_move != 0 && letters_used[(int)(guessed_letter - 'A')] >= 2) // If the letter has been guessed already and is in the puzzle, it counts as a miss
                        {
                            printf("Sorry, you have guessed that letter already.\n");
                            printf("Now it counts as a miss.\n");
                            tries++; 
                            already_used = 1;                
                        }
                        else 
                        {
                            already_used= 0;
                        }
                        if(valid_move == 0)  // If the letter the user guesses is not in the real word, subtract 1 from the number of attempts left
                        {
                            printf("Sorry, that letter is NOT in the puzzle.\n");
                            tries++;
                        }
                        else if(already_used == 0 && valid_move != 0 && !gameSolved(word,emptyword))
                        {
                            printf("Congratulations, you guessed a letter in the puzzle!\n");
                        }
                    
                        if(tries != MAXNUMINCORRECT)
                        {
                            printf("You currently have %d incorrect guesses.\n", tries); 
                            drawHangman(tries);
                        }
                        if(tries == MAXNUMINCORRECT)
                        {
                            drawHangman(tries);
                        }
                      
                    }
            }while(strcmp(choice, "no") != 0)  
    
        system("PAUSE");
        return 0;
    }
    
    // Pre-conditions: allWords must be of size numWords, at least, fin must
    //                 point to a file with n words, with only uppercase letters
    //                 ready to read from the first word.
    // Post-conditions: The numWords stored in the file pointed to by fin will
    //                  be stored in the array allWords.
    
    
    int readWords(char all_words[][MAX_SIZE], FILE *fin)
    {
      int numWords = 0, i;
      
      fscanf(fin, "%d", &numWords);
      if(i == EOF || i != 1)
        return 0;
     
      for (i=0; i < numWords; i++) // Loop through the file
      {
        fscanf(fin, "%s", allWords[i]); // Scan every word in the file
      }
      return numWords;
    
        
    int loadWord(char allWords[][MAXSIZE], char word[MAXSIZE], int numWords)
    {
        int get_word; 
        int len;
        get_word = rand() % numWords;  
        strcpy(word, allWords[get_word]); 
        len = strlen(word); 
        return len;
    }
    
    void printBlank(char emptyword[MAXSIZE], int word_len)
    {
        int j; 
        
        printf("Here is your puzzle:\n");
        
        for(j = 0; j < word_len; j++)
        {
            printf("%c ", emptyword[j]);
        }
        printf("\n");   
    }
    
    char getMove(int letters_used[26])
    {
        char attempt, up_attempt;
        printf("Please enter your guess.\n"); 
        scanf("%c", &attempt);
        attempt = toupper(attempt); 
        return attempt;
    }
    int doMove(int len, char emptyword[MAXSIZE], char word[MAXSIZE],  char attempt)
    {
        int valid;
        int j; 
        
        
        for(j = 0; j < len; j++)
        {                                   
            if(word[j] == attempt)
            {
                valid++;
                attempt = emptyword[j];
            }
        }
        return valid;
    }
    void drawHangman(int tries)
    {
        if(tries == MAXNUMINCORRECT)
        {
            char word;
            printf("Sorry, you have made %d incorrect guesses, you lose.  The word was %s.\n", MAXNUMINCORRECT, word); 
            printf("+----+       \n");
            printf("|    |       \n");
            printf("|    O       \n");
            printf("|   /|\\     \n");
            printf("|   / \\     \n");
            printf("| Your're Dead  \n");
            printf("=============\n\n");
        }
        else if(tries == 4)
        { 
            printf("+----+       \n");
            printf("|    |       \n");
            printf("|    O       \n");
            printf("|   /|\\     \n");
            printf("|     \\     \n");
            printf("|            \n");
            printf("=============\n\n");
        }
        else if(tries == 3)
        {
            printf("+----+       \n");
            printf("|    |       \n");
            printf("|    O       \n");
            printf("|   /|\\     \n");
            printf("|            \n");
            printf("|            \n");
            printf("=============\n\n");
        }
        else if(tries == 2)
        {
            printf("+----+       \n");
            printf("|    |       \n");
            printf("|    O       \n");
            printf("|   /|       \n");
            printf("|            \n");
            printf("|            \n");
            printf("=============\n\n");
        }
        else if(tries == 1)
        {
            printf("+----+       \n");
            printf("|    |       \n");
            printf("|    O       \n");
            printf("|    |       \n");
            printf("|            \n");
            printf("|            \n");
            printf("=============\n\n");
        }
        else if(tries == 0)
        {
            printf("+----+       \n");
            printf("|    |       \n");
            printf("|            \n");
            printf("|            \n");
            printf("|            \n");
            printf("|            \n");
            printf("=============\n\n");
        }
    }
    int gameSolved(char emptyword[MAXSIZE], char word[MAXSIZE])
    {
        if(strcmp(word,emptyword) != 0)
        {
            return 0;
        }
        else
        {
            return 1;
        }
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Do you get any errors or anything? What doesn't work about it? Does it compile? Will it run? Is it a "segmentation fault"? Why do you think it doesn't work?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    The following will at least get it to compile.
    Code:
    Line 128: }while(strcmp(choice, "no") != 0); // missing semi-colon
    Line 141: int readWords(char all_words[][MAXSIZE], FILE *fin) // MAXSIZE, not MAX_SIZE
    Line 151: fscanf(fin, "%s", all_words[i]); // all_words, not allWords
    Line 154: } // missing closing brace
    Line 185:     attempt = toupper(attempt); // requires #include <ctype.h>

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    31
    this is my error..
    [Warning] passing arg 1 of `readWords' from incompatible pointer type

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > [Warning] passing arg 1 of `readWords' from incompatible pointer type
    Make your implementation match your prototype.
    In other words, watch the _
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hang man program hanging
    By pieisgood in forum C++ Programming
    Replies: 14
    Last Post: 02-21-2009, 06:08 PM
  2. casting - pointer to pointer
    By terminator in forum C Programming
    Replies: 56
    Last Post: 04-23-2008, 12:54 AM
  3. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  4. we of the cage
    By mithrandir in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 08-21-2002, 10:14 AM
  5. Gender Humour Thread
    By stevey in forum A Brief History of Cprogramming.com
    Replies: 39
    Last Post: 06-01-2002, 01:12 PM