diff --git a/src/main/c/Tim/hangman.c b/src/main/c/Tim/hangman.c index d49319b..08886d6 100644 --- a/src/main/c/Tim/hangman.c +++ b/src/main/c/Tim/hangman.c @@ -1,7 +1,261 @@ #include "hangman.h" #include +#include +#include +#include +#include +#include + + + + +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=0 && x0) + { + 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= 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"); +} \ No newline at end of file diff --git a/src/main/c/Tim/hangman.h b/src/main/c/Tim/hangman.h index cd17d02..f68abf8 100644 --- a/src/main/c/Tim/hangman.h +++ b/src/main/c/Tim/hangman.h @@ -1,6 +1,22 @@ +#include #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 diff --git a/src/test/c/Tim/test_hangman.c b/src/test/c/Tim/test_hangman.c index 2afdf17..47f8e40 100644 --- a/src/test/c/Tim/test_hangman.c +++ b/src/test/c/Tim/test_hangman.c @@ -1,5 +1,7 @@ #include "hangman.h" #include "unity.h" +#include +#include @@ -13,12 +15,165 @@ void tearDown(void) } -void test_ceedling_functionality() +void test_getWordFromList_Kartoffel_0() { //arrange - int expectedResult = 0; + int pos = 0; + char expectedResult[] = "Kartoffel"; //act - int actualResult = 0; + char actualResult[30]; + strcpy(actualResult,getWordFromList(pos)); //assert - TEST_ASSERT_EQUAL_INT(expectedResult, actualResult); -} \ No newline at end of file + TEST_ASSERT_EQUAL_STRING(expectedResult, actualResult); +} + +void test_getWordFromList_Kleiderschrank_5() +{ + //arrange + int pos = 5; + char expectedResult[] = "Kleiderschrank"; + //act + char actualResult[30]; + strcpy(actualResult,getWordFromList(pos)); + //assert + TEST_ASSERT_EQUAL_STRING(expectedResult, actualResult); +} + + +void test_getWordFromList_Index_lower_Listsize() +{ + //arrange + int pos = -5; + char expectedResult[] = "Index nicht vorhanden"; + //act + char actualResult[30]; + strcpy(actualResult,getWordFromList(pos)); + //assert + TEST_ASSERT_EQUAL_STRING(expectedResult, actualResult); +} +void test_getWordFromList_Index_higher_Listsize() +{ + //arrange + int pos = LISTSIZE+1; + char expectedResult[] = "Index nicht vorhanden"; + //act + char actualResult[30]; + strcpy(actualResult,getWordFromList(pos)); + //assert + TEST_ASSERT_EQUAL_STRING(expectedResult, actualResult); +} + + +void test_wonGame_wordGuessed() +{ + //arrange + char word1[] ="Kartoffel"; + char word2[] = "Kartoffel"; + //assert + TEST_ASSERT_TRUE(wordGuessed(word1, word2)); +} + +void test_not_wordGuessed() +{ + //arrange + char word1[] ="Kartoffel"; + char word2[] ="Thunfisch"; + //assert + TEST_ASSERT_FALSE(wordGuessed(word1, word2)); +} + +void test_wordGuessed_differentCaps() +{ + //arrange + char word1[] ="Kartoffel"; + char word2[] ="karTOFFel"; + //assert + TEST_ASSERT_FALSE(wordGuessed(word1, word2)); +} + + +void test_letterGuessed_differentCaps_small_big() +{ + //arrange + char x ='F'; + char y[] ="Kartoffel"; + int length = 9; + //assert + TEST_ASSERT_TRUE(letterGuessed(x,y,length)); +} + +void test_letterGuessed_differentCaps_big_small() +{ + //arrange + char x ='k'; + char y[] ="Kartoffel"; + int length = 9; + //assert + TEST_ASSERT_TRUE(letterGuessed(x,y,length)); +} +void test_letterGuessed_sameCaps_small() +{ + //arrange + char x ='f'; + char y[] ="Kartoffel"; + int length = 9; + //assert + TEST_ASSERT_TRUE(letterGuessed(x,y,length)); +} +void test_letterGuessed_differentLetter_small() +{ + //arrange + char x ='p'; + char y[] ="Kartoffel"; + int length = 9; + //assert + TEST_ASSERT_FALSE(letterGuessed(x,y,length)); +} +void test_letterGuessed_differentLetter_big() +{ + //arrange + char x ='P'; + char y[] ="Kartoffel"; + int length = 9; + //assert + TEST_ASSERT_FALSE(letterGuessed(x,y,length)); +} + + +void test_letterGuessed_sameCaps_big() +{ + //arrange + char x ='K'; + char y[] ="Kartoffel"; + int length = 9; + //assert + TEST_ASSERT_TRUE(letterGuessed(x,y,length)); +} + +void test_noTrysLeft_x_equals_POSSIBLE_TRYS() +{ + //arrange + char x = POSSIBLE_TRYS; + char y[] ="Kartoffel"; + //assert + TEST_ASSERT_TRUE(noTrysLeft(x, y)); +} + +void test_noTrysLeft_x_lower_POSSIBLE_TRYS() +{ + //arrange + char x = POSSIBLE_TRYS-2; + char y[] ="Kartoffel"; + //assert + TEST_ASSERT_FALSE(noTrysLeft(x, y)); +} + +void test_noTrysLeft_x_higher_POSSIBLE_TRYS() +{ + //arrange + char x = POSSIBLE_TRYS+2; + char y[] ="Kartoffel"; + //assert + TEST_ASSERT_TRUE(noTrysLeft(x, y)); +} +