assignment purpose: User defined functions, character arrays, c style string mem
ID: 3567234 • Letter: A
Question
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:
Declare and use three character arrays:
o one for the file_word
o one for the guess
o one for all of the used_letters
Declare additional variables as needed
Preload the arrays
o file_word array - start with an empty string.
o guess array should be adjusted to the correct length (that is the same size as the
o used_letters array should be empty a character is added with each guess Read a word from the file hangwords.txt and store it in the file_word array. word read from the file but consists only of * (asterisks) in the beginning An integer for total number of guesses keeps track of how many characters are in this 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 guess and the list of used_letters If any letter is correctly guessed the player is notified and the letter appears in its proper location in the guess array. 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 for used_letters/ Each time the user is asked for a letter, the array containing the guess is displayed with * replacing any letters that have not yet been guessed. The list of used_letters is also displayed each time the user is asked for a letter. 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 whichever occurs first. 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)
COP2220 Hangman Helpful Hints A word is guessed correctly when all of the characters in file_word match the guess. You do not have to be concerned with correct spelling, or if the input from the user belongs to the alphabet. Only letters from a to z and from A to Z will be used. 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 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. o char tolower(char x) takes the original letter and returns the lowercase equivalent Assume all input words from the file have lower case letters Assume all words to be guessed will have single instances of each character (i.e words like apple, hello, goodbye will not be tested use words like: word, today, period)
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 determines if the player would like to play again.
o A function that determines if the second array matches the first array.
o A function that tells the user if they won or lost
o A function that returns the index of the letter that the user has guessed or something else if
o A function that displays the instructions on how to play the game. o A function that displays the contents of a character array (not a string).
o Two additional functions of your own choice.
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. the letter is not in the word.
This is the code needed to create the file that they take words from. We also need to use tolower in order to make everything the same case:
FILE * outptr; outptr = fopen("hangwords.txt", "w");
fprintf(outptr, "kind ");
fprintf(outptr, "dOuBLe ");
fprintf(outptr,"prInt ");
fprintf(outptr, "Extra ");
fprintf(outptr, "woRD ");
fclose(outptr);
return 0;
Explanation / Answer
#include #include typedef struct { char name[10]; int score; } player; void error_m(int x); void menu(); char choice(); void game(player *p); void highestscores(); void getusername(player *p); int main() { int choice; player play; while(choice!=3) { menu(); scanf("%d",&choice); switch(choice){ case 1: getusername(&play); game(&play); break; case 2: highestscores(); break; case 3: printf("Good Bye!"); } } } void menu() { printf(" WELCOME TO HANGMAN!!! "); printf("Please choose a number "); printf("1. Play a new game "); printf("2. See highest score "); printf("3. Exit "); } void getusername(player *play){ printf("Please enter your name: "); scanf("%s", &play->name); } void game(player *play) { char category[20]; char word[20]; FILE *hmPtr; if ( (hmPtr = fopen( "hangman.dat", "r" )) == NULL ) { printf( "File could not be opened " ); } else { int i = 0; srand ( time(NULL) ); int j = rand() % 4; while(!feof(hmPtr)) { fscanf(hmPtr,"%s %s",category, word); if (i == j) { int turns=0; int x; char input[1]; char get[20]; char print[20]; strcpy(get,word); printf("Category: %s ", category); word[strlen(word)]; for(x=0; xRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.