From efb5ba1d75ec836d010a38dcfaabcbbaf58c3494 Mon Sep 17 00:00:00 2001 From: KRUGSON Date: Tue, 7 Feb 2023 17:58:18 +0100 Subject: [PATCH] refactoring: shop output embellished --- src/c/print_helper.c | 50 ++++++++++++++++++++++++++++++++++++++++++++ src/c/print_helper.h | 7 +++++++ src/c/shop.c | 13 ++++++++---- src/c/shop.h | 1 + 4 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 src/c/print_helper.c create mode 100644 src/c/print_helper.h diff --git a/src/c/print_helper.c b/src/c/print_helper.c new file mode 100644 index 0000000..2a07bd0 --- /dev/null +++ b/src/c/print_helper.c @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include + +#include "print_helper.h" + +char *append(const char *s, char c); + +void printHeader(char *header) +{ + printf("\n"); + int len = strlen(header); + + //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; + + printf("%-*d %-*s %*d$ \n", 2, itemID, maxSizeForItem, item, 5, itemPrice); +} + +char *append(const char *s, char c) +{ + int len = strlen(s); + char buf[len + 2]; + strcpy(buf, s); + buf[len] = c; + buf[len + 1] = 0; + return strdup(buf); +} diff --git a/src/c/print_helper.h b/src/c/print_helper.h new file mode 100644 index 0000000..deaaddd --- /dev/null +++ b/src/c/print_helper.h @@ -0,0 +1,7 @@ +#ifndef PRINT_HELPER_H +#define PRINT_HELPER_H + +void printHeader(char *header); +void printItem(char *header, char *item, int itemID, int itemPrice); + +#endif \ No newline at end of file diff --git a/src/c/shop.c b/src/c/shop.c index b8f7d53..617b2af 100644 --- a/src/c/shop.c +++ b/src/c/shop.c @@ -49,12 +49,14 @@ int *openShop(Item *availableItems, Player player) while (shopIsOpen == 1) { - printf("*** SHOP-Items *** \n\n"); + char *header = "SHOP-Items"; + printHeader(header); // printf("%-5s %-30s %5s\n", "Index", "Name", "Price"); 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); } printf("\n-> to buy items type '[index of item]' \n-> write '0' to quit the shop'\n\n"); @@ -92,14 +94,17 @@ int *openShop(Item *availableItems, Player player) { if (player.itemCounter > 0) { - printf("*** Your inventory *** \n\n"); + char *header = "Your inventory"; + + printHeader(header); // printf("%-5s %-30s %5s\n", "Index", "Name", "Price"); for (int i = 0; i < player.itemCounter; i++) { - printf("%-5d %-40s %5d$\n", i+1, player.itemInventory[i].itemName, player.itemInventory[i].price); + printItem(header, player.itemInventory[i].itemName, i+1, player.itemInventory[i].price); } + printf("\n-> to sell items type '[index of item]' \n-> write '0' to quit the shop'\n\n"); while (scanf(" %d", &userInput) != 1) diff --git a/src/c/shop.h b/src/c/shop.h index 56e1603..deaaf5c 100644 --- a/src/c/shop.h +++ b/src/c/shop.h @@ -3,6 +3,7 @@ #include "items.h" #include "player.h" +#include "print_helper.h" int *openShop(Item *availableItems, Player player);