diff --git a/src/c/main.c b/src/c/main.c index a82e827..613cd06 100644 --- a/src/c/main.c +++ b/src/c/main.c @@ -41,6 +41,7 @@ int main() { // define variables actualPlayer.id = 1; + actualPlayer.wallet = 100; char userInput[20]; // maximum defined user input length gameRunning = 1; @@ -138,10 +139,15 @@ void processInput(char userInput[20]) Room actualRoom = map[playerPosition]; if (actualRoom.shopAvailable == 1) { - int result = openShop(availableItems); // result > 0 -> integer = index of item OR result = 0 -> cancel - if (result > 0) + int *result = malloc (sizeof (int) * 2); + result = openShop(availableItems, actualPlayer); // result > 0 -> integer = index of item OR result = 0 -> cancel + if (result[0] == 0) { - actualPlayer = addItemToInventory(availableItems, result, actualPlayer); + actualPlayer = buyItem(availableItems, result[1], actualPlayer); + } + else if (result[0] == 1) + { + actualPlayer = sellItem(result[1], actualPlayer); } } else diff --git a/src/c/player.c b/src/c/player.c index 85602bf..0e3fedd 100644 --- a/src/c/player.c +++ b/src/c/player.c @@ -79,7 +79,7 @@ Player buyItem(Item *availableItems, int itemIndex, Player actualPlayer) { actualPlayer = addItemToInventory(availableItems, itemIndex, actualPlayer); actualPlayer = removeMoneyFromPlayer(actualPlayer, itemPrice); - printf("You bought item for %d$! Your balance is now: %d$\n", itemPrice, actualPlayer.wallet); + printf("You bought item[%d] for %d$! \nYour balance is now: %d$\n", itemIndex, itemPrice, actualPlayer.wallet); return actualPlayer; } else @@ -91,9 +91,11 @@ Player buyItem(Item *availableItems, int itemIndex, Player actualPlayer) Player sellItem(int itemIndex, Player actualPlayer) { + int priceItemSell = actualPlayer.itemInventory[itemIndex].price; actualPlayer = addMoneyToPlayer(actualPlayer, actualPlayer.itemInventory[itemIndex].price / 2); + printf("Item has been sold for 50%% of the purchase price (purchase price: %d$)! \nYour balance is now: %d$\n", priceItemSell, actualPlayer.wallet); + actualPlayer = removeItemFromInventory(itemIndex, actualPlayer); - printf("Item has been sold! Your balance is now: %d$\n", actualPlayer.wallet); return actualPlayer; } diff --git a/src/c/shop.c b/src/c/shop.c index 4f871aa..88e0659 100644 --- a/src/c/shop.c +++ b/src/c/shop.c @@ -2,48 +2,143 @@ #include #include #include +#include #include "shop.h" +#include "player.h" -int openShop(Item *availableItems) +int *openShop(Item *availableItems, Player actualPlayer) { - int userInput; + int mode = 0; + int *x = malloc(sizeof(int) * 2); + + int userModeInput; fflush(stdout); - bool shopIsOpen = 1; - while (shopIsOpen == 1) + bool selectMode = 1; + while (selectMode == 1) { - printf("*** SHOP-Items *** \n\n"); - - // printf("%-5s %-30s %5s\n", "Index", "Name", "Price"); - for (int i = 0; i < 6; i++) + printf("Please type '0' for buy or '1' to sell items!\n"); + while (scanf(" %d", &userModeInput) != 1) { - printf("%-5d %-40s %5d$\n", availableItems[i].id, availableItems[i].itemName, availableItems[i].price); + printf("Invalid [index]. Please try again: "); + fflush(stdout); } - printf("\n-> to buy items type '[index of item]' \n-> write '0' to quit the shop'\n\n"); - - while (scanf("%d", &userInput) != 1) + if (userModeInput > 1 || userModeInput < 0) { printf("Invalid [index]. Please try again: "); - fflush(stdout); } - - if (userInput > 0) + else if (userModeInput == 0) + { + mode = 0; + selectMode = 0; + } + else if (userModeInput == 1) { - return userInput; - // BUY ITEM added later -> addItemToInventory(userInput); + mode = 1; + selectMode = 0; } - else if (userInput == 0) + } + + int userInput; + fflush(stdout); + bool shopIsOpen = 1; + + if (mode == 0) + { + + while (shopIsOpen == 1) { - shopIsOpen = 0; - printf("Enjoy your items, have a great day!\n"); - return 0; + printf("*** SHOP-Items *** \n\n"); + + // 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("\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); + } + + if (userInput > 0) + { + x[0] = mode; + x[1] = userInput; + return x; + // BUY ITEM added later -> addItemToInventory(userInput); + } + else if (userInput == 0) + { + shopIsOpen = 0; + printf("Enjoy your items, have a great day!\n"); + x[0] = -1; + x[1] = -1; + return x; + } + else + { + printf("Invalid [index]. Please try again: "); + } } - else + } + else if (mode == 1) + { + while (shopIsOpen == 1) { - printf("Invalid [index]. Please try again: "); + if (actualPlayer.itemCounter > 0) + { + printf("*** Your inventory *** \n\n"); + + // printf("%-5s %-30s %5s\n", "Index", "Name", "Price"); + for (int i = 0; i < actualPlayer.itemCounter; i++) + { + printf("%-5d %-40s %5d$\n", i+1, actualPlayer.itemInventory[i].itemName, actualPlayer.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) + { + printf("Invalid [index]. Please try again: "); + fflush(stdout); + } + + 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) + { + shopIsOpen = 0; + 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 + { + shopIsOpen = 0; + printf("*** Your inventory is empty *** \n\n"); + x[0] = -1; + x[1] = -1; + return x; + } } } - return 0; + x[0] = -1; + return x; } \ No newline at end of file diff --git a/src/c/shop.h b/src/c/shop.h index 24a390b..bb41f3b 100644 --- a/src/c/shop.h +++ b/src/c/shop.h @@ -2,7 +2,8 @@ #define SHOP_H #include "items.h" +#include "player.h" -int openShop(Item *availableItems); +int *openShop(Item *availableItems, Player actualPlayer); #endif \ No newline at end of file