Question

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 the puzzles for the game of Hangman. You will need to randomly select one of the words from array. Read “Getting a Random Number” for an explanation on how to do this. For those of you that have never played the game of Hangman before, the idea behind the game is to be able to guess all the letters in a puzzle before a certain number of missed guesses. If a player guesses an incorrect letter, then they have one less incorrect guess that they can make. If a player guesses a correct letter, then they can make the next guess without any penalties. In the case of our game, we will allow for 7 missed guesses. With each guess, a letter for the word “HANGMAN” is added to the screen. Once the complete word is spelled out, the player loses. If the player correctly guesses all letters in the puzzle before HANGMAN is spelled, then they win and can either choose to start another game or quit the game. Creating the Game You are required to create three different files: hangman.c, hangman.h, and client.c. The files hangman.c and hangman.h contain the logic for your game, while the file client.c is the main entry point for your program. Upon the start of the game, your terminal should display the following:

Welcome to Hangman! Please enter the name of the file containing your puzzles:

The player will then type the name of a file with their puzzles into the terminal. Afterward, the screen should look similar to the following:

Welcome to Hangman! Please enter the name of the file containing your puzzles: puzzles.txt

Current Puzzle: _ _ _ _ _ _ _

Missed Guesses:

Please guess a letter:

We will use the character ‘_’ to indicate a missing letter. “Missed Guesses” refers to the number of missed guesses that the player has and this is where you will spell out “HANGMAN”. The player is being prompted to enter a possible letter. At this point, one of two things should happen. Either the player guesses a correct letter or they guess an incorrect letter. For simplicity, you may assume that the player will only guess letters and that no other inputs will be entered (this is not a safe assumption in real world applications!).

If they make a correct guess, the screen will look like this:

Current Puzzle: _ _ _ _ _ _ _

Missed Guesses:

Please guess a letter:o

Current Puzzle: _ _ _ _ o o _

Missed Guesses:

Please guess a letter:

If they make an incorrect guess, the screen will look like this:

Current Puzzle: _ _ _ _ _ _ _

Missed Guesses:

Please guess a letter:k

Current Puzzle: _ _ _ _ o o _

Missed Guesses: H

Please guess a letter:

The game will proceed in this way until either they correctly guess all letters in the puzzle or until they spell they word “HANGMAN”. At which time, the game should display an appropriate message and prompt the player to either play again or load a new puzzle file (this means you can create Hangman games based on different themes if you would like):

Current Puzzle: c a r t o o n

Missed Guesses: HANGM

Please guess a letter:

You win!!

Would you like to:

1. Play a new game

2. Load a new file of puzzles

Please make a selection:

If the player chooses to load a new file, the game will prompt the player to enter a new file as they did when first running the program.

The Hangman Files

You will need to create a struct that can hold the array of puzzle words, the number of puzzles currently loaded, the current puzzle word, the guessed word to the current point in time (filled with correctly guessed letters and ‘_’ characters), a boolean (use stdbool.h, refer to references in zyBooks for this if needed) for whether the game is over, a boolean for whether the game is won, and the number of missed guesses thus far. Note that nothing is printed from this file. We are purposefully separating the logic of Hangman from the view that gets displayed to the user. Your file should have the minimum set of functions:

• Hangman createHangmanGame(char *puzzleFile) – Will create the main Hangman game while loading the initial set of puzzles into the struct. You can initialize the words and to NULL and the current number of missed guesses to 0.

• void newHangmanPuzzle(Hangman currentHangmanGame) – Sets up a new puzzle for the player to guess. You will need to randomly select a new puzzle from the array of puzzles in the game.

• void loadPuzzleFile(Hangman currentHangmanGame, char *puzzleFile) – Will free the previous array for puzzles and load a new set of puzzles from the input file.

• bool isPuzzleOver(Hangman currentHangmanGame) – Will return whether or not the current puzzle has reached a state where either the player has won or lost.

• bool isPuzzleSolved(Hangman currentHangmanGame) – Will return whether or not the current puzzle has been solved.

• char* getGuessedWord(Hangman currentHangmanGame) – Will return the string for the current state of the word that has been guessed thus far (the string that contains both the guessed letters and remaining ‘_’).

