diff --git a/.gitignore b/.gitignore index 9b3702a..258070d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -Quiz-Game.exe \ No newline at end of file +quiz-game.exe +unittests-exec.exe \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..e554b2a --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "functions.h": "c" + } +} \ No newline at end of file diff --git a/build-project-windows-mingw.bat b/build-project-windows-mingw.bat index c177ca1..79b0b2d 100644 --- a/build-project-windows-mingw.bat +++ b/build-project-windows-mingw.bat @@ -1,2 +1,5 @@ -gcc src/main.c -o Quiz-Game -Quiz-Game.exe \ No newline at end of file +gcc src/*.c -o quiz-game +gcc unittests/*.c src/functions.c -I src -o unittests-exec + +unittests-exec.exe +quiz-game.exe \ No newline at end of file diff --git a/build-project.sh b/build-project.sh index 52a949f..496d02c 100644 --- a/build-project.sh +++ b/build-project.sh @@ -1,2 +1,5 @@ -gcc src/main.c -o Quiz-Game -./Quiz-Game \ No newline at end of file +gcc src/*.c -o quiz-game +gcc unittests/*.c src/functions.c -I src -o unittests-exec + +./unittests-exec +./quiz-game \ No newline at end of file diff --git a/quiz-game.zip b/quiz-game.zip deleted file mode 100644 index 51b6912..0000000 Binary files a/quiz-game.zip and /dev/null differ diff --git a/src/functions.c b/src/functions.c new file mode 100644 index 0000000..4f4112e --- /dev/null +++ b/src/functions.c @@ -0,0 +1,18 @@ +#include "functions.h" + +int resetInput(char* input) +{ + memset(&input[0], 0, sizeof(input)); + return 0; +} + +int printEndMsg(int score, int questCount) +{ + printf("Sie haben %d von %d Fragen richtig beantwortet.\n", score, questCount); + return 0; +} + +int compareStrings(const char* input, const char* buf) +{ + return strcmp(input, buf); +} \ No newline at end of file diff --git a/src/functions.h b/src/functions.h new file mode 100644 index 0000000..f6d72b2 --- /dev/null +++ b/src/functions.h @@ -0,0 +1,11 @@ +#pragma once + +#include +#include + +#include "structs.h" + +int resetInput(char* input); +int printEndMsg(int score, int questCount); +int compareStrings(const char* input, const char* buf); +void startGame(int catCount, char* input, Category* categories); \ No newline at end of file diff --git a/src/main.c b/src/main.c index cfd172a..4484d73 100644 --- a/src/main.c +++ b/src/main.c @@ -1,32 +1,56 @@ -#include -#include +#include "structs.h" +#include "functions.h" -typedef struct +void startGame(int catCount, char* input, Category* categories) { - const char* question; - char answer[256]; -} Question; + int questCount = 0; + int curCatNum = 0; -typedef struct -{ - int size; - Question questions[12]; -} Level; + int level = 0; + int score = 0; -typedef struct -{ - int size; - const char* name; - Level lvlQuestions[10]; -} Category; + for (int i = 0; i < catCount; i++) + { + Level lvlQuestions[10]; + memcpy(lvlQuestions, categories[i].lvlQuestions, sizeof(categories[i].lvlQuestions)); -int main() -{ - int score = 0; - int level = 0; + printf("Kategorie %s\n", categories[i].name); - char input[256]; + while (level < sizeof(lvlQuestions) / sizeof(Level)) + { + for (int i = 0; i < lvlQuestions[level].size; i++) + { + printf(lvlQuestions[level].questions[i].question); + printf(" "); + fgets(input, 256, stdin); + input[strlen(input) - 1] = '\0'; + const char* buf = lvlQuestions[level].questions[i].answer; + if (compareStrings(input, buf)) + { + printf("Falsch. Die richtige Antwort ist: %s.\n", lvlQuestions[level].questions[i].answer); + } + else + { + printf("Richtig!\n"); + score++; + } + resetInput(input); + questCount++; + } + + printf("Level %d abgeschlossen!\n", level + 1); + level++; + } + level = 0; + } + + // Ergebnis + printEndMsg(score, questCount); +} + +int main() +{ Question fussQst1 = {.question = "Welche Fussballmannschaft wurde 1930 Weltmeister?", .answer = "Uruguay"}; Question fussQst2 = {.question = "Welche Fussballmannschaft wurde 1934 Weltmeister?", .answer = "Italien"}; Question fussQst3 = {.question = "Welche Fussballmannschaft wurde 1938 Weltmeister?", .answer = "Italien"}; @@ -262,49 +286,10 @@ int main() printf("Willkommen beim Quiz-Spiel! Beantworten Sie die folgenden Fragen:\n"); - int questCount = 0; - int curCatNum = 0; int catCount = 1; // anzahl an kategorien + char input[256]; - for (int i = 0; i < catCount; i++) - { - Level lvlQuestions[10]; - memcpy(lvlQuestions, categories[i].lvlQuestions, sizeof(categories[i].lvlQuestions)); - - printf("Kategorie %s\n", categories[i].name); - - while (level < sizeof(lvlQuestions) / sizeof(Level)) - { - for (int i = 0; i < lvlQuestions[level].size; i++) - { - printf(lvlQuestions[level].questions[i].question); - printf(" "); - fgets(input, 256, stdin); - input[strlen(input) - 1] = '\0'; - const char* buf = lvlQuestions[level].questions[i].answer; - if (strcmp(input, buf)) - { - printf("Falsch. Die richtige Antwort ist: %s.\n", lvlQuestions[level].questions[i].answer); - } - else - { - printf("Richtig!\n"); - score++; - } - memset(&input[0], 0, sizeof(input)); - - questCount++; - } - - printf("Level %d abgeschlossen!\n", level + 1); - level++; - } - - level = 0; - } - - // Ergebnis - printf("Sie haben %d von %d Fragen richtig beantwortet.\n", score, questCount); + startGame(catCount, input, categories); return 0; } \ No newline at end of file diff --git a/src/structs.h b/src/structs.h new file mode 100644 index 0000000..729cfd1 --- /dev/null +++ b/src/structs.h @@ -0,0 +1,20 @@ +#pragma once + +typedef struct +{ + const char* question; + char answer[256]; +} Question; + +typedef struct +{ + int size; + Question questions[12]; +} Level; + +typedef struct +{ + int size; + const char* name; + Level lvlQuestions[10]; +} Category; \ No newline at end of file diff --git a/unittests/test_1.c b/unittests/test_1.c new file mode 100644 index 0000000..6951562 --- /dev/null +++ b/unittests/test_1.c @@ -0,0 +1,7 @@ +#include "tests.h" + +void test_1() +{ + assert(!printEndMsg(220, 1110)); + printf("TEST 1 ERFOLGREICH\n"); +} \ No newline at end of file diff --git a/unittests/test_main.c b/unittests/test_main.c new file mode 100644 index 0000000..5048a8c --- /dev/null +++ b/unittests/test_main.c @@ -0,0 +1,8 @@ +#include + +#include "tests.h" + +int main() +{ + test_1(); +} \ No newline at end of file diff --git a/unittests/tests.h b/unittests/tests.h new file mode 100644 index 0000000..3604ea9 --- /dev/null +++ b/unittests/tests.h @@ -0,0 +1,8 @@ +#pragma once + +#include +#include + +#include "functions.h" + +void test_1(); \ No newline at end of file