fdai7727
11 months ago
3 changed files with 431 additions and 6 deletions
@ -1,7 +1,261 @@ |
|||
#include "hangman.h" |
|||
#include <stdio.h> |
|||
#include <string.h> |
|||
#include <stdlib.h> |
|||
#include <time.h> |
|||
#include <stdbool.h> |
|||
#include <ctype.h> |
|||
|
|||
|
|||
|
|||
|
|||
char wordlist[LISTSIZE][MAX_WORD_LENGTH] = { |
|||
"Kartoffel", "Zigarette", "Haus", "Fenster", "Kartenleseettiketiergeraet", |
|||
"Kleiderschrank", "Schnee","Wasserhahn","Fernbedienung", |
|||
"Computertastatur", "Verlies","Zucchini","lizenzieren", |
|||
"Portemonnaie","brillant","Rückgrat","Toilettenpapier", |
|||
"Dachpappe","Hund","Zwiebelsuppe","Zebra", |
|||
"Kruzifix","Anschnallgurt","Bügeleisen","Fliesenleger", |
|||
"Adventskranz","Weihnachtsbaum","Autoreifen","Waschbecken", |
|||
"Busfahrkarte" |
|||
}; |
|||
|
|||
char hangmanStages[STAGENUM][ASCII_ART_SIZE]={ |
|||
"+---+\n" |
|||
"| |\n" |
|||
"|\n" |
|||
"|\n" |
|||
"|\n" |
|||
"|\n" |
|||
"=========\n", |
|||
"+---+\n" |
|||
"| |\n" |
|||
"| O\n" |
|||
"|\n" |
|||
"|\n" |
|||
"|\n" |
|||
"=========\n", |
|||
"+---+\n" |
|||
"| |\n" |
|||
"| O\n" |
|||
"| |\n" |
|||
"|\n" |
|||
"|\n" |
|||
"=========\n", |
|||
"+---+\n" |
|||
"| |\n" |
|||
"| O\n" |
|||
"| /|\n" |
|||
"|\n" |
|||
"|\n" |
|||
"=========\n", |
|||
"+---+\n" |
|||
"| |\n" |
|||
"| O\n" |
|||
"| /|\\\n" |
|||
"|\n" |
|||
"|\n" |
|||
"=========\n", |
|||
"+---+\n" |
|||
"| |\n" |
|||
"| O\n" |
|||
"| /|\\\n" |
|||
"| /\n" |
|||
"|\n" |
|||
"=========\n", |
|||
"+---+\n" |
|||
"| |\n" |
|||
"| O\n" |
|||
"| /|\\\n" |
|||
"| / \\\n" |
|||
"|\n" |
|||
"=========\n", |
|||
}; |
|||
|
|||
|
|||
void hangman() |
|||
{ |
|||
printf("Hello World!"); |
|||
srand(time(NULL)); |
|||
char userSelection; |
|||
getWelcomeMessageHangman(); |
|||
|
|||
do |
|||
{ |
|||
char guessWord[MAX_WORD_LENGTH]; |
|||
int length; |
|||
int countWrongGuess=0; |
|||
strcpy(guessWord,getWordFromList(rand() % LISTSIZE)); |
|||
length = strlen(guessWord); |
|||
|
|||
char displayWord[MAX_WORD_LENGTH]; |
|||
drawHangman(countWrongGuess); |
|||
for (int i = 0; i <= length; i++) |
|||
{ |
|||
if(i<length) |
|||
{ |
|||
displayWord[i] = '_'; |
|||
} |
|||
else if(i == length) |
|||
{ |
|||
displayWord[i] = '\0'; |
|||
} |
|||
} |
|||
printf("\n\n%s\n", displayWord); |
|||
|
|||
while(1) |
|||
{ |
|||
printf("Bitte gib einen Buchstaben ein!\n"); |
|||
scanf(" %c", &userSelection); |
|||
if(userSelection == '0') |
|||
{ |
|||
break; |
|||
} |
|||
if(!letterGuessed(userSelection, guessWord, length)) |
|||
{ |
|||
countWrongGuess++; |
|||
printf("Der von dir getippte Buchstabe war falsch! Du hast noch %d Versuche.\n", POSSIBLE_TRYS-countWrongGuess); |
|||
} |
|||
changeLetter(userSelection, guessWord, length, displayWord); |
|||
drawHangman(countWrongGuess); |
|||
if(wordGuessed(guessWord, displayWord)||noTrysLeft(countWrongGuess,guessWord)) |
|||
{ |
|||
break; |
|||
} |
|||
printf("\n\n%s\n", displayWord); |
|||
} |
|||
userSelection = endGame(); |
|||
}while(userSelection != '0'); |
|||
printf("Danke fuers Spielen! Auf Wiedersehen!\n"); |
|||
} |
|||
|
|||
void getWelcomeMessageHangman() |
|||
{ |
|||
printf("Willkommen bei Hangman!!!\n\n"); |
|||
drawHangman(6); |
|||
printf("\nPer Zufall wird jede Runde ein Wort aus einem Pool ausgewaehlt. Gebe einen Buchstaben ein, von dem du vermutest, dass er in dem gesuchten Wort ist, sobald die Konsole dich dazu auffordert.\n" |
|||
"Fuer jede falsche Antwort kommst du dem Tod immer naeher, also waehle weise!\n" |
|||
"Mit der Auswahl 0 kommst du zurueck ins Hauptmenue\n\n"); |
|||
} |
|||
|
|||
char* getWordFromList(int x) |
|||
{ |
|||
if(x>=0 && x<LISTSIZE) |
|||
{ |
|||
return wordlist[x]; |
|||
} |
|||
else |
|||
{ |
|||
return "Index nicht vorhanden"; |
|||
} |
|||
} |
|||
|
|||
bool wordGuessed(char guessWord[], char displayWord[]) |
|||
{ |
|||
if(strcmp(guessWord, displayWord) == 0) |
|||
{ |
|||
printf("Du hast gewonnen!\nDas gesuchte Wort war \"%s\"\n\n",guessWord); |
|||
return true; |
|||
} |
|||
else |
|||
{ |
|||
return false; |
|||
} |
|||
} |
|||
|
|||
bool letterGuessed(char selectedLetter, char guessWord[], int length) |
|||
{ |
|||
int counter = 0; |
|||
for(int i = 0; i<length; i++) { |
|||
if(isupper(selectedLetter) != 0) |
|||
{ |
|||
if (selectedLetter == guessWord[i] - 32 || selectedLetter == guessWord[i] ) |
|||
{ |
|||
counter++; |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
if (selectedLetter == guessWord[i] || selectedLetter == guessWord[i] + 32) |
|||
{ |
|||
counter++; |
|||
} |
|||
} |
|||
|
|||
} |
|||
if(counter>0) |
|||
{ |
|||
printf("Dein gewaehlter Buchstabe %c war ein Treffer!\n", selectedLetter); |
|||
return true; |
|||
} |
|||
else |
|||
{ |
|||
return false; |
|||
} |
|||
|
|||
} |
|||
|
|||
void changeLetter(char selectedLetter, char guessWord[], int length, char ptrDisplayWord[]) |
|||
{ |
|||
for(int i = 0; i<length; i++) { |
|||
if(isupper(selectedLetter) != 0) |
|||
{ |
|||
if (selectedLetter == guessWord[i] - 32 || selectedLetter == guessWord[i] ) |
|||
{ |
|||
if(i ==0 && isupper(guessWord[0]) != 0) |
|||
{ |
|||
ptrDisplayWord[i] = selectedLetter; |
|||
} |
|||
else |
|||
{ |
|||
ptrDisplayWord[i] = selectedLetter + 32; |
|||
} |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
if (selectedLetter == guessWord[i] || selectedLetter == guessWord[i] + 32) |
|||
{ |
|||
if(i == 0 && isupper(guessWord[0]) != 0) |
|||
{ |
|||
ptrDisplayWord[i] = selectedLetter - 32; |
|||
} |
|||
else { |
|||
ptrDisplayWord[i] = selectedLetter; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
void drawHangman(int wrongGuessCount) |
|||
{ |
|||
printf("%s", hangmanStages[wrongGuessCount]); |
|||
} |
|||
|
|||
bool noTrysLeft(int wrongGuessCount, char guessWord[]) |
|||
{ |
|||
if(wrongGuessCount >= POSSIBLE_TRYS) |
|||
{ |
|||
printf("Du hast verloren!\n\nDas gesuchte Wort war \"%s\"\n\n", guessWord); |
|||
return true; |
|||
} |
|||
else |
|||
{ |
|||
return false; |
|||
} |
|||
} |
|||
|
|||
char endGame() |
|||
{ |
|||
char userSelect; |
|||
endGameQuestionHangman(); |
|||
scanf(" %c", &userSelect); |
|||
|
|||
return userSelect; |
|||
} |
|||
|
|||
|
|||
void endGameQuestionHangman() |
|||
{ |
|||
printf("Moechtest du nochmal spielen?\n\nBeliebige Taste: Nochmal spielen\n 0 : Beenden\n"); |
|||
} |
@ -1,6 +1,22 @@ |
|||
#include <stdbool.h> |
|||
#ifndef HANGMAN_H |
|||
#define HANGMAN_H |
|||
|
|||
#define LISTSIZE 30 |
|||
#define MAX_WORD_LENGTH 30 |
|||
#define STAGENUM 7 |
|||
#define ASCII_ART_SIZE 1000 |
|||
#define POSSIBLE_TRYS 6 |
|||
|
|||
void hangman(); |
|||
void getWelcomeMessageHangman(); |
|||
char* getWordFromList(int); |
|||
bool wordGuessed(char[],char[]); |
|||
bool letterGuessed(char, char[], int); |
|||
void changeLetter(char, char[], int, char[]); |
|||
void drawHangman(int); |
|||
bool noTrysLeft(int,char[]); |
|||
char endGame(); |
|||
void endGameQuestionHangman(); |
|||
|
|||
#endif |
Write
Preview
Loading…
Cancel
Save
Reference in new issue