Hangman. Write an interactive program that plays a game of hangman. Sore the wor
ID: 3625363 • Letter: H
Question
Hangman. Write an interactive program that plays a game of hangman. Sore the word to be guessed in a string called word. The program should terminate when either all letters have been guessed correctly (the player wins) or a specified number of incorrect guesses have been made (the computer wins). Hint: Use an array or string called, guessed, to keep track of the solution so far. Initialize all elements of guessed to the '*' symbol. Each time a letter in word is guessed, replace the corresponding '*' in guessed with that letter.Explanation / Answer
please rate - thanks
you didn't say where the word comes from, so create a file with many words in it.
when the program asks enter that file and it will randomly choose a word from that list
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#define MAX_GUESSES 26
#define MAX_INCORRECT_GUESSES 9
#define MAX_WORD_LENGTH 15
#define MAX_WORDS 50
int read_words(char[],char[][MAX_WORD_LENGTH]);
void get_word(char[][MAX_WORD_LENGTH],char[],int);
int get_random(int);
void print_guessed_letters(char[],int);
int good_guess(char[],char);
void printword(char[]);
char getletter();
void checkLetter(char[],int*,char,int * );
int main()
{
FILE *output;
int i,count,used=0,guesses=0,good,tot=0,alreadyused;
char filename[20];
char WordArray[MAX_WORDS][MAX_WORD_LENGTH];
char Word[MAX_WORD_LENGTH];
char word_so_far[MAX_WORD_LENGTH];
char lettersUsed[26];
char guess;
srand(time(0));
printf("This is the game of Hangman ");
printf("I will choose a word, and you will enter characters to ");
printf("try to guess the characters in my word without making ");
printf("more than nine incorrect guesses. If you guess all the letter ");
printf("in my word before making nine incorrect guesses, you win! ");
printf("What file would you like to use for data input: ");
scanf("%s",&filename);
printf(" ");
printf("We will be using the file %s for our word choices today. ");
count=read_words(filename,WordArray);
if(count==-1)
return 1;
get_word(WordArray,Word,count);
printf("Your file had %d words ", count);
//printf("Your *hangman secret word* is: %s ",Word);
used=0;
guesses=0;
count=0;
tot=0;
for(i=0;i<26;i++)
lettersUsed[i]=' ';
for(i=0;i<strlen(Word);i++)
word_so_far[i]='*';
word_so_far[i]='';
while ( getchar() != ' ');
printf(" Your word: ");
printword(word_so_far);
while(guesses<MAX_INCORRECT_GUESSES&&count<strlen(Word)&&tot<MAX_GUESSES)
{guess=getletter();
checkLetter(lettersUsed,&used,guess,&alreadyused);
if(alreadyused==0)
{ guesses++;
good=good_guess(Word,guess);
if(good==1)
{for(i=0;i<strlen(Word);i++)
if(toupper(Word[i])==toupper(guess))
{word_so_far[i]=toupper(guess);
count++;
}
}
}
print_guessed_letters(lettersUsed,used);
printf(" Here is your word so far: ");
printword(word_so_far);
}
if(guesses>=MAX_INCORRECT_GUESSES||tot>=MAX_GUESSES)
printf("Sorry! You lost ");
else
printf("Congratulations! You win! ");
printf("The word was: ");
printword(Word);
getchar();
system("pause");
}
void checkLetter(char lets[],int* used,char c,int * alreadyused)
{int i;
*alreadyused=0;
for(i=0;i<*used;i++)
if(c==lets[i])
{*alreadyused=1;
printf("You've already guessed that letter ");
i=*used+10;
}
if(*alreadyused==0)
{lets[*used]=c;
*used=*used+1;
}
}
char getletter()
{char letter;
printf("Enter a new letter: ");
scanf("%c",&letter);
while( (fgetc( stdin )) != ' ' );
return letter;
}
void printword(char w[])
{int i;
for(i=0;i<strlen(w);i++)
printf("%c",w[i]);
printf(" ");
}
int good_guess(char w[],char let)
{int i;
for(i=0;i<strlen(w);i++)
if(toupper(let)==toupper(w[i]))
{printf("Good guess! My word contains your letter! ");
return 1;
}
printf("Sorry! That letter is not in the chosen word! ");
return 0;
}
void print_guessed_letters(char letters[],int n)
{int i;
printf("The letters you have guessed so far are: ");
for(i=0;i<n;i++)
{putchar(letters[i]);
printf(" ");
}
printf(" ");
}
void get_word(char a[][MAX_WORD_LENGTH],char w[],int count)
{
int n,i;
n=get_random(count);
for(i=0;i<MAX_WORD_LENGTH;i++)
w[i]=a[n][i];
return;
}
int get_random(int num_words)
{
return(rand() % num_words);
}
int read_words(char filename[],char a[][MAX_WORD_LENGTH])
{
int i,j,count=0,k;
FILE *input;
input= fopen(filename,"r");
if(input==NULL)
{
printf("Error opening Input File! ");
getchar();
return -1;
}
i=0;
while(i<MAX_WORDS)
{
j=0;
k=0;
while(j<MAX_WORD_LENGTH)
{
a[i][j]=fgetc(input);
k=1;
if(a[i][j]==EOF)
k=0;
if(a[i][j]==' '||k==0||a[i][j]==' ')
{
a[i][j]='';
if(j!=0)
count++;
j=MAX_WORDS;
}
j++;
if(k==0)
i=MAX_WORDS;
}
i++;
}
fclose(input);
return count;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.