Question

Create the game hangman using c code: Program description In the game of hangman, one player...

Create the game hangman using c code:

Program description
In the game of hangman, one player picks a word, and the other player has to try to guess what the word is by selecting one letter at a time. If they select a correct letter, all occurrences of the letter are shown. If no letter shows up, they use up one of their turns. The player is allowed to use no more than 10 incorrect turns to guess what the word is. They can guess the word at any time, but if they guess incorrectly, they lose a turn.

To simulate this game on a computer, the unknown word is stored in a simple text file (which only stores one word). The computer will display the word but with asterisks in place of letters. This lets the player know how many letters are in the word. The player is then asked to guess one of the 26 letters, or to guess what the word is. If they choose a letter, an updated version of the word is shown with that letter filled in, and their number of remaining turns remains fixed. If they make an incorrect guess, they lose a turn.

The program execution should look like the following (user input in bold text):

Give the filename with the unknown word: unknown1.txt

Ready to Start!

The word is: ********

Number of turns remaining: 10
Would you like to guess the word [w] or guess a letter [l]: l
What letter have you chosen?: a

***********************************************

Good choice! The word is: *****a**
Number of turns remaining: 10
Would you like to guess the word [w] or guess a letter [l]: l
What letter have you chosen?: o

***********************************************

Bad Choice The word is: *****a**
Number of turns remaining: 9
Would you like to guess the word [w] or guess a letter [l]: l
What letter have you chosen?: e

***********************************************

Good Choice! The word is: e*e**a**
Number of turns remaining: 9
Would you like to guess the word [w] or guess a letter [l]: l
What letter have you chosen?: t

etc...

***********************************************

Good Choice!
The word is: ele**ant
Number of turns remaining: 3
Would you like to guess the word [w] or guess a letter [l]: w
What is your guessed word?: elephant
Congratulations!

***********************************************

Do you want to play again [y/n]:

Your file should be called hangman.c. The files unknown1.txt and unknown2.txt can be used as sample input words, or you can make up your own.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshot

Program

/*
Program Hangman
Player has to find out secret word
Give a file name with different words line by line
Read word as secret word from file
Player has 2 choice guess a letter or word
Compare letter if it correct add into guess word
Otherwis not
compare guess word with secretword
if matched
Player win
If the number of turns to guess correct answer is 10 when turns reach 10 you lose
*/

//Header files
#include <stdio.h>
#include<stdlib.h>
#include<string.h>

//Function prototype
bool guessLetterChecker(char* secretWord, char* guessWord, char letter);

//Main function
int main()
{
   //Variable declaration
   char filename[100];
   char secretWord[100];
   char guessWord[100];
   char guessSingleWord[100];
   int numTurns, i;
   char ch, guessChoice, guessLetter;
   //Prompt for file name
   printf("Give the filename with the unknown word: ");
   scanf("%s", filename);
   //Set file path
   FILE* filePath;
   filePath = fopen(filename, "r");
   //File open error check
   if (filePath == NULL) {
       printf("Error!!!File not open\n");
       exit(0);
   }
   //Loop until user want to exit
   do {
       //Read secret word from file
       fscanf(filePath, "%s", secretWord);
       //Set guessword with *
       i = 0;
       while (secretWord[i] != '\0') {
           guessWord[i] = '*';
           i++;
       }
       guessWord[i] = '\0';
       //Each time set number of turns 10
       numTurns = 10;
       //Start game with the current secret word
       printf("Ready to Start!\n");
       printf("The word is: %s\n", guessWord);
       //Loop until guess word correctly or turns become 0
       do {
           printf("Number of turns remaining: %d\n", numTurns);
           //Prompt for word or letter guess choice for user
           printf("Would you like to guess the word [w] or guess a letter [l]: ");
           scanf(" %c", &guessChoice);
           //If letter choice
           if (guessChoice == 'l' || guessChoice == 'L') {
               //Prompt for letter
               printf("What letter have you chosen?: ");
               scanf(" %c", &guessLetter);
               printf("***********************************************\n");
               //Check is present or not
               if (guessLetterChecker(secretWord, guessWord, guessLetter)) {
                   printf("Good choice! The word is: %s\n", guessWord);
               }
               else {
                   printf("Bad Choice The word is: %s\n", guessWord);
                   numTurns--;
               }
           }
           //Word guess
           else if (guessChoice == 'w' || guessChoice == 'W') {
               printf("What is your guessed word?: ");
               scanf("%s", guessSingleWord);
               if (strcmp(secretWord, guessSingleWord) != 0) {
                   numTurns--;
               }
           }
           else {
               printf("Bad Choice The word is: %s\n", guessWord);
               numTurns--;
           }
           //Loop repeat condition
       } while (numTurns!=0 && strcmp(secretWord,guessWord)!=0 && strcmp(secretWord, guessSingleWord) != 0);
       //Game result
       if (strcmp(secretWord, guessWord) != 0 && strcmp(secretWord, guessSingleWord) != 0) {
           printf("SORRY!!!YOU LOSE\n");
       }
       else {
           printf("Congratulations!\n");
       }
       printf("***********************************************\n");
       //Next game choice
       printf("Do you want to play again [y/n]: ");
       scanf(" %c", &ch);
   } while (ch == 'Y' || ch=='y');
   //Close file
   fclose(filePath);
   return 0;
}

