From d9fcbbe57937cde4e2894ce6f1492c0e8af4e7ea Mon Sep 17 00:00:00 2001 From: KRUGSON Date: Wed, 8 Feb 2023 15:27:18 +0100 Subject: [PATCH] refactoring: combine util functions in one file --- src/c/helper.h | 3 ++ src/c/items.c | 2 +- src/c/main.c | 12 +++--- src/c/map.h | 2 +- src/c/nav_helper.c | 43 ---------------------- src/c/nav_helper.h | 13 ------- src/c/utils.c | 38 ++++++++++++++++++- src/c/utils.h | 8 ++++ test/c/test_commands.c | 3 +- test/c/test_items.c | 3 +- test/c/test_map.c | 3 +- test/c/test_player.c | 3 +- test/c/{test_nav_helper.c => test_utils.c} | 5 ++- 13 files changed, 67 insertions(+), 71 deletions(-) delete mode 100644 src/c/nav_helper.c delete mode 100644 src/c/nav_helper.h rename test/c/{test_nav_helper.c => test_utils.c} (89%) diff --git a/src/c/helper.h b/src/c/helper.h index c1a5799..d439e06 100644 --- a/src/c/helper.h +++ b/src/c/helper.h @@ -2,6 +2,9 @@ #define HELPER_H //erlaubt es z.b. rand() zu mocken +#include +#include + int randomInt(); diff --git a/src/c/items.c b/src/c/items.c index 930e59e..d9f66a7 100644 --- a/src/c/items.c +++ b/src/c/items.c @@ -3,7 +3,7 @@ #include #include -#include "nav_helper.h" +#include "utils.h" #include "items.h" Item *getItems(char *itemsMapFile) diff --git a/src/c/main.c b/src/c/main.c index ab164ee..da0e210 100644 --- a/src/c/main.c +++ b/src/c/main.c @@ -5,7 +5,7 @@ #include #include "map.h" -#include "nav_helper.h" +#include "utils.h" #include "items.h" #include "shop.h" #include "player.h" @@ -16,7 +16,7 @@ bool gameRunning; bool acceptedRules; // declare needed variables -Room *map; //game Map +Room *gameMap; //game Map Item *availableItems; //shop items Player actualPlayer; //player int inputCounter = 0; //need to check on some positions @@ -86,7 +86,7 @@ void setUp() gameRunning = 1; // get Content - map = getMap(gameMapFile); + gameMap = getMap(gameMapFile); availableItems = getItems(itemsMapFile); } @@ -129,7 +129,7 @@ void processInput(char userInput[20]) { lastPlayerPosition = playerPosition; //playerPosition doesn't change but lastPlayerPosition needs update - Room actualRoom = map[playerPosition]; + Room actualRoom = gameMap[playerPosition]; if (actualRoom.shopAvailable == 1) { int *result = malloc(sizeof(int) * 2); @@ -172,7 +172,7 @@ int checkExit(char userInput[20]) // check is user moved int checkMove(char userInput[20]) { - Room r = map[playerPosition]; + Room r = gameMap[playerPosition]; inputCounter += 1; if (strcmp(userInput, "north") == 0) { @@ -232,6 +232,6 @@ void printStatus() if (lastPlayerPosition != playerPosition || lastPlayerPosition == playerPosition || playerPosition == 0 && inputCounter == 0) { - printRoomStatus(moveMessage, map[playerPosition], playerPosition); + printRoomStatus(moveMessage, gameMap[playerPosition], playerPosition); } } \ No newline at end of file diff --git a/src/c/map.h b/src/c/map.h index 1cff482..c90b57f 100644 --- a/src/c/map.h +++ b/src/c/map.h @@ -7,7 +7,7 @@ #include #include -#include "nav_helper.h" +#include "utils.h" //defs #define mapMax 4 // for map (adjust to txt count of rooms -> game.map) diff --git a/src/c/nav_helper.c b/src/c/nav_helper.c deleted file mode 100644 index 5a2d670..0000000 --- a/src/c/nav_helper.c +++ /dev/null @@ -1,43 +0,0 @@ -//bibs -#include -#include -#include - -//headers -#include "nav_helper.h" - -bool startsWith(const char *a, const char *b) -{ - if (strncmp(a, b, strlen(b)) == 0) - return 1; - return 0; -}; - -FILE *getStream(char *filePath) -{ - FILE *stream; - stream = fopen(filePath, "r"); - if (stream == NULL) - { - printf("ERROR: couldn't open or find file: %s !\n", filePath); - exit(EXIT_FAILURE); // exit - } - - return stream; -} - -void printStreamLines(FILE *stream){ - - char *line = NULL; - size_t len = 0; - ssize_t read; - - /* print line by line from file */ - while ((read = getline(&line, &len, stream)) != -1) - { - // printf("Retrieved line of length %u :\n", read); - printf("%s", line); - } - - free(line); /* Deallocate allocated memory */ -} diff --git a/src/c/nav_helper.h b/src/c/nav_helper.h deleted file mode 100644 index 056a4f1..0000000 --- a/src/c/nav_helper.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef NAV_HELPER_H -#define NAV_HELPER_H - -#include -#include -#include -#include - -bool startsWith(const char *a, const char *b); -FILE *getStream(char *filePath); -void printStreamLines(FILE *stream); - -#endif \ No newline at end of file diff --git a/src/c/utils.c b/src/c/utils.c index 8686724..0b2433f 100644 --- a/src/c/utils.c +++ b/src/c/utils.c @@ -1,3 +1,4 @@ + #include "utils.h" #include "helper.h" @@ -7,9 +8,44 @@ int randomIntRange(int min, int max) return (value % (max - min + 1)) + min; } - int map(int x, int in_min, int in_max, int out_min, int out_max) { //vgl Arduino map() https://www.arduino.cc/reference/en/language/functions/math/map/ return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; +} + +bool startsWith(const char *a, const char *b) +{ + if (strncmp(a, b, strlen(b)) == 0) + return 1; + return 0; +}; + +FILE *getStream(char *filePath) +{ + FILE *stream; + stream = fopen(filePath, "r"); + if (stream == NULL) + { + printf("ERROR: couldn't open or find file: %s !\n", filePath); + exit(EXIT_FAILURE); // exit + } + + return stream; +} + +void printStreamLines(FILE *stream){ + + char *line = NULL; + size_t len = 0; + ssize_t read; + + /* print line by line from file */ + while ((read = getline(&line, &len, stream)) != -1) + { + // printf("Retrieved line of length %u :\n", read); + printf("%s", line); + } + + free(line); /* Deallocate allocated memory */ } \ No newline at end of file diff --git a/src/c/utils.h b/src/c/utils.h index eed2e44..4bd8622 100644 --- a/src/c/utils.h +++ b/src/c/utils.h @@ -1,7 +1,15 @@ #ifndef UTILS_H #define UTILS_H +#include +#include +#include +#include + int randomIntRange(int min, int max); int map(int x, int in_min, int in_max, int out_min, int out_max); +bool startsWith(const char *a, const char *b); +FILE *getStream(char *filePath); +void printStreamLines(FILE *stream); #endif \ No newline at end of file diff --git a/test/c/test_commands.c b/test/c/test_commands.c index 052a6cb..ade7f49 100644 --- a/test/c/test_commands.c +++ b/test/c/test_commands.c @@ -3,7 +3,8 @@ #include "unity.h" #include "commands.h" #include "map.h" -#include "nav_helper.h" +#include "utils.h" +#include "helper.h" void setUp(void) { diff --git a/test/c/test_items.c b/test/c/test_items.c index 8083d3b..63fbc04 100644 --- a/test/c/test_items.c +++ b/test/c/test_items.c @@ -1,8 +1,9 @@ #ifdef TEST #include "unity.h" -#include "nav_helper.h" #include "items.h" +#include "utils.h" +#include "helper.h" void setUp(void) { diff --git a/test/c/test_map.c b/test/c/test_map.c index fce9775..0515286 100644 --- a/test/c/test_map.c +++ b/test/c/test_map.c @@ -2,7 +2,8 @@ #include "unity.h" #include "map.h" -#include "nav_helper.h" +#include "utils.h" +#include "helper.h" void setUp(void) diff --git a/test/c/test_player.c b/test/c/test_player.c index 5e86752..6aa75ef 100644 --- a/test/c/test_player.c +++ b/test/c/test_player.c @@ -3,7 +3,8 @@ #include "unity.h" #include "player.h" #include "items.h" -#include "nav_helper.h" +#include "utils.h" +#include "helper.h" Item *availableItems; Player actualPlayer; diff --git a/test/c/test_nav_helper.c b/test/c/test_utils.c similarity index 89% rename from test/c/test_nav_helper.c rename to test/c/test_utils.c index cbf1d42..0831235 100644 --- a/test/c/test_nav_helper.c +++ b/test/c/test_utils.c @@ -1,7 +1,8 @@ #ifdef TEST #include "unity.h" -#include "nav_helper.h" +#include "utils.h" +#include "helper.h" void setUp(void) { @@ -11,7 +12,7 @@ void tearDown(void) { } -void test_nav_helper(void) +void test_startsWith(void) { /* arrange */ // Hier die Werte eingeben