Browse Source

refactoring: shop output embellished

remotes/origin/fdai7372-main-patch-93194
KRUGSON 2 years ago
parent
commit
efb5ba1d75
  1. 50
      src/c/print_helper.c
  2. 7
      src/c/print_helper.h
  3. 13
      src/c/shop.c
  4. 1
      src/c/shop.h

50
src/c/print_helper.c

@ -0,0 +1,50 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
#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);
}

7
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

13
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)

1
src/c/shop.h

@ -3,6 +3,7 @@
#include "items.h"
#include "player.h"
#include "print_helper.h"
int *openShop(Item *availableItems, Player player);
Loading…
Cancel
Save