//Function to check the given letter present in secret word
//if then return true
//Otherwise false
//And also add letter in guessword in proper place
bool guessLetterChecker(char* secretWord, char* guessWord, char letter) {
   int i = 0;
   bool check = false;
   while (secretWord[i] != '\0') {
       if (secretWord[i] == letter) {
           guessWord[i] = letter;
           check = true;
       }
       i++;
   }
   if (check) {
       return true;
   }
   return false;
}

---------------------------------

Output

Ready to Start!
The word is: *****
Number of turns remaining: 10
Would you like to guess the word [w] or guess a letter [l]: l
What letter have you chosen?: a
***********************************************
Bad Choice The word is: *****
Number of turns remaining: 9
Would you like to guess the word [w] or guess a letter [l]: l
What letter have you chosen?: l
***********************************************
Good choice! The word is: **ll*
Number of turns remaining: 9
Would you like to guess the word [w] or guess a letter [l]: o
Bad Choice The word is: **ll*
Number of turns remaining: 8
Would you like to guess the word [w] or guess a letter [l]: l
What letter have you chosen?: o
***********************************************
Good choice! The word is: **llo
Number of turns remaining: 8
Would you like to guess the word [w] or guess a letter [l]: w
What is your guessed word?: hello
Congratulations!
***********************************************
Do you want to play again [y/n]: n

Add a comment
Know the answer?
Add Answer to:
Create the game hangman using c code: Program description In the game of hangman, one player...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Hangman (game) In this project, you are required to implement a program that can play...

    C++ Hangman (game) In this project, you are required to implement a program that can play the hangman game with the user. The hangman game is described in https://en.wikipedia.org/wiki/Hangman_(game) . Your program should support the following functionality: - Randomly select a word from a dictionary -- this dictionary should be stored in an ASCII text file (the format is up to you). The program then provides the information about the number of letters in this word for the user to...

  • Hangman is a game for two or more players. One player thinks of a word, phrase...

    Hangman is a game for two or more players. One player thinks of a word, phrase or sentence and the other tries to guess it by suggesting letters or numbers, within a certain number of guesses. You have to implement this game for Single Player, Where Computer (Your Program) will display a word with all characters hidden, which the player needed to be guessed. You have to maintain a dictionary of Words. One word will be selected by your program....

  • Overview In this exercise you are going to recreate the classic game of hangman. Your program...

    Overview In this exercise you are going to recreate the classic game of hangman. Your program will randomly pick from a pool of words for the user who will guess letters in order to figure out the word. The user will have a limited number of wrong guesses to complete the puzzle or lose the round. Though if the user answers before running out of wrong answers, they win. Requirements Rules The program will use 10 to 15 words as...

  • For a C program hangman game: Create the function int play_game [play_game ( Game *g )]...

    For a C program hangman game: Create the function int play_game [play_game ( Game *g )] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...

  • javafx assisantance confused on where to start Problem Description: (Game: hangman) Write a JavaFX program that...

    javafx assisantance confused on where to start Problem Description: (Game: hangman) Write a JavaFX program that lets a user play the hangman game. The user guesses a word by entering one letter at a time, as shown in Figure followings. If the user misses seven times, a hanging man swings, as shown in Figures. Once a word is finished, the user can press the Enter key to continue to guess another word. Guess a word: ***** God Missed letters Guess...

  • This is for C programming: You will be given files in the following format: n word1...

    This is for C programming: You will be given files in the following format: n word1 word2 word3 word4 The first line of the file will be a single integer value n. The integer n will denote the number of words contained within the file. Use this number to initialize an array. Each word will then appear on the next n lines. You can assume that no word is longer than 30 characters. The game will use these words as...

  • Create a Hangman Game in C++ using Classes. The program doesn't need to have the hangman...

    Create a Hangman Game in C++ using Classes. The program doesn't need to have the hangman drawing, it just needs the following: - Ten 3-letter words - Keep track of everytime the player misses a letter - Simple and commented code

  • JAVA PROGRAMMING (Game: hangman) Write a hangman game that randomly generates a word and prompts the...

    JAVA PROGRAMMING (Game: hangman) Write a hangman game that randomly generates a word and prompts the user to guess one letter at a time, as shown in the sample run. Each letter in the word is displayed as an asterisk. When the user makes a correct guess, the actual letter is then displayed. When the user finishes a word, display the number of misses and ask the user whether to continue to play with another word. Declare an array to...

  • Please help with this Intro to programming in C assignment! Intro to Programming in C-Large Program...

    Please help with this Intro to programming in C assignment! Intro to Programming in C-Large Program 3 - Hangman Game 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: e You will 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...

  • Create a new program, WordGuessingGame. Using a while loop, create a game for the user. The...

    Create a new program, WordGuessingGame. Using a while loop, create a game for the user. The game requires the user to guess a secret word. The game does not end until the user guesses the word. After they win the game, they are prompted to choose to play again. The secret word that user must guess is "valentine". It is a case-sensitive analysis With each guess... If the guess does not have an equal number of characters, tell the user...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT