abc123mewot

Hangman

Oct 15th, 2023 (edited)
621
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.91 KB | None | 0 0
  1. #include "stdio.h"
  2. #include "stdlib.h"
  3. #include "string.h"
  4. #include "stdint.h"
  5.  
  6. void drawHangman(uint8_t tries, char** extraLns) {
  7.     if(tries > 10) puts("ERROR - Too many tries!");
  8.     char lns[8][16];
  9.     strcpy(lns[0], "    ______    ");
  10.     strcpy(lns[1], "    |/   |    ");
  11.     strcpy(lns[2], "    |   `O`   ");
  12.     strcpy(lns[3], "    |   /|\\  ");
  13.     strcpy(lns[4], "    |  * | * ");
  14.     strcpy(lns[5], "    |   /*\\  ");
  15.     strcpy(lns[6], "    |  /   \\  ");
  16.     strcpy(lns[7], "____|____     ");
  17.     char fc = ' ';
  18.     switch(tries) {
  19.         case 10: fc = 'X';       // Remove head and replace figure character with X
  20.                 lns[2][9] = fc;
  21.         case 9: lns[2][8] = fc;  // Remove ears
  22.                 lns[2][10] = fc;
  23.         case 8: lns[3][9] = fc;  // Remove torso
  24.         case 7: lns[4][9] = fc;  // Remove pelvis
  25.                 lns[5][9] = fc;  
  26.         case 6: lns[5][8] = fc;  // Remove legs
  27.                 lns[5][10] = fc;
  28.         case 5: lns[3][8] = fc;  // Remove arms
  29.                 lns[3][10] = fc;
  30.         case 4: lns[6][11] = fc; // Remove right foot
  31.         case 3: lns[6][7] = fc;  // Remvoe left foot
  32.         case 2: lns[4][11] = fc; // Remove right hand
  33.         case 1: lns[4][7] = fc;  // Remove left hand
  34.         break;
  35.     }
  36.     for(uint8_t i = 0; i < 8; i++) {
  37.         printf("%s%s\n", ((extraLns && extraLns[i]) ? extraLns[i] : ""), lns[i]);
  38.     }
  39.     printf("\n%s%d/10 Tries\n\n", ((extraLns && extraLns[8]) ? extraLns[8] : ""), 10 - tries);
  40. }
  41.  
  42. int main(int argc, char** argv) {
  43.     if(argc < 2) return 1;
  44.     char* secret = argv[1];
  45.     uint8_t secretLen = strlen(secret);
  46.    
  47.     // Initialize a line containing all the spaces needed
  48.     uint8_t borderLen = secretLen < 12 ? 16 - secretLen : 4;
  49.     uint8_t extraLen = borderLen * 2 + secretLen;
  50.     char* spaceLn = (char*) malloc(extraLen + 1);
  51.     for(uint8_t i = 0; i < extraLen; i++) spaceLn[i] = ' ';
  52.     spaceLn[extraLen] = 0;
  53.     // Initialize a line for the word
  54.     char* wordStr = (char*) malloc(extraLen + 1);
  55.     for(uint8_t i = 0; i < borderLen; i++) wordStr[i] = ' ';
  56.     for(uint8_t i = 0; i < secretLen; i++) wordStr[i + borderLen] = '_';
  57.     for(uint8_t i = 0; i < borderLen; i++) wordStr[i + secretLen + borderLen] = ' ';
  58.     wordStr[extraLen] = 0;
  59.     // Intialize a line for the tries string
  60.     char* triesStr = (char*) malloc(extraLen + 1);
  61.     uint8_t triesBorderLen = (extraLen - 10) / 2;
  62.     for(uint8_t i = 0; i < triesBorderLen; i++) triesStr[i] = ' ';
  63.     for(uint8_t i = 0; i < 10; i++) triesStr[i + triesBorderLen] = '_';
  64.     uint8_t extraSpaceLen = (extraLen - 10) % 2 ? triesBorderLen + 1 : triesBorderLen;
  65.     for(uint8_t i = 0; i < extraSpaceLen; i++) triesStr[i + 10 + triesBorderLen] = ' ';
  66.     triesStr[extraLen] = 0;
  67.     // Extra lines used by drawHangman
  68.     char* extraLns[9] = { spaceLn, spaceLn, spaceLn, wordStr, spaceLn, spaceLn, spaceLn, spaceLn, triesStr };
  69.    
  70.     uint8_t tries = 0;
  71.     puts("Welcome to hangman!");
  72.     while(tries <= 10) {
  73.         printf("\e[2J\e[1;1H");
  74.         puts("Try to guess the magic word!");
  75.         drawHangman(tries, extraLns);
  76.         if(tries == 10) break;
  77.         printf("Your guess: ");
  78.         char guess = getchar();
  79.         if((guess >= 'a' && guess <= 'z') || (guess >= 'A' && guess <= 'Z') || guess == ' ') {
  80.             getchar();
  81.             uint8_t originalGuess = 1;
  82.             for(uint8_t i = triesBorderLen; i < triesBorderLen + 10; i++) {
  83.                 if(triesStr[i] == guess) {
  84.                     originalGuess = 0;
  85.                     break;
  86.                 }
  87.             }
  88.             if(originalGuess) {
  89.                 uint8_t winnerCheck = 0, invalidGuess = 1;;
  90.                 for(uint8_t i = 0; i < secretLen; i++) {
  91.                     if(secret[i] == guess) {
  92.                         wordStr[borderLen + i] = guess;
  93.                         invalidGuess = 0;
  94.                     }
  95.                     if(secret[i] == wordStr[borderLen + i]) winnerCheck++;
  96.                 }
  97.                 if(invalidGuess) { 
  98.                     triesStr[triesBorderLen + tries] = guess;
  99.                     tries++;
  100.                 }
  101.                 if(winnerCheck == secretLen) {
  102.                     break;
  103.                 }
  104.             } else puts("You've already guessed that letter!");
  105.         } else puts("Invalid guess!");
  106.     }
  107.     if(tries == 10) {
  108.         printf("You loose! The word was: %s\n", secret);
  109.     } else {
  110.         printf("You win with %d tries remaining! Good job!\n", 10 - tries);
  111.     }
  112.     free(spaceLn);
  113.     free(wordStr);
  114.     return 0;
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment