Question: Hangman in C! Write a program that will read from a random word from a text file (dictionary.txt) containing English words (one word per line).

Hangman in C!

Write a program that will read from a random word from a text file (dictionary.txt) containing English words (one word per line). Continue to ask the user for a letter until the user has either guessed all the letters in the word, or until the word H-A-N-G-M-A-N has been completely filled out, whichever comes first.

After each guess, print out the letters correctly guessed and the letters in H-A- N-G-M-A-N opened as a consequence of missed guesses.

Example Below:

In file dictionary.txt:

computer program ...

Suppose the word program was randomly selected.

Guess your next letter >> p p------

------- //where hangman should be spelled

Guess your next letter >> g

p--g--- ------- Guess your next letter >> z

>>>>>>>Incorrect<<<<<<<<<

p--g---

H------

Here is my code:

I have all this code done, but i'm unsure what I need to do to have it as the same familiar as above. If you can please help me out, and show me the output that you get. The dictionairy file is above and just a list of words.

hangman.h (Header):

#include

#include

#include

//char* means character string

void get_word(char*, char*);

int game_over(int, char*, char*);

void get_guess(char*);

void check_guess(char, int*, char*, char*);

get_word.c

#include "hangman.h"

void get_word(char filename[100], char key[100])

{

int num_words, word_num;

FILE *fp;

srand((int)time(0));

fp = fopen(filenam, "r");

fscanf(fp, "%d", &num_words);

word_num = rand() % num_words;

for(int i = 0, i <= word_num; i++) {

fgets(key, 100, fp); //fscanf(fp, "%s", key);

}

fclose(fp);

}

game_over.c

#include "hangman.h"

int game_over(int hangman, char guess_word[100], char key[100])

{

if(hangman == 7)

return 1;

else if(strcmp(key, guess_word) == 0)

return 1;

else

return 0;

}

get_guess.c

#include "hangman.h"

void get_guess(char *c) {

print("Enter a guess: ");

scanf("%c", &c);

}

check_guess.c

#include "hangman.h"

void check_guess(char guess_char, int *hangman, char guess_word[100], char key[100])

{

int found = 0;

for(int i = 0; i < strlen(key); i++)

{

if(guess_char == key[i])

{

guess_word[i] = guess_char;

found =1;

}

}

if(found = 0)

(*hangman)++;

}

print_info.c

#include "hangman.h"

void print_info(int hangman, char word[100])

{

printf("%s ", word);

printf("You have %d characters left", 7-hangman);

}

main.c

#include "hangman.h"

int main(int argc, char *argv[])

{

char key[100], guess_word[100] = " ";

int hangman = 0;

char guess_char;

if(argc == 1)

getWord("dict.txt", key);

else

get_word(argv[1], key);

strcpy(guess_word, key);

for(int i = 0; i < strlen(key); i++)

guess_word[i] = '_';

while(!game_over(hangman, guess_word, key))

{

print_info(hangman, guess_word);

get_guess(&guess_char);

check_guess(guess_char, &hangman, guess_word, key);

}

} //done

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!