C Program A Really simple hangman game

How to write a C Program A Really simple hangman game in C Programming Language ?


  1. /*
  2.  * hangman.c
  3.  * a REALLY simple hangman game
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. int wrong = 0;
  11. int correct = 0;
  12. int finished = 0;
  13. char solution[8] = "hangman";
  14. int charIsSolved[7] = {0,0,0,0,0,0,0};
  15. char triedLetters[27] = "??????????????????????????";
  16. char input;
  17. char stage1[] = "  -----\n /     \\\n";
  18. char stage2[28] = "    |\n    |\n    |\n";
  19. char *stage2_new = "    |   o\n    |  ^W^\n    |  / \\\n";
  20. char stage3[11] = "    |/\n";
  21. char *stage3_new = "    |/  |\n";
  22. char stage4[11] = "    +----\n";
  23. char *stage4_new = "    +---+\n";
  24.  
  25. void drawGallow() {
  26.  
  27.    if (!wrong) {
  28.       printf("\n########################################\n\n");
  29.       return;
  30.    }
  31.  
  32.    if (wrong < 5) {
  33.       switch (wrong) {
  34.          case 4:
  35.             printf("%s", stage4);
  36.          case 3:
  37.             printf("%s", stage3);
  38.          case 2:
  39.             printf("%s", stage2);
  40.          case 1:
  41.             printf("%s", stage1);
  42.       }
  43.    } else {
  44.       printf("%s", stage4);
  45.       printf("%s", stage3);
  46.       printf("%s", stage2);
  47.       printf("%s", stage1);
  48.    }
  49.  
  50.    printf("\n########################################\n\n");
  51. }
  52.  
  53.  
  54. void checkInput() {
  55.  
  56.    int foundChar = 0;
  57.  
  58.    /* check each letter of the solution whether it matches or not */
  59.    for (int i = 0; i < 7; ++i) {
  60.       if (input == solution[i]) {
  61.          charIsSolved[i] = 1;
  62.          correct++;
  63.          foundChar = 1;
  64.       }
  65.    }
  66.  
  67.    /* if there were no matches, increase the wrong-counter to extend the gallow */
  68.    if (!foundChar) {
  69.       triedLetters[wrong] = input;
  70.       wrong++;
  71.    }
  72. }
  73.  
  74. void drawStatus() {
  75.  
  76.    /* print all the letters which were found out */
  77.    printf("The word: ");
  78.    for (int i = 0; i < 7; ++i) {       
  79.       if (charIsSolved[i]) {
  80.          printf("%c", solution[i]);
  81.       } else {
  82.          printf("_");
  83.       }
  84.    }
  85.    printf("\n");
  86.  
  87.    /* print all the letters that were tried */
  88.    printf("Your tries: ");
  89.    for (int i = 0; i < 26; ++i) {
  90.       if (triedLetters[i] != '?') {
  91.          printf("%c, ", triedLetters[i]);
  92.       } else {
  93.          break;
  94.       }
  95.    }
  96.    printf("\n");
  97. }
  98.  
  99. void guessChar() {
  100.  
  101.    char dummy;
  102.  
  103.    printf("\nYour guess: ");
  104.  
  105.    scanf("%c", &input);
  106.    scanf("%c", &dummy);
  107. }
  108.  
  109. void updateGallow() {
  110.  
  111.    switch (wrong) {
  112.       case 5:
  113.          strcpy(stage4, stage4_new);
  114.          break;
  115.       case 6:
  116.          strcpy(stage3, stage3_new);
  117.          break;
  118.       case 7:
  119.          strcpy(stage2, stage2_new);
  120.          break;
  121.    }
  122. }
  123.  
  124. void nextTurn() {
  125.  
  126.    system("clear");
  127.  
  128.    if (correct == 7) {
  129.       printf("\nCongrats, you've won!\n\n");
  130.       finished = 1;
  131.       return;
  132.    } else if (wrong == 7) {
  133.       drawGallow();
  134.       printf ("\nYou were to bad for staying alive...\n\n");
  135.       finished = 1;
  136.       return;
  137.    } else {
  138.       drawGallow();
  139.       drawStatus();
  140.       guessChar();
  141.       checkInput();
  142.       updateGallow();
  143.    }
  144. }
  145.  
  146.  
  147. int main(void) {
  148.  
  149.    while (!finished) {
  150.       nextTurn();
  151.    }
  152.  
  153.    return 0;
  154. }


Learn More :