Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Hangman game! http://en.wikipedia.org/wiki/Hangman_(game ). In your game

ID: 3559329 • Letter: W

Question

Write a Hangman game!
http://en.wikipedia.org/wiki/Hangman_(game).
In your game, computer chooses a word randomly and shows how many letters it has. Then user can guess
the letters in the word, user have 6 chances for wrong guesses. With every wrong guess, the picture of man
being hanged will be completed. Your program will have a Main menu with 3 options, New Game, Score
and Quit. The program needs to have at least 3 functions:
a. selectWord to make a random selection of words that has been attached to homework description.
b. drawMan to draw the hanging man regarding to your situation.
c. checkWord to check the input letter and print out the correctly guessed letters and print dashes for
the remaining letters.

Hangman list
#include<stdio.h>
#include<time.h>
main()
{
      int index;
      char list[65][13]={{"sailboat"}, {"school"}, {"rug"}, {"chalk"}, {"tusk"}, {"camera"}, {"pajamas"}, {"cello"}, {"cake"}, {"beehive"}, {"gate"}, {"swing"}, {"lighthouse"}, {"toe"}, {"newspaper"}, {"stingray"}, {"brain"}, {"french"}, {"purse"}, {"soda"}, {"money"}, {"dryer"}, {"scale"}, {"clam"}, {"knee"}, {"artist"}, {"stomach"}, {"ticket"}, {"face"}, {"TV"}, {"can"}, {"blowfish"}, {"popsicle"}, {"beaver"}, {"snowball"}, {"dollar"}, {"sprinkler"}, {"hair"}, {"bathroom"}, {"rain"}, {"skirt"}, {"beach"}, {"suitcase"}, {"garden"}, {"doormat"}, {"pretzel"}, {"watering"}, {"fur"}, {"birthday"}, {"fries"}, {"owl"}, {"chin"}, {"spool"}, {"mushroom"}, {"peach"}, {"pelican"}, {"pineapple"}, {"flagpole"}, {"doghouse"}, {"boot"}, {"flute"}, {"quilt"}, {"hook"}, {"rainbow"}, {"happy"}};
      srand(time(NULL));
      index=rand()%65;
      printf("%s ",list[index]);
      system("PAUSE");
}

Explanation / Answer

