Question

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 written. It takes a Game structure pointer as input, and continues playing the game until it's over - either too many wrong guesses (MAX_WRONG_GUESSES) or the player has won. The general algorithm is exactly what you would expect for a game of hangman:

While the game isn't over (either too many wrong guesses or player has won):

display the game board (function is already written so just call it. (int display_game ( Game *g ))

read a guess from the user (function is already written so just call it. (char read_guess(unsigned char guesses[]))

add the guess to the guesses array (function is already written so just call it. (int add_guess(char guess, unsigned char guesses[26]))

check if the guess is correct or not (function is already written so just call it. (int check_guess ( char word[], char guess ))

If it hasn't been guessed, add one to wrong_guesses (no function for this, but I'm sure you'll work it out)

Check if the player has won (function is already written so just call it. (int check_win ( Game *g ))

Once loop has finished, you can see if the player won or lost (check wrong_guesses) and display an appropriately mocking message. Return 1 if they won, 0 if they lost. Game over.

   // Data that is in g (Game)

    g->wrong_guesses

    g->guesses

    g->hidden_word

    int play_game ( Game *g )

{

    // while the game isn't over (player has won or player has lost) (MAX_WRONG_GUESSES is 8)

        // display the game

        // get a guess from player

        // add that guess to guesses array

        // check if guess is in the word or not, if it isn't

            // add 1 to wrong guesses

        // check if player has won

    // if they won, reutrn 1, else return 0

}

The exist code

/* Hangman game!

Author: 1305ENG students
Date: 13/7/2020

A simple hangman game
*/

#include
#include
#include
#include
#include
#include
// include all OUR function declarations and constants
#include "hangman.h"


// The main function!
int main( int argc, char **argv )
{
char wordfile[256], option, temp[256] ;
char wordlist[MAX_WORDS][MAX_WORD_LENGTH] ;
int num_words, result ;
Game g ;

// seed the rand() function first
srand ( time(NULL));

// check to see if command line argument was given, if so use it as the filename for the words
if ( argc > 1 ){
strcpy ( wordfile, argv[1] ) ;
} else {
strcpy ( wordfile, "wordlist.txt" ) ;
}

// now read word file
num_words = read_words ( wordfile, wordlist ) ;

if ( num_words == 0 ){
printf ( "No words were read from file, exiting\n") ;
exit ( -1 ) ;
}
printf ( "Read %d words from file\n", num_words ) ;

setup_game ( &g, wordlist, num_words ) ;
result = play_game ( &g ) ;
printf ( "Would you like to play again (y/n)? " ) ;
fgets ( temp, 256, stdin ) ; // read the rest of the line to get rid of it from stdin
while ( option == 'y' || option == 'Y' ) ;
return 0 ;
}

// Functions used in the program here!

// WEEK 1 FUNCTIONS
// Replace the call to the library function with your own code

// draw_man()
// Draws the hangman picture for the specified wrong number of moves.
// There is no need to exactly copy the example program, but it should progress from almost nothing
// at zero wrong moves to the man being "hanged" at MAX_WRONG_GUESSES
int draw_man ( int misses ){
return draw_man_lib(int misses);

}

// display_guesses
// Displays a representation of the guessed letters from the guesses array.
// Each letter is displayed in all caps, separated by a space. If the array is '1', that letter is display,
// otherwise if it is '0' it is not. For example if elements 0, 9, and 19 are '1' and the others are 0, then
// "A J T" would be displayed. No newline should be displayed after the list of letters.
int display_guesses( unsigned char guesses[])
{
printf("Guesses so far:");
for(int i=0;i<26;i++)
{
if(guesses[i]==1)//checking the array of guesses if the its there or not
{
printf("%c",i+65);
}
printf("\t");
}
return 0;
}

// read_guess()
// Reads a guess from the user. Uses the guesses array to make sure that the player has not
// already guessed that letter. If an invalid character is entered or they have already guessed
// the letter, they are asked to continue guessing until they enter a valid input.
// Returns the character that was read.
// Note that it is usually better to read an entire line of text rather than a single character, and taking the first
// character of the line as the input. This way, the entire line is removed from the input buffer and won't interfere
// with future input calls.
char read_guess(unsigned char guesses[])
{
return read_guess_lib(guesses);
}
//Week 2 Functions

// add_guess()
// Adds the given guess to the guesses array, making the relevant entry 1. For exmpale, if guess is 'a' or 'A',
// element 0 of the guesses array is set to 1. If 'z' or 'Z' is input, then element 25 is set to 1. Your function
// should check that the character is a valid letter and return -1 if it is not.
// Returns 0 if successful, or -1 if an invalid character is entered.
int add_guess(char guess, unsigned char guesses[26])
{
if ((guess >= 'a' && guess <= 'z') || (guess >= 'A' && guess <= 'Z'))
{
if (guess >= 'a' && guess <= 'z')
{
guesses[guess - 'a'] = 1;
}
else
{
guesses[guess - 'A'] = 1;
}
return 0;
}
return -1;
}
// check_guess()
// Checks if the given character 'guess' is contained within the string 'word'.
// Returns 1 if it is, 0 otherwise
int check_guess ( char word[], char guess )
{
int i;
while(word[i] != '\0')
{
if(guess == word[i])
return 1;
}
return 0;
}
// hidden_word()

// Creates the word that is displayed to the user, with all the correctly guessed letters
// shown, and the rest displayed as underscores. Any non-letters (punctuation, etc) are displayed.
// The function takes two strings as inputs. word[] is the word that the player is trying to guess,
// and display_word[] is the output string to be displayed to the player. The guesses array is a binary
// array of size 26 indicating whether each letter (a-z) has been guessed yet or not.
// Returns 0 if successful, -1 otherwise.
int hidden_word ( char display_word[], char word[], unsigned char guesses[])
{

return hidden_word_lib (display_word, word, guesses);
}
// WEEK 3 FUNCTIONS

// read_words()
// takes a filename as input as well as the wordlist to populate
// Reads from the give file and stores each word (line of text) into a new row of the array.
// A maximum of MAX_WORDS is read, and the wordlist array should be this big (at least).
// Each word read from the file should be a maximum of MAX_wORD_LENGTH characters long.
// Returns the total number of words read. If the file cannot be opened, 0 is returned.
int read_words ( char input[], char wordlist[][MAX_WORD_LENGTH])
{
int count= 0;
FILE*fp = fopen(input, "r");
if(fp == NULL)
return 0;

char buf[MAX_WORD_LENGTH];
while (fscanf(fp, "%s", buf) != EOF && count < MAX_WORDS)
{
strcpy(wordlist[count], buf);
count++;
}
fclose(fp);
return count;
}

// display_game()
// Displays the entire game to the screen. This includes the picture of the hangman, the hidden word,
// and the guesses so far.
int display_game ( Game *g )
{
return display_game_lib (g);
}

// WEEK 4-5 FUNCTIONS


// check_win()
// Checks to see whether all letters in the word have been guessed already.
// The hidden word and guesses inside the Game structure should be used to check this
// Returns 1 if so, 0 otherwise
int check_win ( Game *g ){

return check_win_lib ( g ) ;
}

// setup_game()
// Initialises the given game structure by chooseing a random word from the supplied wordlist.
// The number of words in the wordlist is also passed to the function.
// As well as doing this, the number of incorrect guesses is set to 0, and the guesses array is
// initialised to be all zeros.
int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords ){

return setup_game_lib ( g, wordlist, numwords ) ;

}

// play_game()
// Runs one complete game of hangman with the supplied Game structure.
// The usual hangman rules are followed - the player guesses letters one at a time until either
// the entire word is guessed or the maximum number of incorrect guesses MAX_WRONG_GUESSES is
// reached. If the player wins, 1 is returned, otherwise 0.
int play_game ( Game *g ){

return play_game_lib ( g );
}

How to check if your code works?

https://drive.google.com/drive/folders/1oPdKuqbkSMmncbI8SM-K2_yEzFKuV7g4?usp=sharing

The link above is a google drive which contains the program files

download the header (.h) file, C template file (hangman.c), and appropriate library file for your operating system/compiler. The wordlist.txt file should also be downloaded if you don't want to make your own.

If you are using VirtualBox and Linux Mint, please use the Ubuntu object files also.

For Ubuntu, MacOS (in Terminal), Raspberry Pi, etc, you should use the following command to compile (replace YOUROS with the correct version of the object file for your OS):

gcc hangman.c hangman_lib_YOUROS.o -o hangman

After you compiled the files and checked that the game runs without any problems. Then replace the call to Library [return play_game_lib ( g )] in the play_game function with the code you created. If the code works, then the game should run smoothly.

Thank you!

0 0
Add a comment Improve this question Transcribed image text
Request Professional Answer

Request Answer!

We need at least 10 more requests to produce the answer.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the answer will be notified once they are available.
Know the answer?
Add Answer to:
For a C program hangman game: Create the function int play_game [play_game ( Game *g )]...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • 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...

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

  • I need help with this. I need to create the hangman game using char arrays. I've...

    I need help with this. I need to create the hangman game using char arrays. I've been provided with the three following classes to complete it. I have no idea where to start. HELP!! 1. /** * This class contains all of the logic of the hangman game. * Carefully review the comments to see where you must insert * code. * */ public class HangmanGame { private final Integer MAX_GUESSES = 8; private static HangmanLexicon lexicon = new HangmanLexicon();...

  • Need help with this C program? I cannot get it to compile. I have to use...

    Need help with this C program? I cannot get it to compile. I have to use Microsoft Studio compiler for my course. It's a word game style program and I can't figure out where the issue is. \* Program *\ // Michael Paul Laessig, 07 / 17 / 2019. /*COP2220 Second Large Program (LargeProg2.c).*/ #define _CRT_SECURE_NO_DEPRECATE //Include the following libraries in the preprocessor directives: stdio.h, string.h, ctype.h #include <stdio.h> /*printf, scanf definitions*/ #include <string.h> /*stings definitions*/ #include <ctype.h> /*toupper, tolower...

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

  • Help please, write code in c++. The assignment is about making a hangman game. Instructions: This...

    Help please, write code in c++. The assignment is about making a hangman game. Instructions: This program is part 1 of a larger program. Eventually, it will be a complete Hangman game. For this part, the program will Prompt the user for a game number, Read a specific word from a file, Loop through and display each stage of the hangman character I recommend using a counter while loop and letting the counter be the number of wrong guesses. This...

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

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

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

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