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.