• bool guessLetter(Hangman currentHangmanGame, char letterToGuess) – Will allow the user to guess a letter and then return whether the guessed letter was part of the puzzle word. This function should update the word guessed thus far or increase the number of missed guesses depending on whether the guess was correct.

• char* getStateOfHangman(Hangman currentHangmanGame) – Will return the string for the current state of “HANGMAN”.

• void freeHangmanGame(Hangman currentHangmanGame) – Will free the memory allocated for the game of Hangman.

Getting a Random Number

You must select a puzzle from your array at random. In C, this can be accomplished by using the rand() function from stdlib.h. It is important to note that before using the random number generator you will need to provide the random number generator with a seed value, this can be done by using the existing time. An example of using the rand() function to pick a random number from 0 – 9 is as follows:

#include <stdlib.h>

int main () {

time_t t;

/* Intializes random number generator */

srand((unsigned) time(&t));

printf("%d\n", rand() % 10); return 0;

}

Note that we have to mod the result of rand by 10 to get a number within the range 0 – 9. You will be using the number of elements in your puzzles array as the mod value in the case of the Hangman game.

The Client

Your client file handles all display messages, running the game, and taking in user input. Separating the client that displays the menus to the player from the game of Hangman allows us to reuse the existing Hangman game in different programs or to change out our current view for a new one if we so choose.

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

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

void showHangman(int);

int main(void)

