Thread: A little confused and could use some 1 on 1 help please :-D

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    15

    A little confused and could use some 1 on 1 help please :-D

    So, I've completed this hangman game and must now convert it to Struct. Problem is, I really don't understand how Struct works. I mean, it's kinda appealing to me but it's still confusing to me as well. So, here's the original code.

    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 : ");
                          gets(&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]");
        gets(&c);
        c = tolower(c);
        if(c == 'y')
             return 1;
        else
            return 0;
    }
    
    void initialize()
    {
    
         guess = MAX_GUESS;
         
         memset(letters_guessed, '-', MAX_LETTERS);
         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;
    }
    And here are my instructions

    Code:
    Modify you Hangman program to incorporate the following additions:
    ·	Use the following struct definition 
    typedef struct{
    	char hangword[SIZE];//for the word read from a file
    	int numguess;//should be set to 0 or 6 
    	char currentword[SIZE];//the word filledwith ?, *, -, or any character that works
    	char guessedltrs[SIZE];//the list of used letters
    	char guess[SIZE];//to contain the current letter guessed by the user
    	//this can aslo be a single char if that's how your original hangman program works
    }hangGame;
    
    ·	Declare and use an array of hangGames. 
    ·	Ask the user for the number of games to play
    o	This will tell you how many words to read from the file. 
    ·	Preload the array :
    o	with the words to be guessed from the file (if the user answered 5 to the last question, you should load 5 words from the file into the hangword string in 5 hangGame cells
    o	The “word in progress” array and the “guessed letters” array should be set up
    I put it in code tags just so it'd stick out. Any help, including why my play_again loop isn't working, would be much appriciated. I've worked with it a little and got it working some what but it's not doing what it should. Also, just not understanding Struct enough to really confidently use it and the book, no matter how much I read it, doesn't give clear enough examples to really provide me with a background of Struct. Please, any help would be many times over appriciated! Just like a little help or an example to get me on the way or even some examples/instructions.

  2. #2
    Registered User
    Join Date
    Apr 2007
    Posts
    51
    Quote Originally Posted by Halios00 View Post
    So, I've completed this hangman game and must now convert it to Struct. Problem is, I really don't understand how Struct works. I mean, it's kinda appealing to me but it's still confusing to me as well. So, here's the original code.
    A struct is a way of grouping traits into a "container" that you can use to reference. I know there are tons of tutorials on structs if you just do a google search you should be able to get one.

    for instance -> here, or here, etc...

    Quote Originally Posted by Halios00 View Post
    ....including why my play_again loop isn't working....
    What is it doing or not doing?

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    Well, when it asks Would you like to play again and you hit y (yes) then it just shows the same word you had already guessed.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Which shows that you're not re-initializing after one game in played if the user hits 'y',

    BEFORE anything is printed out on the screen. Initialize FIRST!

    Adak

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    51
    I am not sure with out running it. I usually use fgetsm not fscanf.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    So, I'm a little confused still. How would I incorporate

    Code:
    typedef struct{
    	char hangword[SIZE];//for the word read from a file
    	int numguess;//should be set to 0 or 6 
    	char currentword[SIZE];//the word filledwith ?, *, -, or any character that works
    	char guessedltrs[SIZE];//the list of used letters
    	char guess[SIZE];//to contain the current letter guessed by the user
    	//this can aslo be a single char if that's how your original hangman program works
    }hangGame;
    into my program? As I see it, it would change the whole program, wouldn't it? Please, can someone give me a quick example or even use my code to show me where I need to get started to incorporate the code into my own.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    Ty Adak for the re-initialize part. I forgot about that.v:-D

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    15
    I've gotta go to work and won't be able to check this board till tonight. If anyone could give me some good help on this and help me out with this, I would be extremely appriciative and owe you greatly. Not sure how I'd repay back or even if ya would want me to but I would be extremely happy and appriciative for the help. Thanks :-D

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    45
    As I see it, it would change the whole program, wouldn't it?
    Yeah, I think that's the point
    we are one

Popular pages Recent additions subscribe to a feed