Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please, use C Create a Hangman game. The following are the requirements for your

ID: 3764641 • Letter: P

Question

Please, use C

Create a Hangman game. The following are the requirements for your game. You should read a list often possible words in from a file. Your game should randomly choose one of the words from the list and display the appropriate number of blanks. The user should then be able to play the game. Your game should display the appropriate ASCII art representing the hangman. If the user chooses to play again, he/she should be able to. If the user does not choose to play again, you should display the number of wins/losses during the session. The user should then be able to play the game. Your game should display the appropriate ASCII art representing the hangman. If the user chooses to play again, he/she should be able to. the user does not choose to play again, you should display the number of wins/losses during the session.

Explanation / Answer

working c code compiled on ideone.

I am reading the list of words allowed from words.txt file for the hangman game.

The game has proper hangman art and allows 10 characters try limit.

It also checks on the user input and will give error in case the input is not a character.

C code

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <ctype.h>
#include <conio.h>
#include <time.h>
#include <string.h>
char fileLoc[500];
void startgame() {
printf("Hangman game for the user ");
}
void print_hangman_pattern(int i) {
switch (i) {
case 0 :
printf("Amount of wrong letters: %d ", i);
printf(" ");
printf(" ");
printf(" ");
printf(" ");
printf(" ");
printf(" ");
printf("____________ ");
break;
case 1 :
printf("Amount of wrong letters: %d ", i);
printf(" ");
printf(" | ");
printf(" | ");
printf(" | ");
printf(" | ");
printf(" | ");
printf("__|_________ ");
break;
case 2 :
printf("Amount of wrong letters: %d ", i);
printf(" _______ ");
printf(" | ");
printf(" | ");
printf(" | ");
printf(" | ");
printf(" | ");
printf("__|_________ ");
break;
case 3 :
printf("Amount of wrong letters: %d ", i);
printf(" _______ ");
printf(" |/ ");
printf(" | ");
printf(" | ");
printf(" | ");
printf(" | ");
printf("__|_________ ");
break;
case 4 :
printf("Amount of wrong letters: %d ", i);
printf(" _______ ");
printf(" |/ | ");
printf(" | O ");
printf(" | ");
printf(" | ");
printf(" | ");
printf("__|_________ ");
break;
case 5 :
printf("Amount of wrong letters: %d ", i);
printf(" _______ ");
printf(" |/ | ");
printf(" | O ");
printf(" | | ");
printf(" | | ");
printf(" | ");
printf("__|_________ ");
break;
case 6 :
printf("Amount of wrong letters: %d ", i);
printf(" _______ ");
printf(" |/ | ");
printf(" | O ");
printf(" | \| ");
printf(" | | ");
printf(" | ");
printf("__|_________ ");
break;
case 7 :
printf("Amount of wrong letters: %d ", i);
printf(" _______ ");
printf(" |/ | ");
printf(" | O ");
printf(" | \|/ ");
printf(" | | ");
printf(" | ");
printf("__|_________ ");
break;
case 8 :
printf("Amount of wrong letters: %d ", i);
printf(" _______ ");
printf(" |/ | ");
printf(" | O ");
printf(" | \|/ ");
printf(" | | ");
printf(" | / ");
printf("__|_________ ");
break;
case 9 :
printf("Amount of wrong letters: %d ", i);
printf(" _______ ");
printf(" |/ | ");
printf(" | O ");
printf(" | \|/ ");
printf(" | | ");
printf(" | / \ ");
printf("__|_________ ");
break;
case 10 :
printf("Amount of wrong letters: %d ", i);
printf(" _______ ");
printf(" |/ | ");
printf(" | X ");
printf(" | \|/ ");
printf(" | | ");
printf(" | / \ ");
printf("__|_________ ");
break;
}
}
char randomNumber(int max_number) {
srand(time(NULL));
int g = (rand() % (max_number + 1));
return g;
}
char *getWord() {
char c[50000];
int n;
FILE *file;
if (strcmp(fileLoc, "") != 1) {
file = fopen("words.txt", "r");
} else {
file = fopen(fileLoc, "r");
}
if(file==NULL) {
printf("An error has occured: can't open words file. Please type the location of the words file: ");
scanf("%s", fileLoc);
printf("Reading file '%s'..... ", fileLoc);
file = fopen(fileLoc, "r");
if (file == NULL) {
while (file==NULL) {
printf("That file doesn't exist. Enter the location of the words file: ");
scanf("%s", fileLoc);
printf("Reading file '%s'..... ", fileLoc);
file = fopen(fileLoc, "r");
}
}
printf("File has been read. ");
n = fread(c, 1, 50000, file);
c[n] = '';
} else {
n = fread(c, 1, 50000, file);
c[n] = '';
}
char *token = strtok(c, "|");
char *words[200] = {0};
int f = 0;
while(token != NULL)
{
words[f] = malloc(strlen(token)+1);
strcpy(words[f],token);
token = strtok(NULL, "|");
f++;
}
fclose(file);
int wordN = randomNumber(f);
int q;
for(q = 0; q < 200; q++)
{
if( q != wordN) {
free(words[q]);
}
}
/* Returning string */
return words[wordN];
}
int main(void) {
char user_in[] = "EMPTY";
while ((strcmp(user_in, "END") != 0) && ((strcmp(user_in, "AGAIN") == 0) || (strcmp(user_in, "EMPTY") == 0))) {
strcpy(user_in, "EMPTY");
char *tempWord = getWord();
/* Declaring the guessWord with the length of dkljafoue */
char guessWord[strlen(tempWord)+1];
/* Copying the string of dkljafoue into guessWord */
strcpy(guessWord, tempWord);
/* Freeing the pointer */
free(tempWord);
/* Calculate the length of the guessWord */
int wordlength = strlen(guessWord);
/* Creating the dotword (name: currentWord) */
char* currentWord = malloc( wordlength );
int t;
for (t = 0; t <= wordlength; t++) {
if (t == wordlength) {
currentWord[t] = '';
} else {
currentWord[t] = '.';
}
}
int errors = 0;
int guessedLetter = 0;
int i,n = 1;
char c;
startgame();
printf("you have 10 chances to guess the word in the game.");
printf("%d. %s", n, "Enter the letter(s) you want to guess: ");
while( (strcmp(currentWord, guessWord) != 0) && (errors < 10) ){
scanf("%c", &c); /* Retrieving the user entry */
c = tolower(c); /* Removing caps */
if (c != ' ') {
if (isalpha(c)) {
for (i = 0; i < wordlength; i++) {
if (guessWord[i] == c) {
currentWord[i] = c;
guessedLetter = 1;
}
}
/* Actions taken if the letter c doesn't occur in the guessWord and when it does */
if (guessedLetter == 0) {
errors++;
printf(" That letter was incorrect. ");
} else {
guessedLetter = 0;
printf(" That letter was correct. ");
}
printf("%s%s ", "The word including the letters you guessed: ", currentWord);
print_hangman_pattern(errors);
n++;
if ( (strcmp(currentWord, guessWord) != 0) && (errors < 10) ) {
printf("%d. %s", n, "Enter the letter(s) you want to guess: ");
}
} else {
printf("Only alphanumeric symbols are allowed (a-z, A-Z), try again: ");
}
}
}
printf("--------------- ");
printf("--- Results --- ");
printf("--------------- ");
if (errors < 10) {
if (strcmp(currentWord, guessWord) == 0) {
printf("Congratulations you have guessed the right word! ");
} else {
printf("You have guessed the wrong word, better luck next time! ");
}
} else {
printf("You have guessed the wrong word, better luck next time! ");
}
printf(" Letters guessed wrong: %d The word that needed to be guessed: %s The word you guessed: %s ", errors, guessWord, currentWord);
printf(" Enter 'end' to end the game or enter 'again' to guess another word: ");
while ((strcmp(user_in, "END") != 0) && (strcmp(user_in, "AGAIN") != 0)) {
if (strcmp(user_in, "EMPTY") != 0) {
printf(" It is not allowed to enter anything else than 'again' or 'end', try again: ");
}
scanf("%s", user_in);
int x;
for (x = 0; x < 5; x++) {
user_in[x] = toupper(user_in[x]);
}
}
printf(" -------------------------------------------- ");
}
return 0;
}