Browse Source

Merge branch 'tim' into dev

remotes/origin/jason
fdai7727 11 months ago
parent
commit
6178f5041f
  1. 256
      src/main/c/Tim/hangman.c
  2. 16
      src/main/c/Tim/hangman.h
  3. 165
      src/test/c/Tim/test_hangman.c

256
src/main/c/Tim/hangman.c

@ -1,7 +1,261 @@
#include "hangman.h" #include "hangman.h"
#include <stdio.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() 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");
}

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

@ -1,6 +1,22 @@
#include <stdbool.h>
#ifndef HANGMAN_H #ifndef HANGMAN_H
#define 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 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 #endif

165
src/test/c/Tim/test_hangman.c

@ -1,5 +1,7 @@
#include "hangman.h" #include "hangman.h"
#include "unity.h" #include "unity.h"
#include <string.h>
#include <stdbool.h>
@ -13,12 +15,165 @@ void tearDown(void)
} }
void test_ceedling_functionality()
void test_getWordFromList_Kartoffel_0()
{ {
//arrange //arrange
int expectedResult = 0;
int pos = 0;
char expectedResult[] = "Kartoffel";
//act //act
int actualResult = 0;
char actualResult[30];
strcpy(actualResult,getWordFromList(pos));
//assert //assert
TEST_ASSERT_EQUAL_INT(expectedResult, actualResult);
}
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));
}
Loading…
Cancel
Save