Hangman Project (Please Answer in C, not C++) Topics: User defined functions, ch
ID: 3748157 • Letter: H
Question
Hangman Project (Please Answer in C, not C++)
Topics: User defined functions, character arrays, C style string member functions. Make a figure pop up for the HANGMAN, after each wrong attempt.
Synopsis: You will write an interactive C program that will allow a user to play the Hangman game.
How to play the game:
Allow the user to guess letters one character at a time
At every stage of the game, the user should be able to view the current state of the “word in progress” (star word) and the list of “guessed letters”
If a letter is correctly guessed the player is notified and the letter appears in its proper location in the “star word” array
If a letter is not correct, the number of guesses is increased
In ALL cases, the guessed letter should be added to the array for “guessed letters”
The user is allowed up to 6 incorrect guesses
The round of the game will end when the user has either guessed the word correctly or has made 6 incorrect letter guesses
If the player guesses the word correctly, the player is notified that they have won and the correct word is displayed
If the player does not get the correct word, the player is notified that they have lost and the correct word is displayed
When the game is over, the user should be allowed to play again without having to execute the program again. (The player will play again with the next word in the file)
Instructions:
You will read the word to be guessed from a file “words.txt” which should contain upper and lower case words
You will use 4 character arrays:
1. one for the “word to be guessed” (which comes from the input file)
2. one for the “star word” (same length as the word to be guessed)
(remember to add the NULL character : ‘’)
3. one for all of the “guessed letters”
(remember to add the NULL character : ‘’
4. one for the “user’s guess”
Declare any additional variables needed • At the beginning of each game:
*word to be guessed array - start with an empty string*
Read a word from the file and store it in the word to be guessed array
word in progress array should be adjusted to the correct length (that is the same size as the word read from the file but consists only of * (asterisks) in the beginning (star word)
guessed letters array should be empty – a character is added with each guess
An integer will keep track of how many characters are in the array
You must have at least 8 user-defined functions with meaningful names that perform the following tasks:
A function that displays the rules/instructions on how to play the game
A function that determines if the player would like to play again (y or n)
A function that plays one round of the game
A function that turns any character array into all upper or lower case letters
A function that creates the star word (array of ‘*’ the same size as the word)
A function that gets the users letter guess and adds it to the guessed letters array
A function that searches the word and replaces any asterisks in the starword with the guess character
A function that gets a guess word from the user, compares it to the solution and tells the user if they won
You must use function prototypes placed before the main function and all function definitions must follow the main function.
All function prototypes and definitions must have a comment describing what the function does.
Additional Requirements:
Do NOT use global variables
Do NOT use break statements to stop loops
Indent your code properly (refer to the textbook and sample code)
Place adequate comments throughout your code
Use meaningful variable names
Helpful Hints
Remember that the C language is case sensitive. For this program a = = A. In order to do this, you will need to convert all input letters to upper or lowercase either before or during the letter matching process. You will need to have a preprocessor directive to include the library file ctype.h in order to access these functions.
Be sure to add the “words.txt” file to your project
A word is guessed correctly when all of the characters in “word to be guessed” match the “word in progress”
All letters and words should be changed to either upper or lower case
char toupper(char x) – takes the original letter and returns the lowercase equivalent
DO NOT assume all input words from the file have upper case letters
Sample Output:
WELCOME TO HANGMAN!
-You will be presented with a word to be guessed -Guess letters one at a time
-You can have up to six incorrect letter guesses
-You can only guess the word when you have made a correct letter guess
-The game will be OVER when you have guessed the word correctly
Or when you have guessed letters incorrectly SIX times.
HAVE FUN!
Here are the letters guessed so far:
*********
Guess a letter: n
-------------------------------
The letter was in the word, you can now guess the word: *N*******
Guess the word: answering
That is not the correct word
Here are the letters guessed so far: N
*N*******
Guess a letter: w
The letter was not in the word, the value of wrongGuesses is now 1
Here are the letters guessed so far: NW
*N*******
Guess a letter: e
-------------------------------
The letter was in the word, you can now guess the word:
*N**E****
Guess the word: understand
That is not the correct word
Here are the letters guessed so far: NWE
*N**E****
Guess a letter: u
-------------------------------
The letter was in the word, you can now guess the word:
UN**E****
Guess the word: universal
You won that round, congratuations!
Would you like to play another round (y or n)? y
Here are the letters guessed so far:
********
Guess a letter: r
-------------------------------
The letter was in the word, you can now guess the word:
*******R
Guess the word: forever
That is not the correct word
Here are the letters guessed so far: R
*******R
Guess a letter: e
-------------------------------
The letter was in the word, you can now guess the word:
E****EER
Guess the word: engineer
You won that round, congratuations!
Would you like to play another round (y or n)? y
Here are the letters guessed so far:
*****
Guess a letter: o
The letter was not in the word, the value of wrongGuesses is now 1
Here are the letters guessed so far: O
*****
Guess a letter: p
The letter was not in the word, the value of wrongGuesses is now 2
Here are the letters guessed so far: OP
*****
Guess a letter: r
-------------------------------
The letter was in the word, you can now guess the word:
R***R
Guess the word: rover
That is not the correct word
Here are the letters guessed so far: OPR
R***R
Guess a letter: i
The letter was not in the word, the value of wrongGuesses is now 3
Here are the letters guessed so far: OPRI
R***R
Guess a letter: e
The letter was not in the word, the value of wrongGuesses is now 4
Here are the letters guessed so far: OPRIE
R***R
Guess a letter: u
The letter was not in the word, the value of wrongGuesses is now 5
Here are the letters guessed so far: OPRIEU
R***R
Guess a letter: x
The letter was not in the word, the value of wrongGuesses is now 6
You did not win this round, the solution was RADAR
Would you like to play another round (y or n)? n
Press any key to continue
Additional Instructions
1.Create a project and name the source code hangman.c
2.Add the function prototype and implement the function definition for the Hangman instructions function
3.Add the function call in the main function *** build run and test
4.Create and implement the (y or n) loop in the main function, it is suggested to use a do/while loop
5.Add the function prototype and implement the function definition for the function that determines if the player wants to play another round
6.Add the function call
*** build run and test
7.Go to the assignment and save “words.txt” into the same directory as hangman.c
8.Declare a file pointer variable, connect to the input file and use fscanf to read a word from the file
9.Remember to add the space in fscanf for %s
10.Use printf statements to see the word from the file print onto the screen (inside the loop)
*** build run and test - Each time you enter y a new word will be read from the file and printed on the screen
11. Add the function prototype and implement the function definition for the function that changes a character array to all one case (upper or lower) Use the toupper or tolower function (from ctype.h) to change each letter in the word to upper or lowercase (Hint: you need to loop thru the array character by character)
a. word[i] = toupper(word[i]); //example
*** build run and test (use printf to test)
12.Now it is time to create the Play One Round function and pass the solution word to this function, make the function prototype, prepare the definition and call the function from inside the do/while loop in main
13.Declare 2 character arrays inside the Play One Round of the game function (starword, lettersGuessed) initialize lettersGuessed to the empty string (“”).
14.Add the function prototype and implement the function definition for the create starword function, pass the empty star word and the number of letters in the solution word to this function, you need to use a loop to assign ‘*’ to each character in the array, do not forget to add the ‘’ null character
*** build run and test
15.There will be a loop inside the Play one round function that will continue until the user has used up 6 (MAXGUESSES) or guesses correctly An entire round of the game:
i.Present the starword
ii.Present the letters guessed so far
iii.Get a character guess
iv.Check if the character is in the word
v.If it is in the word let the user enter a word guess (userGuess) vi.Check if the userGuess matches the solution if yes the user wins if not go back to (i)
16.Implement (i), (ii) by adding printf statements
*** build run and test
17. Add the function prototype and implement the function definition for the function that will get a letter guess from the user and add that letter to the lettersGuessed array. This function will take 2 arguments, the lettersGuessed array and the integer that keeps track of the number of letters that have been entered by the user. Remember to move the null character ‘’ and to also add 1 to the number of letters in that array. This can be done by passing the “address of” the number to the function, this function should return the letter entered by the user. Remember to change the case of the letter to match your solution word.
Hint for the null character:
lettersGuessed[numGuess] = letter; numGuess++;
lettersGuessed[numGuess] = '';
*** build run and test –you should be filling the array with letters and displaying the array Sample output at this point:
WELCOME TO HANGMAN!
Please read the following instructions before you play.
-You will be presented with a word to be guessed
-Guess letters one at a time
-You can have up to six incorrect letter guesses
-You can only guess the word when you have made a correct letter guess
-The game will be OVER when you have guessed the word correctly Or when you have guessed letters incorrectly SIX times.
HAVE FUN!
The solution is universal
The solution in all uppercase letters UNIVERSAL
Here are the letters guessed so far:
*********
Guess a letter: a
Here are the letters guessed so far: A
*********
Guess a letter: d
Here are the letters guessed so far: AD
*********
Guess a letter: u
Here are the letters guessed so far: ADU
*********
Guess a letter: f
Here are the letters guessed so far: ADUF
*********
Guess a letter: g
Here are the letters guessed so far: ADUFG
*********
Guess a letter:
18. Add the function prototype and implement the function definition for the function that will check if the letter is in the solution word, and if it is replace the asterisk(s) it in the starword, with that letter. A loop will be needed to search through the array. The arguments to this function should be the solution word, the starword, and the letter that was returned from step 17. This function should return a value to indicate whether or not a correct letter was guessed. Call this function. The number returned will determine whether the user gets to guess the word or 1 will be added to the number of wrongGuesses.
*** build run and test
19. Add the function prototype and implement the function definition for the function that will get the guess word from the user, compare it to the solution word, and let the user know if they have won the game. Remember to change the word that the user enters to the same case (upper or lower) as the solution word. Use the function you already created earlier. Call the function when the user makes a correct letter guess and pass it to the solution word.
Explanation / Answer
#include<stdio.h> /*Header file declaration*/
#include<string.h> /*<string.h> for strcmp();,strlen(); functions use*/
#include<stdlib.h>
void showHangman(int);
int main(void)
{
char hangmanWord[100], tempWord[100]; /**hangmanWord[] array for the original word and tempWord[] array to get the alphabet from user and compare it with original word**/
char hangmanOutput[100]; /**This array will show the remaining blanks and correct inputs**/
int wrongTry = 6 , matchFound = 0; /**player will get 5 chance, so we use wrongTry as chance counter**/
/**matchFound to search the alphabet, if the alphabet from user does not exist
in the original word it will remain 0, upon finding the word, matchFound will
be set as 1**/
int counter = 0 , position = 0, winner, length , i;
char alphabetFromUser;
system("cls"); /**for clearing the screen**/
printf(" Enter any word in small case and hit >>ENTER<<");
printf(" Enter HERE ==> ");
scanf("%s",hangmanWord); /**get the string from opponent**/
printf(" Now give the COMPUTER to your friend and see if he/she can CRACK it!!!");
printf(" HIT >>ENTER<<");
getchar(); /**hold the computer screen**/
length = strlen(hangmanWord); /**get the length of the word**/
system("cls");
printf(" !!!!!!!!!!!!!!!!!!!Welcome to the HANGMAN GAME!!!!!!!!!!!!!!!!! "); /**Brief description of the game**/
printf(" You will get 5 chances to guess the right word");
printf(" So help the Man and get...set...GO..!!");
getchar();
printf(" HIT >>ENTER<< ");
getchar();
system("cls");
printf(" ||===== "); /**show the HANGMAN**/
printf(" || | ");
printf(" || ");
printf(" || ");
printf(" || ");
printf(" || ");
printf(" The word has %d alphabets ",length); /**tell the user how many alphabets the word has**/
for( i = 0; i < length ; i++)
{
hangmanOutput[i] = '_';
hangmanOutput[length] = '';
}
for(i = 0 ; i < length ; i++)
{
printf(" ");
printf("%c",hangmanOutput[i]); /**Show the Word With n(length of the original word) number of underscores (_)**/
}
while(wrongTry != 0) /**while loop for exiting the program when no try left**/
{
matchFound = 0;
printf(" enter any alphabet from a to z and please use small case!!");
printf(" Enter HERE ==> ");
fflush(stdin);
scanf("%c",&alphabetFromUser); /**get alphabet from user**/
if(alphabetFromUser < 'a' || alphabetFromUser > 'z') /**In case player gives input other than 'a' to 'z' the console will ask again**/
{
system("cls");
printf(" Wrong input TRY AGAIN ");
matchFound = 2;
}
fflush(stdin);
if (matchFound != 2)
{
for(counter=0;counter<length;counter++) /**for loop to check whether player input alphabet exists or not in the word**/
{
if(alphabetFromUser==hangmanWord[counter])
{
matchFound = 1;
}//end of if()
}//end of for()
if(matchFound == 0) /**in case of wrong guess**/
{
printf(" :( You have %d tries left ",--wrongTry);
getchar();
showHangman(wrongTry);
getchar();
}//end of if()
else
{
for(counter = 0; counter < length; counter++)
{
matchFound = 0;
if(alphabetFromUser == hangmanWord[counter])
{
position = counter ;
matchFound = 1;
}//end of if
if(matchFound == 1)
{
for(i = 0 ; i < length ; i++)
{
if( i == position)
{
hangmanOutput[i] = alphabetFromUser; /**Put the alphabet at right position**/
}
else if( hangmanOutput[i] >= 'a' && hangmanOutput[i] <= 'z' ) /** If the position already occupied
by same alphabet then no need to
fill again EASY!! and continue */
{
continue;
}
else
{
hangmanOutput[i] = '_'; /** Put a blank at not guessed alphabet position **/
}
}
tempWord[position] = alphabetFromUser; /**put the alphabet in another char array to check with the original word**/
tempWord[length] = ''; /**put the NULL character at the end of the temp string**/
winner = strcmp(tempWord,hangmanWord); /**upon True comparison it will return 0**/
if(winner == 0) /**if the player guessed the whole word right then he/she is the WINNER**/
{
printf(" YAHOO!!!!! You are the WINNER !!!!!");
printf(" The Word was %s ",hangmanWord);
printf(" EASY HUH??? ");
getchar();
return 0;
}//end of inner if
}//end of outer if
}//end of for loop
}//end of else
}// end of if(matchFound != 2) condition
printf(" ");
for(i = 0 ; i < length ; i++)
{
printf(" ");
printf("%c",hangmanOutput[i]); /**Show the original Word With blanks and right Input alphabet**/
}
getchar();
}//end of while loop
if(wrongTry <= 0) /**if the player can not guess the whole word in 5 chaces**/
{
printf(" The Word was %s ",hangmanWord);
printf(" The man is dead you IDIOT!!!!!");
printf(" Better luck next!!!");
}
getchar();
return 0;
}//end of main();
void showHangman(int choice) /**This function show the hangman after each wrong try**/
{
switch(choice)
{
case 0:
system("cls");
printf(" ||===== ");
printf(" || | ");
printf(" || %cO/",'\');
printf(" || | ");
printf(" || / %c",'\');
printf(" || ");
break;
case 1:
system("cls");
printf(" ||===== ");
printf(" || | ");
printf(" || %cO/",'\');
printf(" || | ");
printf(" || %c",'\');
printf(" || ");
break;
case 2:
system("cls");
printf(" ||===== ");
printf(" || | ");
printf(" || %cO/",'\');
printf(" || | ");
printf(" || ");
printf(" || ");
break;
case 3:
system("cls");
printf(" ||===== ");
printf(" || | ");
printf(" || %cO/",'\');
printf(" || ");
printf(" || ");
printf(" || ");
break;
case 4:
system("cls");
printf(" ||===== ");
printf(" || | ");
printf(" || %cO ",'\');
printf(" || ");
printf(" || ");
printf(" || ");
break;
case 5:
system("cls");
printf(" ||===== ");
printf(" || | ");
printf(" || O ");
printf(" || ");
printf(" || ");
printf(" || ");
break;
}//end of switch-case
return;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.