IMPLMENTED IN C 2. Create a Hangman game. The following are the requirements for
ID: 3765829 • Letter: I
Question
IMPLMENTED IN C
2. Create a Hangman game. The following are the requirements for your game.
a. You should read a list of ten possible words in from a file.
b. Your game should randomly choose one of the words from the list and display the appropriate number of blanks.
c. The user should then be able to play the game.
d. Your game should display the appropriate ASCII art representing the hangman.
e. If the user chooses to play again, he/she should be able to.
f. If the user does not choose to play again, you should display the number of wins/losses during the session.
ALL IMPLMENTED IN C
Explanation / Answer
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>
#define NUM_OF_WORDS 25
#define NUM_OF_CHANCES 5
#define TRUE 1
#define FALSE 0
void show_heading_rules();
void scold_and_quit();
void see_in_word(char, const char [], char *, const size_t, unsigned int *);
void show_word_clue(const size_t);
void show_heading_rules()
{
printf(" HANGMAN ");
printf("Hangman is a game where you are expected to guess a word in a certain number of chances that is"" before you hang the hangman. ");
printf("A word has been chosen, begin guessing it. Good luck ");
printf("Guess the Word: ");
}
void scold_and_quit()
{
printf(" You killed a person, you dumb shit! Good Bye. ");
exit(0);
}
void show_word_clue(const size_t len)
{
size_t i;
for(i = 0; i < len; i++)
printf(" _ ");
printf(" ");
}
void see_in_word(char guessed, const char word[], char *a, const size_t len, unsigned int *found)
{
size_t i;
for(i = 0; i < len; i++)
{
if(guessed == word[i])
{
*(a + i) = guessed;
*found = TRUE;
}
else
{
if(*(a + i) >= 65 && *(a + i) <= 90 ) ;
else if(*(a + i) >= 97 && *(a + i) <= 122) ;
else *(a + i) = '_';
}
}
for(i = 0; i < len; i++)
{
printf("%c ", *(a + i));
}
if(strcmp(a, word) == 0)
{
printf(" You did it. Well done. ");
exit(0);
}
return;
}
int main()
{
char *hanged[]={
"|=====| "
" | "
" | "
" | "
" === ",
"|=====| "
"O | "
" | "
" | "
" === ",
"|=====| "
"O | "
"| | "
" | "
" === ",
"|=====| "
"O | "
"| | "
"| | "
" === ",
" |=====| "
" O | "
" | | "
"/ \ | "
" === "
};
char *words[] = {
"APPLE", "BALL", "CAT", "DOG","ELEPHANT","FAN","GATE","HAT", "INK", "JACKLE", "KITE", "LION",
"MAN", "NOSE", "PARROT", "QUEEN", "RAT", "STATE", "TAXI", "UMBRELLA", "VAN", "WATCH", "XYLOPHONE",
"YAK", "ZEBRA"
};
show_heading_rules();
srand((unsigned) time(NULL));
unsigned short chosen_word = rand() % NUM_OF_WORDS;
char guessed;
size_t len = strlen(words[chosen_word]);
size_t i = 0;
unsigned int found = FALSE;
unsigned int chances = 0;
show_word_clue(len);
char *a = (char *)malloc(len + 1);
printf(" Guess a letter: ");
while(1)
{
guessed = toupper(getchar());
if(guessed != ' ')
{
printf(" Guess a letter: ");
see_in_word(guessed, words[chosen_word], a, len, &found);
if(found == FALSE)
{
if(chances >= 4)
{
printf(" %s ", hanged[chances++]);
scold_and_quit();
}
printf(" Too bad . That was wrong ");
printf(" %s ", hanged[chances++]);
}
found = FALSE;
}
}
}