Browse Source

refactoring: changed var names for plausibility

remotes/origin/fdai7372-main-patch-93194
KRUGSON 2 years ago
parent
commit
df2ca48b39
  1. 9
      src/c/main.c
  2. 82
      src/c/player.c
  3. 16
      src/c/player.h
  4. 8
      src/c/shop.c
  5. 2
      src/c/shop.h

9
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";

82
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);
}
}

16
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

8
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");

2
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
Loading…
Cancel
Save