96 lines
2.6 KiB
C
96 lines
2.6 KiB
C
#include <stdio.h>
|
|
#include "hangman.h"
|
|
#include "termstuff.h"
|
|
|
|
int printWord(char *word, char *tried){
|
|
int unknownCharacterExists = 0;
|
|
int characterFound;
|
|
char *p = word;
|
|
|
|
while (*p != 0) {
|
|
characterFound = 0;
|
|
for (char *triedFindptr = tried; *triedFindptr != 0; triedFindptr++) {
|
|
if (*p == *triedFindptr) {
|
|
putchar(*p);
|
|
characterFound = 1;
|
|
break;
|
|
}
|
|
}
|
|
if (!characterFound) {
|
|
putchar('_');
|
|
unknownCharacterExists = 1;
|
|
}
|
|
p++;
|
|
}
|
|
putchar('\n');
|
|
return unknownCharacterExists;
|
|
}
|
|
|
|
int getInput(char znak, char *tried, char **triedptr, char *word, int *fails, int maxFails) {
|
|
int validGuess = 0;
|
|
int characterFound;
|
|
char *p;
|
|
clear();
|
|
|
|
if(!printWord(word, tried)){
|
|
return 1;
|
|
}
|
|
|
|
printf("Zadaj znak:");
|
|
while (validGuess == 0){
|
|
znak = getchar();
|
|
if(znak != '\n') { // we got a non enter character
|
|
validGuess = 1; //presume no duplicates
|
|
for (char *triedFindptr = tried; *triedFindptr != 0; triedFindptr++) {
|
|
if (znak == *triedFindptr) {
|
|
validGuess = 0; //duplicate found
|
|
printf("Znak už bol zadaný, zadajte znova:");
|
|
}
|
|
}
|
|
if(validGuess) {
|
|
*(*triedptr)++ = znak;
|
|
p = word;
|
|
characterFound = 0;
|
|
while (*p != 0) {
|
|
if (*p == znak) {
|
|
characterFound = 1;
|
|
break;
|
|
}
|
|
p++;
|
|
}
|
|
if (!characterFound) {
|
|
(*fails)++;
|
|
if (*fails > maxFails) {
|
|
printf("Neuhádol si ani na tento počet pokusov: %d, slovo bolo %s\n", maxFails, word);
|
|
return 1;
|
|
} else {
|
|
printf("Neuhádol si, pokusov, ktoré ešte ostávajú je %d.\n", maxFails - *fails + 1);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
while (getchar() != '\n'); // wait for enter to ensure only one character
|
|
return 0;
|
|
}
|
|
|
|
int hangman() {
|
|
|
|
char word[21];
|
|
|
|
char tried[100];
|
|
*tried = 0;
|
|
char *triedptr = tried;
|
|
char znak;
|
|
int fails = 0;
|
|
int maxFails = 2;
|
|
|
|
printf("Zadaj slovo(max 20 znakov):");
|
|
scanf("%20s", word);
|
|
|
|
while (1) {
|
|
if(getInput(znak, tried, &triedptr, word, &fails, maxFails)){
|
|
return 0;
|
|
}
|
|
}
|
|
} |