Browse Source

refactoring: Aufteilen der Funktion letterGuessed zu bool letterGuessed und changeLetter

remotes/origin/tim
fdai7727 11 months ago
parent
commit
89b622bb64
  1. 44
      src/main/c/Tim/hangman.c
  2. 1
      src/main/c/Tim/hangman.h

44
src/main/c/Tim/hangman.c

@ -54,6 +54,7 @@ void hangman()
scanf(" %c", &userSelection); scanf(" %c", &userSelection);
letterGuessed(userSelection, guessWord, length, displayWord); letterGuessed(userSelection, guessWord, length, displayWord);
changeLetter(userSelection, guessWord, length, displayWord);
printf("%s\n", displayWord); printf("%s\n", displayWord);
} }
@ -90,6 +91,37 @@ bool wordGuessed(char x[], char y[])
bool letterGuessed(char x, char y[], int length, char ptr[]) bool letterGuessed(char x, char y[], int length, char ptr[])
{ {
int counter = 0; int counter = 0;
for(int i = 0; i<length; i++) {
if(isupper(x)!=0)
{
if (x == y[i]-32 || x == y[i] )
{
counter++;
}
}
else
{
if (x == y[i]||x == y[i]+32)
{
counter++;
}
}
}
if(counter>0)
{
return true;
}
else
{
return false;
}
}
void changeLetter(char x, char y[], int length, char ptr[])
{
for(int i = 0; i<length; i++) { for(int i = 0; i<length; i++) {
if(isupper(x)!=0) if(isupper(x)!=0)
{ {
@ -104,7 +136,6 @@ bool letterGuessed(char x, char y[], int length, char ptr[])
ptr[i] = x+32; ptr[i] = x+32;
} }
printf("Dein gewaehlter Buchstabe %c war ein Treffer!\n", x); printf("Dein gewaehlter Buchstabe %c war ein Treffer!\n", x);
counter++;
} }
} }
else else
@ -119,18 +150,7 @@ bool letterGuessed(char x, char y[], int length, char ptr[])
ptr[i] = x; ptr[i] = x;
} }
printf("Dein gewaehlter Buchstabe %c war ein Treffer!\n", x); printf("Dein gewaehlter Buchstabe %c war ein Treffer!\n", x);
counter++;
} }
} }
}
if(counter>0)
{
return true;
} }
else
{
return false;
}
} }

1
src/main/c/Tim/hangman.h

@ -6,5 +6,6 @@ void hangman();
char* getWordFromList(int); char* getWordFromList(int);
bool wordGuessed(char[],char[]); bool wordGuessed(char[],char[]);
bool letterGuessed(char, char[], int, char[]); bool letterGuessed(char, char[], int, char[]);
void changeLetter(char, char[], int, char[]);
#endif #endif
Loading…
Cancel
Save