From c6e7ed22bdf9a39a6353b9daf96cfe67d053c17c Mon Sep 17 00:00:00 2001 From: KRUGSON Date: Wed, 8 Feb 2023 14:51:55 +0100 Subject: [PATCH 1/3] refactoring: improved map output&input handl. shop --- src/c/commands.c | 6 +- src/c/main | Bin 0 -> 27120 bytes src/c/main.c | 13 +- src/c/print_helper.c | 61 ++++++-- src/c/print_helper.h | 4 + src/c/shop.c | 223 ++++++++++++++++++------------ src/content/game_instructions.txt | 1 + test/c/test_commands.c | 2 +- 8 files changed, 194 insertions(+), 116 deletions(-) create mode 100755 src/c/main diff --git a/src/c/commands.c b/src/c/commands.c index 7b7eeff..b01b2de 100644 --- a/src/c/commands.c +++ b/src/c/commands.c @@ -13,17 +13,17 @@ char* getPossibleCommands(Room r, int playerPosition) strcpy(msg, ""); //assign char (string) that no leading zero result if (playerPosition > 0) { - strcat(msg, "You can move south from here\n"); + strcat(msg, "> You can move south from here\n"); } if (playerPosition < mapMax - 1) { - strcat(msg, "You can move north from here\n"); + strcat(msg, "> You can move north from here\n"); } if (r.shopAvailable == 1) { - strcat(msg, "You can call 'shop' to buy items.\n"); + strcat(msg, "> You can call 'shop' to buy items.\n"); } return msg; diff --git a/src/c/main b/src/c/main new file mode 100755 index 0000000000000000000000000000000000000000..46cb679f7c10d295e623952b505d70dc8435c790 GIT binary patch literal 27120 zcmeHw4R}=5wf0HEXhbx@5{vl5K|y{jF(A?iMJK|+111t7ser}FkPIX>nTeShNK`O5 z=9c4OTI_{NE#9X6>@BUdr7fjs1x8dtt<_+;Hoe*!Eo~>p))Z-t);9NDYwvyL%$TWd zzxMl{`#c^^*52=0d+oi~Ui<&#aFuWW0Wqz!i0;BirZ|g0HVir}?tpTdx$jN=N3RbR{xfiA<;H zfHZ`n%ARBs`YDxqGieEU^eCE{u3o0gqzgncO;O5&o@Kv^)cqs*RmybThR~Bq%Vqr( zRr%eBbYz#m^-;~dROXkh9eSmIDk`(rE^lmDJ$u&Wjdjx-8-mf+>8+)+r_Y{M91a!F z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EGEZ_S2pYQPm-I1u4fQN9`swP?OToj>BI z79hM57d`>HGz3R{gf>lSqdy#}Gjjl!)xo&A$jdY<(x)4#dS?1KzUmJ<~8%5DI zx@cTRm!*6GI3HI!xaguD07HoH(UO_SAxWxi9lnRlU#z8OXlhO?SmsA;eRbiGuO9x2 zicv9DC4GyQKyzI~&=(B{>NI~{9jUJhrP^4QKRrj$3mjXEF9@dCTyISd9+|2XLK5H*}Tp}B-%!3}=gbp!9i4-b~to%(4P$rVY z&$VKjb};1)GyDGC5S@S0+BH|E%>4Hs)OtuMR_k09r{xzN{i*cX{+$CQuhuy#PRmJm zdb)R@N70EQ>iov?G3}xAB6oUqUZtp>CQWA{>Ca7w{t;)Es{HC)SJ5d_9LpVkGRyA= zdM18UdUfur=w`{FGLr37dbNLS07g|->D4*1qUzijBK&09pY{=SCacoZU+~bQs9dgV zs{Be$(T$L$GhP)}=e&y6kTN`(SQJqF>EO-z?926;*Po3@WbZ zo{aSEGQFZ+S^sS5AIV6s_Nj`#Jx!$)ROPGCLmBDSd5)s${zS=V+V3fuUbVmGm6DP^ zhkc0qQSGO&A0t3jRO!_@qN3{G9Z~X``O`kvonF6ANGfW+PyWAx_}Tc8!;W)atp0tH z%<`(ZqQ6Jl0#|xo{|pBCuF7AXud07fT6S0uWCx|4#i<`6_xE61&rDHBz=x&?%{;98&2NOz$8Nrt20Bg#>+M#r playerPosition) @@ -233,16 +232,6 @@ void printStatus() if (lastPlayerPosition != playerPosition || lastPlayerPosition == playerPosition || playerPosition == 0 && inputCounter == 0) { - printf("\n\n################################################################################\n"); - - printf("--> %s <--\n", moveMessage); - printf("%s\n", actualRoom.nameRoom); - printf("%s\n", actualRoom.msgRoom); - printf("\n"); - - char *possibleCommands = malloc(sizeof(char) * (500)); - possibleCommands = getPossibleCommands(map[playerPosition], playerPosition); - printf("%s", possibleCommands); - free(possibleCommands); + printRoomStatus(moveMessage, map[playerPosition], playerPosition); } } \ No newline at end of file diff --git a/src/c/print_helper.c b/src/c/print_helper.c index 2a07bd0..9907a72 100644 --- a/src/c/print_helper.c +++ b/src/c/print_helper.c @@ -5,6 +5,7 @@ #include #include "print_helper.h" +#include "commands.h" char *append(const char *s, char c); @@ -12,39 +13,71 @@ void printHeader(char *header) { printf("\n"); int len = strlen(header); - - //set + + // set int maxSize = len * 3 + 3; // 2 spaces | left - mid - right = 3 -> 3 times length of header char pHeader[maxSize]; - char ch = '*'; for (int i = 0; i < maxSize; i++) { pHeader[i] = ch; } - printf("%.*s\n", maxSize, pHeader); printf("%-*s %s %-*s \n", len, "", header, len, ""); printf("%.*s\n", maxSize, pHeader); - } void printItem(char *header, char *item, int itemID, int itemPrice) { int len = strlen(header); - int maxSizeForItem = (len * 3 + 3)-10; - + int maxSizeForItem = (len * 3 + 3) - 10; + printf("%-*d %-*s %*d$ \n", 2, itemID, maxSizeForItem, item, 5, itemPrice); } -char *append(const char *s, char c) +void printRoomStatus(char *moveMessage, Room currentRoom, int playerPosition) { - int len = strlen(s); - char buf[len + 2]; - strcpy(buf, s); - buf[len] = c; - buf[len + 1] = 0; - return strdup(buf); + + int len = strlen(moveMessage); + + int maxSizeRoomStatus = 60; // 2 spaces | left - mid - right = 3 -> 3 times length of header + char roomStatusPlaceholder[maxSizeRoomStatus]; + + int show = maxSizeRoomStatus - len; + + int fullLine = show * 2 + len + 2; + int paddingRoomName = (fullLine - strlen(currentRoom.nameRoom)) / 2; + int paddingRoomStory = (fullLine - strlen(currentRoom.msgRoom)) / 6; + int paddingRoomStoryPlace = fullLine - (paddingRoomStory * 2) - 2; + char seperatorLine[fullLine]; + + char ch = '#'; + for (int i = 0; i < fullLine; i++) + { + roomStatusPlaceholder[i] = ch; + } + char sCh = '-'; + for (int i = 0; i < fullLine; i++) + { + seperatorLine[i] = sCh; + } + + printf("\n"); + printf("%.*s\n", fullLine, seperatorLine); + printf("%.*s %s %.*s \n", show, roomStatusPlaceholder, moveMessage, show, roomStatusPlaceholder); + printf("%.*s\n", fullLine, seperatorLine); + printf("\n"); + printf("%-*s%s %-*s \n\n", paddingRoomName, "", currentRoom.nameRoom, paddingRoomName, ""); + printf("%-*s %-*s %-*s \n\n", paddingRoomStory, "", paddingRoomStoryPlace, currentRoom.msgRoom, paddingRoomStory, ""); + printf("%.*s\n", fullLine, seperatorLine); + printf("%.*s\n", fullLine, roomStatusPlaceholder); + printf("%.*s\n\n", fullLine, seperatorLine); + + char *possibleCommands = malloc(sizeof(char) * (500)); + possibleCommands = getPossibleCommands(currentRoom, playerPosition); + printf("%s\n", possibleCommands); + free(possibleCommands); + printf("please enter what you want to do next:\n"); } diff --git a/src/c/print_helper.h b/src/c/print_helper.h index deaaddd..1d50d2d 100644 --- a/src/c/print_helper.h +++ b/src/c/print_helper.h @@ -1,7 +1,11 @@ #ifndef PRINT_HELPER_H #define PRINT_HELPER_H +#include "map.h" + void printHeader(char *header); void printItem(char *header, char *item, int itemID, int itemPrice); +void printRoomStatus(char *moveMessage, Room currentRoom, int playerPosition); + #endif \ No newline at end of file diff --git a/src/c/shop.c b/src/c/shop.c index 617b2af..fa38f96 100644 --- a/src/c/shop.c +++ b/src/c/shop.c @@ -9,141 +9,192 @@ int *openShop(Item *availableItems, Player player) { - int mode = 0; + int mode = -1; int *x = malloc(sizeof(int) * 2); - int userModeInput; - fflush(stdout); - bool selectMode = 1; - while (selectMode == 1) + char *userModeInput = malloc(sizeof(char) * 1); + do { - printf("Please type '0' for buy or '1' to sell items!\n"); - while (scanf(" %d", &userModeInput) != 1) - { - printf("Invalid [index]. Please try again: "); - fflush(stdout); - } + printf("Please type '0' for buy or '1' to sell items:\n"); + scanf(" %s", userModeInput); - if (userModeInput > 1 || userModeInput < 0) + if (strlen(userModeInput) == 1) { - printf("Invalid [index]. Please try again: "); - } - else if (userModeInput == 0) - { - mode = 0; - selectMode = 0; + if (isdigit(userModeInput[0])) + { + int check = userModeInput[0] - '0'; + if (check > 1 || check < 0) + { + printf("\nInvalid Index.\n"); + } + else if (check == 0) + { + mode = 0; + } + else if (check == 1) + { + mode = 1; + } + } + else + { + printf("\nInvalid input. Input isn't a number.\n"); + } } - else if (userModeInput == 1) + else { - mode = 1; - selectMode = 0; + printf("\nOnly ONE char is needed!\n"); } - } + } while (mode != 0 && mode != 1); - int userInput; fflush(stdout); - bool shopIsOpen = 1; + bool shopIsOpen = true; if (mode == 0) { - - while (shopIsOpen == 1) + char *userInput = malloc(sizeof(char) * 1); + do { + // header char *header = "SHOP-Items"; printHeader(header); - // printf("%-5s %-30s %5s\n", "Index", "Name", "Price"); + // items + int maxItemsShop = 0; for (int i = 0; i < 6; i++) { - //printf("%-5d %-40s %5d$\n", availableItems[i].id, availableItems[i].itemName, availableItems[i].price); + // printf("%-5d %-40s %5d$\n", availableItems[i].id, availableItems[i].itemName, availableItems[i].price); printItem(header, availableItems[i].itemName, availableItems[i].id, availableItems[i].price); + maxItemsShop += 1; } + // instruction printf("\n-> to buy items type '[index of item]' \n-> write '0' to quit the shop'\n\n"); - while (scanf(" %d", &userInput) != 1) - { - printf("Invalid [index]. Please try again: "); - fflush(stdout); - } + // input + scanf(" %s", userInput); - if (userInput > 0) - { - x[0] = mode; - x[1] = userInput; - return x; - // BUY ITEM added later -> addItemToInventory(userInput); - } - else if (userInput == 0) + bool finished = false; + // check input + if (strlen(userInput) == 1) { - shopIsOpen = 0; - printf("Enjoy your items, have a great day!\n"); - x[0] = -1; - x[1] = -1; - return x; + if (isdigit(userInput[0])) + { + int check = userInput[0] - '0'; + + if (check > maxItemsShop) // about max items index -> invalid + { + printf("\nInvalid [index]. Please try again: \n"); + } + else if (check > 0) // all available Indizes + { + shopIsOpen = false; + x[0] = mode; + x[1] = check; + return x; + } + else if (check == 0) + { + shopIsOpen = false; + printf("Enjoy your items, have a great day!\n"); + x[0] = -1; + x[1] = -1; + return x; + } + else + { + printf("Invalid [index]. Please try again. \n"); + } + } + else + { + printf("\nInvalid input. Input isn't a number.\n"); + } } else { - printf("Invalid [index]. Please try again: "); + printf("\nToo many chars entered. Only ONE char is needed. Try again!\n"); } - } - } + } while (shopIsOpen == true); + + } //end mode = 0 else if (mode == 1) { - while (shopIsOpen == 1) + + char *userInput = malloc(sizeof(char) * 1); + if (player.itemCounter > 0)//check if inventory of player is empty { - if (player.itemCounter > 0) + do { + // header char *header = "Your inventory"; - printHeader(header); - // printf("%-5s %-30s %5s\n", "Index", "Name", "Price"); + // print inventory + int maxItemsInv = 0; for (int i = 0; i < player.itemCounter; i++) { - printItem(header, player.itemInventory[i].itemName, i+1, player.itemInventory[i].price); + printItem(header, player.itemInventory[i].itemName, i + 1, player.itemInventory[i].price); + maxItemsInv += 1; } - + // instructions printf("\n-> to sell items type '[index of item]' \n-> write '0' to quit the shop'\n\n"); - while (scanf(" %d", &userInput) != 1) - { - printf("Invalid [index]. Please try again: "); - fflush(stdout); - } + // input + scanf(" %s", userInput); - if (userInput > 0) - { - x[0] = mode; - x[1] = userInput-1; //index begin on 1 because we will cancel with 0 - return x; - // BUY ITEM added later -> addItemToInventory(userInput); - } - else if (userInput == 0) + bool finished = false; + // check input + if (strlen(userInput) == 1) { - shopIsOpen = 0; - printf("Enjoy your money, have a great day!\n"); - x[0] = -1; - x[1] = -1; - return x; + if (isdigit(userInput[0])) + { + int check = userInput[0] - '0'; + if (check > maxItemsInv) // about max items index -> invalid + { + printf("\nInvalid [index]. Please try again: \n"); + } + else if (check > 0) + { + shopIsOpen = false; + x[0] = mode; + x[1] = check - 1; // index begin on 1 because we will cancel with 0 + return x; + } + else if (check == 0) + { + shopIsOpen = false; + printf("Enjoy your money, have a great day!\n"); + x[0] = -1; + x[1] = -1; + return x; + } + else + { + printf("Invalid [index]. Please try again: "); + } + } + else + { + printf("\nInvalid input. Input isn't a number.\n"); + } } else { - printf("Invalid [index]. Please try again: "); + printf("\nToo many chars entered. Only ONE char is needed. Try again!\n"); } - } - else - { - shopIsOpen = 0; - printf("*** Your inventory is empty *** \n\n"); - x[0] = -1; - x[1] = -1; - return x; - } + } while (shopIsOpen == true); + } - } + else //when inventory of player is empty + { + printf("*** Your inventory is empty *** \n\n"); + x[0] = -1; + x[1] = -1; + return x; + } + + }//end mode = 1 - x[0] = -1; - return x; } \ No newline at end of file diff --git a/src/content/game_instructions.txt b/src/content/game_instructions.txt index e089751..10eea1c 100644 --- a/src/content/game_instructions.txt +++ b/src/content/game_instructions.txt @@ -27,6 +27,7 @@ enter "south" to go south alltime commands: - 'inventory' -> will show your items that you have bought or found () +- 'commands' -> to show available comamnds list from this spot special commands (not available at any time during the game): - 'shop' -> opens the shop where you can buy or sell items diff --git a/test/c/test_commands.c b/test/c/test_commands.c index aa20fa0..052a6cb 100644 --- a/test/c/test_commands.c +++ b/test/c/test_commands.c @@ -20,7 +20,7 @@ void test_commands(void) Room* mapData; mapData = getMap("./src/content/game.map"); int playerPosition = 3; //last player position in north - so you can only move to south - char* expectedString = "You can move south from here\n"; + char* expectedString = "> You can move south from here\n"; /* act */ // Die Funktion wird ausgeführt From e41e132f664160a2089df6a0a2b3f30d2a0f0bb8 Mon Sep 17 00:00:00 2001 From: fdai7372 Date: Wed, 8 Feb 2023 13:53:31 +0000 Subject: [PATCH 2/3] Delete executable main - not needed --- src/c/main | Bin 27120 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100755 src/c/main diff --git a/src/c/main b/src/c/main deleted file mode 100755 index 46cb679f7c10d295e623952b505d70dc8435c790..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 27120 zcmeHw4R}=5wf0HEXhbx@5{vl5K|y{jF(A?iMJK|+111t7ser}FkPIX>nTeShNK`O5 z=9c4OTI_{NE#9X6>@BUdr7fjs1x8dtt<_+;Hoe*!Eo~>p))Z-t);9NDYwvyL%$TWd zzxMl{`#c^^*52=0d+oi~Ui<&#aFuWW0Wqz!i0;BirZ|g0HVir}?tpTdx$jN=N3RbR{xfiA<;H zfHZ`n%ARBs`YDxqGieEU^eCE{u3o0gqzgncO;O5&o@Kv^)cqs*RmybThR~Bq%Vqr( zRr%eBbYz#m^-;~dROXkh9eS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2BEm#dllN#9@W? z&iOnktZi;f6JT2`4TYMPMf{Oym_PdZUIEGEZ_S2pYQPm-I1u4fQN9`swP?OToj>BI z79hM57d`>HGz3R{gf>lSqdy#}Gjjl!)xo&A$jdY<(x)4#dS?1KzUmJ<~8%5DI zx@cTRm!*6GI3HI!xaguD07HoH(UO_SAxWxi9lnRlU#z8OXlhO?SmsA;eRbiGuO9x2 zicv9DC4GyQKyzI~&=(B{>NI~{9jUJhrP^4QKRrj$3mjXEF9@dCTyISd9+|2XLK5H*}Tp}B-%!3}=gbp!9i4-b~to%(4P$rVY z&$VKjb};1)GyDGC5S@S0+BH|E%>4Hs)OtuMR_k09r{xzN{i*cX{+$CQuhuy#PRmJm zdb)R@N70EQ>iov?G3}xAB6oUqUZtp>CQWA{>Ca7w{t;)Es{HC)SJ5d_9LpVkGRyA= zdM18UdUfur=w`{FGLr37dbNLS07g|->D4*1qUzijBK&09pY{=SCacoZU+~bQs9dgV zs{Be$(T$L$GhP)}=e&y6kTN`(SQJqF>EO-z?926;*Po3@WbZ zo{aSEGQFZ+S^sS5AIV6s_Nj`#Jx!$)ROPGCLmBDSd5)s${zS=V+V3fuUbVmGm6DP^ zhkc0qQSGO&A0t3jRO!_@qN3{G9Z~X``O`kvonF6ANGfW+PyWAx_}Tc8!;W)atp0tH z%<`(ZqQ6Jl0#|xo{|pBCuF7AXud07fT6S0uWCx|4#i<`6_xE61&rDHBz=x&?%{;98&2NOz$8Nrt20Bg#>+M#r Date: Wed, 8 Feb 2023 15:27:18 +0100 Subject: [PATCH 3/3] 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