diff --git a/hangman.c b/hangman.c new file mode 100644 index 0000000..9385bbf --- /dev/null +++ b/hangman.c @@ -0,0 +1,96 @@ +#include +#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; + } + } +} \ No newline at end of file diff --git a/hangman.h b/hangman.h new file mode 100644 index 0000000..c7577c0 --- /dev/null +++ b/hangman.h @@ -0,0 +1 @@ +int hangman(); \ No newline at end of file diff --git a/hviezdicky.c b/hviezdicky.c index 15edc04..4e0a55e 100644 --- a/hviezdicky.c +++ b/hviezdicky.c @@ -4,41 +4,8 @@ #include "hviezdicky.h" #include "stdio.h" +#include "termstuff.h" -void escapeStart(){ - putchar('\033'); - putchar('['); -} - -void escapeEnd() { - putchar('m'); -} - -void white() { - escapeStart(); - putchar('3'); - putchar('7'); - putchar(';'); - putchar('4'); - putchar('0'); - escapeEnd(); -} - -void black() { - escapeStart(); - putchar('3'); - putchar('0'); - putchar(';'); - putchar('4'); - putchar('7'); - escapeEnd(); -} - -void original() { - escapeStart(); - putchar('0'); - escapeEnd(); -} int stars() { int j; //stĺpec diff --git a/stringy.c b/stringy.c index 731ef16..59fd313 100644 --- a/stringy.c +++ b/stringy.c @@ -17,45 +17,5 @@ int meno() { int dlzka = strlen(str); printf("Kópia: %s, meno: %s ,dĺžka: %d, 'a' sa nachádza na: %d mieste v zadanom texte", strkopia, str, dlzka, polohaAcka); - -} - -int stringy() { - char word[21]; - char *p; - char tried[100]; - char *triedptr = tried; - printf("Zadaj slovo(max 20 znakov):"); - fgets(word, 20, stdin); - int unknownCharacterExists = 1; - while (unknownCharacterExists) { - printf("Zadaj znak:"); - char znak = '\n'; - while (znak == '\n'){ - znak = getchar(); - } - *triedptr = znak; - triedptr++; - p = word; - int characterFound; - unknownCharacterExists = 0; - while (*p != '\n') { - 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 0; } \ No newline at end of file diff --git a/stringy.h b/stringy.h index f5cbfd4..1f28051 100644 --- a/stringy.h +++ b/stringy.h @@ -1,2 +1,2 @@ -int stringy(); +int meno(); diff --git a/termstuff.c b/termstuff.c new file mode 100644 index 0000000..6964fe0 --- /dev/null +++ b/termstuff.c @@ -0,0 +1,51 @@ +#include "termstuff.h" +#include "stdio.h" + +void clear(){ + putchar(0x1b); + putchar(0x5b); + putchar(0x48); + putchar(0x1b); + putchar(0x5b); + putchar(0x32); + putchar(0x4a); + putchar(0x1b); + putchar(0x5b); + putchar(0x33); + putchar(0x4a); +} + +void escapeStart(){ + putchar('\033'); + putchar('['); +} + +void escapeEnd() { + putchar('m'); +} + +void white() { + escapeStart(); + putchar('3'); + putchar('7'); + putchar(';'); + putchar('4'); + putchar('0'); + escapeEnd(); +} + +void black() { + escapeStart(); + putchar('3'); + putchar('0'); + putchar(';'); + putchar('4'); + putchar('7'); + escapeEnd(); +} + +void original() { + escapeStart(); + putchar('0'); + escapeEnd(); +} \ No newline at end of file diff --git a/termstuff.h b/termstuff.h new file mode 100644 index 0000000..ea55d76 --- /dev/null +++ b/termstuff.h @@ -0,0 +1,6 @@ +void clear(); +void escapeStart(); +void escapeEnd(); +void white(); +void black(); +void original(); \ No newline at end of file