From df2ca48b39795dac3ef3a60f24d0b47e4ee15c55 Mon Sep 17 00:00:00 2001 From: KRUGSON Date: Tue, 7 Feb 2023 16:07:57 +0100 Subject: [PATCH] refactoring: changed var names for plausibility --- src/c/main.c | 9 +++--- src/c/player.c | 82 +++++++++++++++++++++++++------------------------- src/c/player.h | 16 +++++----- src/c/shop.c | 8 ++--- src/c/shop.h | 2 +- 5 files changed, 59 insertions(+), 58 deletions(-) diff --git a/src/c/main.c b/src/c/main.c index 71b73ee..b85804e 100644 --- a/src/c/main.c +++ b/src/c/main.c @@ -11,14 +11,15 @@ #include "player.h" #include "commands.h" +//variables important to play the game bool gameRunning; bool acceptedRules; // declare needed variables -Room *map; -Item *availableItems; -Player actualPlayer; -int inputCounter = 0; +Room *map; //game Map +Item *availableItems; //shop items +Player actualPlayer; //player +int inputCounter = 0; //need to check on some positions // content char *gameInstructionsFile = "../../src/content/game_instructions.txt"; diff --git a/src/c/player.c b/src/c/player.c index 0e3fedd..51d6363 100644 --- a/src/c/player.c +++ b/src/c/player.c @@ -8,25 +8,25 @@ // PLAYER INVENTORY // add and remove -Player addItemToInventory(Item *availableItems, int itemIndex, Player actualPlayer) +Player addItemToInventory(Item *items, int itemIndex, Player player) { - int counter = actualPlayer.itemCounter; - actualPlayer.itemInventory[counter] = availableItems[itemIndex - 1]; // -1 to get right index (items begin - 1,2,3,4...) - actualPlayer.itemCounter += 1; + int itemCounter = player.itemCounter; + player.itemInventory[itemCounter] = items[itemIndex - 1]; // -1 to get right index (items begin - 1,2,3,4...) + player.itemCounter += 1; - return actualPlayer; + return player; } -Player removeItemFromInventory(int itemIndex, Player actualPlayer) +Player removeItemFromInventory(int itemIndex, Player player) { - Item items[maxItems]; // actualPlayer.itemInventory + Item items[maxItems]; // player.itemInventory int i, index = -1; for (i = 0; i < maxItems; i++) { if (i == itemIndex) { - // printf("%d - '%s' has been removed from inventory.\n", actualPlayer.itemInventory[i].id, actualPlayer.itemInventory[i].itemName); + // printf("%d - '%s' has been removed from inventory.\n", player.itemInventory[i].id, player.itemInventory[i].itemName); index = i; break; } @@ -36,73 +36,73 @@ Player removeItemFromInventory(int itemIndex, Player actualPlayer) { // shift all the element from index+1 by one position to the left for (i = index; i < maxItems - 1; i++) - actualPlayer.itemInventory[i] = actualPlayer.itemInventory[i + 1]; + player.itemInventory[i] = player.itemInventory[i + 1]; /*printf("New Array : "); for(i = 0; i < maxItems - 1; i++) - printf("%d ",actualPlayer.itemInventory[i].id);*/ + printf("%d ",player.itemInventory[i].id);*/ } else printf("Element Not Found\n"); - actualPlayer.itemCounter = actualPlayer.itemCounter - 1; + player.itemCounter = player.itemCounter - 1; - return actualPlayer; + return player; } // currency -Player setTotal(Player actualPlayer, int value) +Player setTotal(Player player, int value) { - actualPlayer.wallet = value; - return actualPlayer; + player.wallet = value; + return player; } -Player addMoneyToPlayer(Player actualPlayer, int money) +Player addMoneyToPlayer(Player player, int moneyValue) { - int newTotal = money + actualPlayer.wallet; - actualPlayer = setTotal(actualPlayer, newTotal); - return actualPlayer; + int newTotal = moneyValue + player.wallet; + player = setTotal(player, newTotal); + return player; } -Player removeMoneyFromPlayer(Player actualPlayer, int money) +Player removeMoneyFromPlayer(Player player, int moneyValue) { - int newTotal = actualPlayer.wallet - money; - actualPlayer = setTotal(actualPlayer, newTotal); - return actualPlayer; + int newTotal = player.wallet - moneyValue; + player = setTotal(player, newTotal); + return player; } //add and remove items with currency -Player buyItem(Item *availableItems, int itemIndex, Player actualPlayer) +Player buyItem(Item *items, int itemIndex, Player player) { - int itemPrice = availableItems[itemIndex - 1].price; - if (actualPlayer.wallet >= itemPrice) + int itemPrice = items[itemIndex - 1].price; + if (player.wallet >= itemPrice) { - actualPlayer = addItemToInventory(availableItems, itemIndex, actualPlayer); - actualPlayer = removeMoneyFromPlayer(actualPlayer, itemPrice); - printf("You bought item[%d] for %d$! \nYour balance is now: %d$\n", itemIndex, itemPrice, actualPlayer.wallet); - return actualPlayer; + player = addItemToInventory(items, itemIndex, player); + player = removeMoneyFromPlayer(player, itemPrice); + printf("You bought item[%d] for %d$! \nYour balance is now: %d$\n", itemIndex, itemPrice, player.wallet); + return player; } else { printf("You don't have enough money.\n\n"); - return actualPlayer; + return player; } } -Player sellItem(int itemIndex, Player actualPlayer) +Player sellItem(int itemIndex, Player player) { - 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); + int priceItemSell = player.itemInventory[itemIndex].price; + player = addMoneyToPlayer(player, player.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, player.wallet); - actualPlayer = removeItemFromInventory(itemIndex, actualPlayer); - return actualPlayer; + player = removeItemFromInventory(itemIndex, player); + return player; } // show -void showInventory(Player actualPlayer) +void showInventory(Player player) { - int inventoryItemCounter = actualPlayer.itemCounter; + int inventoryItemCounter = player.itemCounter; if (inventoryItemCounter == 0) { @@ -112,10 +112,10 @@ void showInventory(Player actualPlayer) { printf("*** Inventory *** \n\n"); - for (int i = 0; i < actualPlayer.itemCounter; i++) + for (int i = 0; i < player.itemCounter; i++) { // printf("%d: %d - %s\n", i, inventory[i].id, inventory[i].itemName); - printf("> %s - %d\n", actualPlayer.itemInventory[i].itemName, i); + printf("> %s - %d\n", player.itemInventory[i].itemName, i); } } diff --git a/src/c/player.h b/src/c/player.h index 4518f70..ae603f2 100644 --- a/src/c/player.h +++ b/src/c/player.h @@ -12,19 +12,19 @@ typedef struct } Player; //add and remove items -Player addItemToInventory(Item *availableItem, int itemIndex, Player actualPlayer); -Player removeItemFromInventory(int index, Player actualPlayer); +Player addItemToInventory(Item *availableItem, int itemIndex, Player player); +Player removeItemFromInventory(int index, Player player); //currency -Player setTotal(Player actualPlayer, int value); -Player addMoneyToPlayer(Player actualPlayer, int money); -Player removeMoneyFromPlayer(Player actualPlayer, int money); +Player setTotal(Player player, int value); +Player addMoneyToPlayer(Player player, int money); +Player removeMoneyFromPlayer(Player player, int money); //add and remove items with currency -Player buyItem(Item *availableItems, int itemIndex, Player actualPlayer); -Player sellItem(int itemIndex, Player actualPlayer); +Player buyItem(Item *availableItems, int itemIndex, Player player); +Player sellItem(int itemIndex, Player player); //show -void showInventory(Player actualPlayer); +void showInventory(Player player); #endif \ No newline at end of file diff --git a/src/c/shop.c b/src/c/shop.c index 88e0659..b8f7d53 100644 --- a/src/c/shop.c +++ b/src/c/shop.c @@ -7,7 +7,7 @@ #include "shop.h" #include "player.h" -int *openShop(Item *availableItems, Player actualPlayer) +int *openShop(Item *availableItems, Player player) { int mode = 0; int *x = malloc(sizeof(int) * 2); @@ -90,14 +90,14 @@ int *openShop(Item *availableItems, Player actualPlayer) { while (shopIsOpen == 1) { - if (actualPlayer.itemCounter > 0) + if (player.itemCounter > 0) { printf("*** Your inventory *** \n\n"); // printf("%-5s %-30s %5s\n", "Index", "Name", "Price"); - for (int i = 0; i < actualPlayer.itemCounter; i++) + for (int i = 0; i < player.itemCounter; i++) { - printf("%-5d %-40s %5d$\n", i+1, actualPlayer.itemInventory[i].itemName, actualPlayer.itemInventory[i].price); + printf("%-5d %-40s %5d$\n", i+1, player.itemInventory[i].itemName, player.itemInventory[i].price); } printf("\n-> to sell items type '[index of item]' \n-> write '0' to quit the shop'\n\n"); diff --git a/src/c/shop.h b/src/c/shop.h index bb41f3b..56e1603 100644 --- a/src/c/shop.h +++ b/src/c/shop.h @@ -4,6 +4,6 @@ #include "items.h" #include "player.h" -int *openShop(Item *availableItems, Player actualPlayer); +int *openShop(Item *availableItems, Player player); #endif \ No newline at end of file