Write a Program in C. The program name is Hangman Assignment purpose: User defin
ID: 3707617 • Letter: W
Question
Write a Program in C. The program name is Hangman
Assignment purpose: User defined functions, character arrays, c style string member functions
Write an interactive program that will allow a user to play the game of Hangman.
You will need to use four character arrays:
o one for the word to be guessed (solution)
o one for the word in progress (starword)
o one for all of the guessed letters (letters)
o one for the users word guess (guess)
Declare additional variables as needed.
At the beginning of each game:
o 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.
o 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 (starword)
o 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
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 (starword) and the list of guessed letters (letters)
? If any letter is correctly guessed the player is notified and the letter appears in its proper location in the word in progress array (starword), this array is displayed again with the letter(s) in place and the user can input a word
? If a letter is not correct, the number of used guesses is increased.
? In ALL cases, the guessed letter should be added to the array of guessed letters
? The user is allowed up to 6 incorrect guesses.
? The game is over when the word is guessed correctly or the six incorrect guesses are used up.
? 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)
Other Requirements
? You must have meaningful variable and function names.
? You must use consistent and proper indentation
? You must use sufficient comments that tell a programmer what is happening in the program
? You must have at least 8 user-defined functions with meaningful names that perform the specified tasks:
o A function that displays the instructions/ rules on how to play the game.
o A function that determines if the player would like to play again (y or n)
o A function that plays one round of the game
o A function that changes a character array to all upper case or all lower case letters
o A function that creates the “starword” , an array of ‘*’ the same size as the solution word
o A function that gets a character from the user and adds that character to an array of all the letter that have been guessed
o A function that searches the solution word and replaces any asterisks in the starword with the character
o 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.
Function Proto-type:
//this function provides instructions to the user on how to play the game
void HangmanRules();
//this function asks the user if they want to play another game (y or n)
void PlayAgain(char *againPtr);
//this function runs one game.
//input: character from the file, void return type
//all other functions to Play one round of a game
//are called from within the PlayOneGame function
void PlayOneGame(char solution[]);
//this function changes a character array to all uppercase letters
void UpperCaseWord(char word[]);
//this function creates the starword array
void CreateStarword(char starword[], int length);
//this function gets the users guess letter and adds it to the lettersGuessed array
//returns the letter entered by the user
char GetTheLetterGuess(char lettersGuessed[], int *numPtr);
//this function replaces any asterik in the starword with the current character enteed by the user
int ReplaceStars(char solution[], char starword[], char letter);
//this function gets the guess word,
//compares the solution and the guess word
//tells the user if they have won and returns a 1
//otherwise it returns a 0
int DidYouWin(char solution[]);
The file name below.
word.txt:
universal
engineer
radar
Trouble
apple
KEYS
extra
kind
Sample Output:
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!
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...........
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");
printf("WELCOME TO HANGMAN! ");
printf("Please read the following instructions before you play. ");
printf("- You will be presented with a word to be guessed ");
printf("- Guess letters one at a time ");
printf("- You can have up to six incorrect letter guesses ");
printf("- You can only guess the word when you have made a correct letpter guess ");
printf("- The game will be OVER when you have guessed the word correctly ");
printf("Or when you have guessed letters incorrectly SIX times. ");
printf("HAVE FUN! ");
/**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(" DO YOU WANT TO CONTINUE??? ");
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 !!!!");
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
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.