#include "stdio.h" #include "stdlib.h" #include "string.h" #include "stdint.h" void drawHangman(uint8_t tries, char** extraLns) { if(tries > 10) puts("ERROR - Too many tries!"); char lns[8][16]; strcpy(lns[0], " ______ "); strcpy(lns[1], " |/ | "); strcpy(lns[2], " | `O` "); strcpy(lns[3], " | /|\\ "); strcpy(lns[4], " | * | * "); strcpy(lns[5], " | /*\\ "); strcpy(lns[6], " | / \\ "); strcpy(lns[7], "____|____ "); char fc = ' '; switch(tries) { case 10: fc = 'X'; // Remove head and replace figure character with X lns[2][9] = fc; case 9: lns[2][8] = fc; // Remove ears lns[2][10] = fc; case 8: lns[3][9] = fc; // Remove torso case 7: lns[4][9] = fc; // Remove pelvis lns[5][9] = fc; case 6: lns[5][8] = fc; // Remove legs lns[5][10] = fc; case 5: lns[3][8] = fc; // Remove arms lns[3][10] = fc; case 4: lns[6][11] = fc; // Remove right foot case 3: lns[6][7] = fc; // Remvoe left foot case 2: lns[4][11] = fc; // Remove right hand case 1: lns[4][7] = fc; // Remove left hand break; } for(uint8_t i = 0; i < 8; i++) { printf("%s%s\n", ((extraLns && extraLns[i]) ? extraLns[i] : ""), lns[i]); } printf("\n%s%d/10 Tries\n\n", ((extraLns && extraLns[8]) ? extraLns[8] : ""), 10 - tries); } int main(int argc, char** argv) { if(argc < 2) return 1; char* secret = argv[1]; uint8_t secretLen = strlen(secret); // Initialize a line containing all the spaces needed uint8_t borderLen = secretLen < 12 ? 16 - secretLen : 4; uint8_t extraLen = borderLen * 2 + secretLen; char* spaceLn = (char*) malloc(extraLen + 1); for(uint8_t i = 0; i < extraLen; i++) spaceLn[i] = ' '; spaceLn[extraLen] = 0; // Initialize a line for the word char* wordStr = (char*) malloc(extraLen + 1); for(uint8_t i = 0; i < borderLen; i++) wordStr[i] = ' '; for(uint8_t i = 0; i < secretLen; i++) wordStr[i + borderLen] = '_'; for(uint8_t i = 0; i < borderLen; i++) wordStr[i + secretLen + borderLen] = ' '; wordStr[extraLen] = 0; // Intialize a line for the tries string char* triesStr = (char*) malloc(extraLen + 1); uint8_t triesBorderLen = (extraLen - 10) / 2; for(uint8_t i = 0; i < triesBorderLen; i++) triesStr[i] = ' '; for(uint8_t i = 0; i < 10; i++) triesStr[i + triesBorderLen] = '_'; uint8_t extraSpaceLen = (extraLen - 10) % 2 ? triesBorderLen + 1 : triesBorderLen; for(uint8_t i = 0; i < extraSpaceLen; i++) triesStr[i + 10 + triesBorderLen] = ' '; triesStr[extraLen] = 0; // Extra lines used by drawHangman char* extraLns[9] = { spaceLn, spaceLn, spaceLn, wordStr, spaceLn, spaceLn, spaceLn, spaceLn, triesStr }; uint8_t tries = 0; puts("Welcome to hangman!"); while(tries <= 10) { printf("\e[2J\e[1;1H"); puts("Try to guess the magic word!"); drawHangman(tries, extraLns); if(tries == 10) break; printf("Your guess: "); char guess = getchar(); if((guess >= 'a' && guess <= 'z') || (guess >= 'A' && guess <= 'Z') || guess == ' ') { getchar(); uint8_t originalGuess = 1; for(uint8_t i = triesBorderLen; i < triesBorderLen + 10; i++) { if(triesStr[i] == guess) { originalGuess = 0; break; } } if(originalGuess) { uint8_t winnerCheck = 0, invalidGuess = 1;; for(uint8_t i = 0; i < secretLen; i++) { if(secret[i] == guess) { wordStr[borderLen + i] = guess; invalidGuess = 0; } if(secret[i] == wordStr[borderLen + i]) winnerCheck++; } if(invalidGuess) { triesStr[triesBorderLen + tries] = guess; tries++; } if(winnerCheck == secretLen) { break; } } else puts("You've already guessed that letter!"); } else puts("Invalid guess!"); } if(tries == 10) { printf("You loose! The word was: %s\n", secret); } else { printf("You win with %d tries remaining! Good job!\n", 10 - tries); } free(spaceLn); free(wordStr); return 0; }