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/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 b85804e..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) { @@ -211,7 +211,6 @@ int checkMove(char userInput[20]) // print actual location and messages from game.map void printStatus() { - Room actualRoom = map[playerPosition]; char moveMessage[30]; if (lastPlayerPosition > 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, 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/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/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/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..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) { @@ -20,7 +21,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 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