Don't like ads? PRO users don't see any ads ;-)
Guest

hangman game in C

By: a guest on May 10th, 2012  |  syntax: C  |  size: 7.08 KB  |  hits: 29  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /* begin hangman.h
  2. eventually this will be added to a header file
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <time.h>
  8. #include <ctype.h>
  9.  
  10. #define TOTAL_PHRASES      3    /* Total number of phrases for lose screen. */
  11. #define TOTAL_WORDS        6    /* Total number of words to choose.         */
  12. #define MAX_LENGTH        13    /* Maximum word length.                     */
  13. #define MAX_CHANCES        6    /* Number of chances until hanged.          */
  14. /*
  15. * 0 - head     1 - body
  16. * 2 - l_arm    3 - r_arm
  17. * 4 - l_leg    5 - r_leg
  18. */
  19.  
  20. /* all the function prototypes(for explanations see code). */
  21. void clr_scrn(void){ system("clear"); };
  22. int check_win(char*);
  23. int check_match(char*, char*, char);
  24. char *choose_word(void);
  25. char get_guess(char*, char*, int);
  26. void hanged(char*);
  27. void winner(char*);
  28.  
  29. /*
  30. ----------- end hangman.h -----------
  31. */
  32.  
  33.  
  34. /* begin hangman.c */
  35. int main(void)
  36. {
  37.     char *word;                  /* the word to decipher.              */
  38.     char shadow[MAX_LENGTH];     /* a shadowed copy of the word.       */
  39.     char letter, win;            /* the guessed letter and win status. */
  40.     int check, guess_count = 0;  /* count how many wrong guesses.      */
  41.    
  42.     clr_scrn();                  /* clear the screen.                  */
  43.     word = choose_word();        /* randomly choose a word.            */
  44.  
  45.     /* shadow the current word. */
  46.     int i;
  47.     for(i = 0; i < strlen(word); i++)
  48.         { shadow[i] = '*'; }    
  49.     shadow[i] = '\0'; /* to keep gibberish from sneaking into the string */
  50.     /* main loop of the game. */
  51.     do{  
  52.     letter = get_guess(word, shadow, guess_count);
  53.     check = check_match(word, shadow, letter);
  54.     clr_scrn();
  55.  
  56.     /* if the letter isn't in the word, use up one chance. */
  57.     if(check == 0)
  58.         { guess_count++; }
  59.     /* check for a win.. */
  60.     win = check_win(shadow);
  61.     if(win == 0)
  62.         { winner(word); }
  63.  
  64.     }while(guess_count < MAX_CHANCES);
  65.    
  66.  
  67.     hanged(word);
  68.  
  69.     return(0);
  70. }
  71.  
  72. /********************************************
  73. * randomly choose a word from the list.     *
  74. * generatesa number between 0 and           *
  75. * TOTAL_WORDS. A word is assigned according *
  76. * to a hardcoded list. If a word is added   *
  77. * TOTAL_WORDS will have to be changed to    *
  78. * the total number of words in the list.    *
  79. ********************************************/
  80. char *choose_word(void)
  81. {
  82.     int word;
  83.     srand(time(NULL));
  84.     word = (int)rand()%TOTAL_WORDS;
  85.     char *newword;
  86.     switch(word)
  87.     {
  88.         case 0:
  89.             newword = "camper\0";
  90.             break;
  91.         case 1:
  92.             newword = "motorcycle\0";
  93.             break;
  94.         case 2:
  95.             newword = "elephant\0";
  96.             break;
  97.         case 3:
  98.             newword = "handbag\0";
  99.             break;
  100.         case 4:
  101.             newword = "imagination\0";
  102.             break;
  103.         case 5:
  104.             newword = "terrestrial\0";
  105.             break;
  106.     }
  107.  
  108.     return (newword);  
  109. }
  110.  
  111. /*********************************************
  112. * get a guess from the player.               *
  113. * the player guesses a letter. Then it gets  *
  114. * converted to a lowercase version of itself.*
  115. *********************************************/
  116. char get_guess(char *word, char *shadow, int chances)
  117. {
  118.      char guess;
  119.      printf("\n%s\n", shadow);
  120.      printf("\n\nChance: %d/6\tGuess a letter: ", chances);
  121.      scanf(" %c", &guess); /* scanf is a strange fruit... */
  122.  
  123.      return(tolower(guess));
  124. }
  125.  
  126. /*********************************************
  127. * check each letter of the word for a match. *
  128. * In addition to checking the guess against  *
  129. * the word for matching letters, the shadow  *
  130. * gets modified according the guess.         *
  131. *********************************************/
  132. int check_match(char *cur_word, char *shadow, char guess)
  133. {
  134.     int i, status = 0;
  135.    
  136.     for(i = 0;i < strlen(cur_word); i++)
  137.     {
  138.         if(cur_word[i] == tolower(guess))
  139.             { shadow[i] = cur_word[i]; status++; }
  140.     }
  141.     if(status > 0)
  142.         { printf("\nThere are %d %cs.\n", status, guess); }
  143.     else
  144.         { printf("\nThere is no %c.\n", guess); }
  145.        
  146.     return(status);
  147. }
  148.  
  149. /*********************************************
  150. * If the user has lost.                      *
  151. * I want to make the a random quote similiar *
  152. * to the hardcoded wordlist used earlier.    *
  153. *********************************************/
  154. void hanged(char *word)
  155. {
  156.     int phrase;
  157.     srand(time(NULL));
  158.     phrase = (int)rand()%TOTAL_PHRASES;
  159.  
  160.     printf("\n   You were hanged for your failure "
  161.            "to recognize the word %s.\n"
  162.            "        ---------        \n"
  163.            "        |       |        \n"
  164.            "        |     , O ,      \n"
  165.            "        |      \\I/      \n"
  166.            "        |       |        \n"
  167.            "        |     _/ \\_      \n"
  168.            "        |                \n"
  169.            " _____________________   \n", word);    
  170.  
  171.     switch(phrase)
  172.     {
  173.         case 0:
  174.             printf("\n\n%cBut now I laugh and pull so hard and see you swingin'\n"
  175.             "                          on the gallow's pole.%c\n"
  176.             "               - Led Zeppelin\n\n", '"', '"');
  177.             break;
  178.  
  179.         case 1:
  180.             printf("\n\n%cEarl of Sandwhich:  You shall either die of the\n"
  181.                    "pox or on the gallows\nJohn Wilkes:  That sir depends on\n"
  182.                    "whether I embrace your mistress or your politics.%c\n"
  183.                    "                    - John Wilkes\n\n", '"', '"');
  184.             break;
  185.         case 2:
  186.             printf("\n\n%cGALLOWS, n. A stage for the performance of miracle\n"
  187.                    "plays, in which the leading actor is translated to heaven.\n"
  188.                    "In this country the gallows is chiefly remarkable for the\n"
  189.                    "number of persons who escape it.\n\nWhether on the gallows "
  190.                    "high\nOr where blood flows the reddest, The noblest place\n"
  191.                    "for man to die\n      -- Is where he died the deadest. "
  192.                    "--(Old play)%c\n                   - Ambrose Bierce\n\n",
  193.                                                                        '"', '"');
  194.            break;
  195.                    
  196.  
  197.     }
  198.  
  199.    
  200. }
  201.  
  202.  
  203. /*********************************************
  204. * Determine whether the puzzle is solved.    *
  205. * Check to see if there are any underscores  *
  206. * in the shadowed word.  :D                  *
  207. *********************************************/
  208. int check_win(char *shade)
  209. {
  210.     int i, status = 0;
  211.    
  212.     for(i = 0;i <= strlen(shade)-1; i++)
  213.     {
  214.         if(shade[i] == '*')
  215.             { status++; }
  216.     }
  217.    
  218.     return(status);
  219. }
  220.  
  221. /*********************************************
  222. * Print congratulations for winning...       *
  223. * What you see when you win!                 *
  224. *********************************************/
  225. void winner(char *word)
  226. {
  227.     printf("\n\nCongratulations!  You have deciphered the word: "
  228.            "%s.\n\n", word);
  229.     exit(0);
  230. }