/*
* hangman.c
* a REALLY simple hangman game
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int wrong = 0;
int correct = 0;
int finished = 0;
char solution[8] = "hangman";
int charIsSolved[7] = {0,0,0,0,0,0,0};
char triedLetters[27] = "??????????????????????????";
char input;
char stage1[] = " -----\n / \\\n";
char stage2[28] = " |\n |\n |\n";
char *stage2_new = " | o\n | ^W^\n | / \\\n";
char stage3[11] = " |/\n";
char *stage3_new = " |/ |\n";
char stage4[11] = " +----\n";
char *stage4_new = " +---+\n";
void drawGallow() {
if (!wrong) {
printf("\n########################################\n\n");
return;
}
if (wrong < 5) {
switch (wrong) {
case 4:
printf("%s", stage4);
case 3:
printf("%s", stage3);
case 2:
printf("%s", stage2);
case 1:
printf("%s", stage1);
}
} else {
printf("%s", stage4);
printf("%s", stage3);
printf("%s", stage2);
printf("%s", stage1);
}
printf("\n########################################\n\n");
}
void checkInput() {
int foundChar = 0;
/* check each letter of the solution whether it matches or not */
for (int i = 0; i < 7; ++i) {
if (input == solution[i]) {
charIsSolved[i] = 1;
correct++;
foundChar = 1;
}
}
/* if there were no matches, increase the wrong-counter to extend the gallow */
if (!foundChar) {
triedLetters[wrong] = input;
wrong++;
}
}
void drawStatus() {
/* print all the letters which were found out */
printf("The word: ");
for (int i = 0; i < 7; ++i) {
if (charIsSolved[i]) {
printf("%c", solution[i]);
} else {
printf("_");
}
}
printf("\n");
/* print all the letters that were tried */
printf("Your tries: ");
for (int i = 0; i < 26; ++i) {
if (triedLetters[i] != '?') {
printf("%c, ", triedLetters[i]);
} else {
break;
}
}
printf("\n");
}
void guessChar() {
char dummy;
printf("\nYour guess: ");
scanf("%c", &input);
scanf("%c", &dummy);
}
void updateGallow() {
switch (wrong) {
case 5:
strcpy(stage4, stage4_new);
break;
case 6:
strcpy(stage3, stage3_new);
break;
case 7:
strcpy(stage2, stage2_new);
break;
}
}
void nextTurn() {
system("clear");
if (correct == 7) {
printf("\nCongrats, you've won!\n\n");
finished = 1;
return;
} else if (wrong == 7) {
drawGallow();
printf ("\nYou were to bad for staying alive...\n\n");
finished = 1;
return;
} else {
drawGallow();
drawStatus();
guessChar();
checkInput();
updateGallow();
}
}
int main(void) {
while (!finished) {
nextTurn();
}
return 0;
}