{

    char hangmanWord[100], tempWord[100];

    char hangmanOutput[100];

    int wrongTry = 6 , matchFound = 0;

    int counter = 0 , position = 0, winner, length , i;

    char alphabetFromUser;

    system("cls");

    printf("\n\n Enter any word in small case and hit >>ENTER<<");

    printf("\n\n\t Enter HERE ==> ");

    scanf("%s",hangmanWord);

    printf("\n\n Now give the COMPUTER to your friend and see if he/she can CRACK it!!!");

    printf("\n\n\tHIT >>ENTER<<");

    getchar();

    length = strlen(hangmanWord);

    system("cls");

    printf("\n\n !!!!!!!!Welcome !!!!!!\n\n\n");   /**Brief description of the game**/

    printf("\n\n You will get 5 chances to guess the right word");

    printf("\n\n So help the Man and get...set...GO..!!");

    getchar();

    printf("\n\n\tHIT >>ENTER<< ");

    getchar();

    system("cls");

        printf("\n\t||===== ");                 /**show the HANGMAN**/

                printf("\n\t||    | ");

        printf("\n\t||      ");

        printf("\n\t||      ");

        printf("\n\t||      ");

        printf("\n\t||      ");

    printf("\n\n     The word has %d alphabets \n\n",length); /**tell the user how many alphabets the word has**/

    for( i = 0; i < length ; i++)

    {

        hangmanOutput[i] = '_';

        hangmanOutput[length] = '\0';

    }

    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)

    {

        matchFound = 0;

        printf("\n\n   enter any alphabet from a to z and please use small case!!");

        printf("\n\n\t Enter HERE ==> ");

                    fflush(stdin);

                    scanf("%c",&alphabetFromUser);

    if(alphabetFromUser < 'a' || alphabetFromUser > 'z') /**In case player gives input other than 'a' to 'z' the console will ask again**/

    {

        system("cls");

        printf("\n\n\t Wrong input TRY AGAIN ");

        matchFound = 2;

    }

    fflush(stdin);

    if (matchFound != 2)

    {

        for(counter=0;counter<length;counter++)

                    {

                                    if(alphabetFromUser==hangmanWord[counter])

                                     {

                                       matchFound = 1;

                     }//end of if()

            }//end of for()

                   if(matchFound == 0)

                    {

                     printf("\n\t :( 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' )

                      {

                          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] = '\0';                    /**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)

                    printf("\n\n\t \t YAHOO!!!!! You are the WINNER !!!!!");

                    printf("\n\n\t The Word was %s ",hangmanWord);

                    printf("\n\n\n\n\t\tEASY HUH???\n\n");

                    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("\n\n\t");

    for(i = 0 ; i < length ; i++)

      {

          printf(" ");

          printf("%c",hangmanOutput[i]);    

}

    getchar();

    }//end of while loop

      if(wrongTry <= 0)                                 /**if the player can not guess the whole word in 5 chaces**/

      {

          printf("\n\n\t The Word was %s ",hangmanWord);

          printf("\n\n\t The man is dead you IDIOT!!!!!");

                      printf("\n\n\t 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("\n\t||===== ");

                printf("\n\t||    | ");

                printf("\n\t||   %cO/",'\\');

                printf("\n\t||    | ");

                printf("\n\t||   / %c",'\\');

                printf("\n\t||      ");

                break;

     case 1:

         system("cls");

                printf("\n\t||===== ");

                printf("\n\t||    | ");

                printf("\n\t||   %cO/",'\\');

                printf("\n\t||   | ");

                printf("\n\t||     %c",'\\');

                printf("\n\t||      ");

                break;

     case 2:

         system("cls");

                printf("\n\t||===== ");

                printf("\n\t||    | ");

                printf("\n\t||   %cO/",'\\');

                printf("\n\t||    | ");

                printf("\n\t||      ");

                printf("\n\t||      ");

                break;

     case 3:

         system("cls");

                printf("\n\t||===== ");

                printf("\n\t||    | ");

                printf("\n\t||   %cO/",'\\');

                printf("\n\t||      ");

                printf("\n\t||      ");

                printf("\n\t||      ");

                break;

     case 4:

         system("cls");

                printf("\n\t||===== ");

                printf("\n\t||    | ");

                printf("\n\t||   %cO ",'\\');

                printf("\n\t||      ");

                printf("\n\t||      ");

                printf("\n\t||      ");

                break;

     case 5:

         system("cls");

                printf("\n\t||===== ");

                printf("\n\t||    | ");

                printf("\n\t||    O ");

                printf("\n\t||      ");

                printf("\n\t||      ");

                printf("\n\t||      ");

                break;

      }

      return;

}

Add a comment
Know the answer?
Add Answer to:
This is for C programming: You will be given files in the following format: n word1...
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
  • 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...

  • 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...

  • C++ programming Submit assignment to Blackboard by the midnight on the due date listed above. (Verify...

    C++ programming Submit assignment to Blackboard by the midnight on the due date listed above. (Verify if your submission is completed.) The only people you are to discuss this project with are myself and the CS Department tutor. Duplicate code, if discovered, will result in a 0 grade. Your source file(s) should follow the Computer Science Coding Standards (available at the Computer Science Homepage) including proper indenting and spacing 1 Overview program, you will play a game of hangman. In...

  • 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...

  • 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...

  • can this code be provided? Project #3 Introduction As you wrap up with JavaScript, let's put...

    can this code be provided? Project #3 Introduction As you wrap up with JavaScript, let's put together everything you've learned to make the classic game "Hangman" in a webpage. If you are unfamiliar with the rules of Hangman, the game works like this: A gallows is drawn on a surface, and Player 1 chooses a word that player 2 must guess. Empty dashes for each letter in the word are drawn next to the gallows (so a 7-letter word means...

  • Note wordBank, an array of 10 strings (char *s). Your program should do the following: 1....

    Note wordBank, an array of 10 strings (char *s). Your program should do the following: 1. Select a word at random from the wordBank. This is done for you. 2. On each turn display the word, with letters not yet guessed showing as *'s, and letters that have been guessed showing in their correct location 3. The user should have 10 attempts (?lives?). Each unsuccessful guess costs one attempt. Successful guesses do NOT count as a turn. 4. You must...

  • 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...

  • Simple JavaScript and HTML: You'll create a simple word guessing game where the user gets infinite...

    Simple JavaScript and HTML: You'll create a simple word guessing game where the user gets infinite tries to guess the word (like Hangman without the hangman, or like Wheel of Fortune without the wheel and fortune). Create two global arrays: one to hold the letters of the word (e.g. 'F', 'O', 'X'), and one to hold the current guessed letters (e.g. it would start with '_', '_', '_' and end with 'F', 'O', 'X'). Write a function called guessLetter that...

  • JAVA Hangman Your game should have a list of at least ten phrases of your choosing.The...

    JAVA Hangman Your game should have a list of at least ten phrases of your choosing.The game chooses a random phrase from the list. This is the phrase the player tries to guess. The phrase is hidden-- all letters are replaced with asterisks. Spaces and punctuation are left unhidden. So if the phrase is "Joe Programmer", the initial partially hidden phrase is: *** ********** The user guesses a letter. All occurrences of the letter in the phrase are replaced in...

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