Work on hangman

This commit is contained in:
Bruno Rybársky 2024-02-20 21:15:11 +01:00
parent 6bcb7fc73c
commit 01b776a5a9
9 changed files with 162 additions and 77 deletions

@ -33,6 +33,10 @@ add_executable(skola calc.c
hviezdicky.h hviezdicky.h
stringy.c stringy.c
stringy.h stringy.h
hangman.c
hangman.h
termstuff.c
termstuff.h
) )
target_link_libraries(skola m) target_link_libraries(skola m)

96
hangman.c Normal file

@ -0,0 +1,96 @@
#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;
}
}
}

1
hangman.h Normal file

@ -0,0 +1 @@
int hangman();

@ -4,41 +4,8 @@
#include "hviezdicky.h" #include "hviezdicky.h"
#include "stdio.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 stars() {
int j; //stĺpec int j; //stĺpec

4
main.c

@ -1,5 +1,5 @@
#include "stringy.h" #include "hangman.h"
int main() { int main() {
return stringy(); return hangman();
} }

@ -17,45 +17,5 @@ int meno() {
int dlzka = strlen(str); 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, printf("Kópia: %s, meno: %s ,dĺžka: %d, 'a' sa nachádza na: %d mieste v zadanom texte", strkopia, str, dlzka,
polohaAcka); 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; return 0;
} }

@ -1,2 +1,2 @@
int stringy(); int meno();

51
termstuff.c Normal file

@ -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();
}

6
termstuff.h Normal file

@ -0,0 +1,6 @@
void clear();
void escapeStart();
void escapeEnd();
void white();
void black();
void original();