#include #include #include #include #include #include #include /******************************************* * * Code written by Graphix. * More information regarding this code, other * hangman source code and words files * can be found on: * * http://www.hangman.symbolwebdesign.nl * * Contact: info@symbolwebdesign.nl * ********************************************/ char fileLoc[500]; // The backup file location void showLogo() { printf("-------------------------------------------- "); printf("| # # # # # #### # # # # # | "); printf("| # # # # ## # # ## ## # # ## # | "); printf("| #### ##### # # # # ## # # # ##### # # # | "); printf("| # # # # # ## # # # # # # # ## | "); printf("| # # # # # # ### # # # # # # | "); printf("-------------------------------------------- "); } void prn_galg(int i) { switch (i) { case 0 : printf("Amount of wrong letters: %d ", i); printf(" "); printf(" "); printf(" "); printf(" "); printf(" "); printf(" "); printf("____________ "); break; case 1 : printf("Amount of wrong letters: %d ", i); printf(" "); printf(" | "); printf(" | "); printf(" | "); printf(" | "); printf(" | "); printf("__|_________ "); break; case 2 : printf("Amount of wrong letters: %d ", i); printf(" _______ "); printf(" | "); printf(" | "); printf(" | "); printf(" | "); printf(" | "); printf("__|_________ "); break; case 3 : printf("Amount of wrong letters: %d ", i); printf(" _______ "); printf(" |/ "); printf(" | "); printf(" | "); printf(" | "); printf(" | "); printf("__|_________ "); break; case 4 : printf("Amount of wrong letters: %d ", i); printf(" _______ "); printf(" |/ | "); printf(" | O "); printf(" | "); printf(" | "); printf(" | "); printf("__|_________ "); break; case 5 : printf("Amount of wrong letters: %d ", i); printf(" _______ "); printf(" |/ | "); printf(" | O "); printf(" | | "); printf(" | | "); printf(" | "); printf("__|_________ "); break; case 6 : printf("Amount of wrong letters: %d ", i); printf(" _______ "); printf(" |/ | "); printf(" | O "); printf(" | \| "); printf(" | | "); printf(" | "); printf("__|_________ "); break; case 7 : printf("Amount of wrong letters: %d ", i); printf(" _______ "); printf(" |/ | "); printf(" | O "); printf(" | \|/ "); printf(" | | "); printf(" | "); printf("__|_________ "); break; case 8 : printf("Amount of wrong letters: %d ", i); printf(" _______ "); printf(" |/ | "); printf(" | O "); printf(" | \|/ "); printf(" | | "); printf(" | / "); printf("__|_________ "); break; case 9 : printf("Amount of wrong letters: %d ", i); printf(" _______ "); printf(" |/ | "); printf(" | O "); printf(" | \|/ "); printf(" | | "); printf(" | / \ "); printf("__|_________ "); break; case 10 : printf("Amount of wrong letters: %d ", i); printf(" _______ "); printf(" |/ | "); printf(" | X "); printf(" | \|/ "); printf(" | | "); printf(" | / \ "); printf("__|_________ "); break; } } char randomNumber(int max_number) { srand(time(NULL)); int g = (rand() % (max_number + 1)); return g; } char *getWord() { char c[50000]; /* declare a char array */ int n; FILE *file; /* declare a FILE pointer */ /* Opening words file */ if (strcmp(fileLoc, "") != 1) { // Here is the default words file, you can change this into whatever words file you'd like. file = fopen("words.txt", "r"); } else { file = fopen(fileLoc, "r"); } /* Incase the file cant be openend */ if(file==NULL) { printf("An error has occured: can't open words file. Please type the location of the words file: "); scanf("%s", fileLoc); printf("Reading file '%s'..... ", fileLoc); file = fopen(fileLoc, "r"); if (file == NULL) { while (file==NULL) { printf("That file doesn't exist. Enter the location of the words file: "); scanf("%s", fileLoc); printf("Reading file '%s'..... ", fileLoc); file = fopen(fileLoc, "r"); } } printf("File has been read. "); n = fread(c, 1, 50000, file); c[n] = ''; } else { /* Reading the contents of the file */ n = fread(c, 1, 50000, file); c[n] = ''; } /* Separating the contents, divided by | and declaring variables */ char *token = strtok(c, "|"); char *words[200] = {0}; int f = 0; while(token != NULL) { /* Allocating memory for the pointer */ words[f] = malloc(strlen(token)+1); /* Copying entire string to pointer */ strcpy(words[f],token); /* Resetting pointer */ token = strtok(NULL, "|"); f++; } /* Closing the file */ fclose(file); /* Retrieving a random number */ int wordN = randomNumber(f); /* Freeing all the memory allocated for the strings */ int q; for(q = 0; q < 200; q++) { if( q != wordN) { free(words[q]); } } /* Returning string */ return words[wordN]; } int main(void) { char udi[] = "EMPTY"; while ((strcmp(udi, "END") != 0) && ((strcmp(udi, "AGAIN") == 0) || (strcmp(udi, "EMPTY") == 0))) { strcpy(udi, "EMPTY"); /* Declaring variables */ /* Random deciding which word is chosen to be guessed: guessWord is the word that needs to be guessed currentWord is the word that is filled with dots */ /* Retrieving the word that matches with the wordNumber */ /* Check which number was chosen: printf("%d", wordNumber); */ char *tempWord = getWord(); /* Declaring the guessWord with the length of dkljafoue */ char guessWord[strlen(tempWord)+1]; /* Copying the string of dkljafoue into guessWord */ strcpy(guessWord, tempWord); /* Freeing the pointer */ free(tempWord); /* Calculate the length of the guessWord */ int wordlength = strlen(guessWord); /* Creating the dotword (name: currentWord) */ char* currentWord = malloc( wordlength ); int t; for (t = 0; t
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote