Browse Source

added removeItemFromInventory with test

remotes/origin/navigation
KRUGSON 2 years ago
parent
commit
1d863f9410
  1. 39
      src/c/player.c
  2. 1
      src/c/player.h
  3. 24
      test/c/test_player.c

39
src/c/player.c

@ -7,6 +7,8 @@
// PLAYER INVENTORY // PLAYER INVENTORY
//add and remove
Player addItemToInventory(Item *availableItems, int itemIndex, Player actualPlayer) Player addItemToInventory(Item *availableItems, int itemIndex, Player actualPlayer)
{ {
int counter = actualPlayer.itemCounter; int counter = actualPlayer.itemCounter;
@ -16,6 +18,40 @@ Player addItemToInventory(Item *availableItems, int itemIndex, Player actualPlay
return actualPlayer; return actualPlayer;
} }
Player removeItemFromInventory(int itemIndex, Player actualPlayer)
{
Item items[maxItems]; // actualPlayer.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);
index = i;
break;
}
}
if (index != -1)
{
// 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];
/*printf("New Array : ");
for(i = 0; i < maxItems - 1; i++)
printf("%d ",actualPlayer.itemInventory[i].id);*/
}
else
printf("Element Not Found\n");
actualPlayer.itemCounter = actualPlayer.itemCounter - 1;
return actualPlayer;
}
//show
void showInventory(Player actualPlayer) void showInventory(Player actualPlayer)
{ {
int inventoryItemCounter = actualPlayer.itemCounter; int inventoryItemCounter = actualPlayer.itemCounter;
@ -36,4 +72,5 @@ void showInventory(Player actualPlayer)
} }
printf("\n\n"); printf("\n\n");
}
}

1
src/c/player.h

@ -12,6 +12,7 @@ typedef struct
} Player; } Player;
Player addItemToInventory(Item *availableItem, int itemIndex, Player actualPlayer); Player addItemToInventory(Item *availableItem, int itemIndex, Player actualPlayer);
Player removeItemFromInventory(int index, Player actualPlayer);
void showInventory(Player actualPlayer); void showInventory(Player actualPlayer);

24
test/c/test_player.c

@ -5,8 +5,11 @@
#include "items.h" #include "items.h"
#include "nav_helper.h" #include "nav_helper.h"
Item *availableItems;
Player actualPlayer;
void setUp(void) void setUp(void)
{ {
availableItems = getItems("./src/content/items.map");
} }
void tearDown(void) void tearDown(void)
@ -18,8 +21,6 @@ void test_addItemToInventory(void)
/* arrange */ /* arrange */
// Hier die Werte eingeben/deklarieren // Hier die Werte eingeben/deklarieren
int shopItemIndex = 1; int shopItemIndex = 1;
Player actualPlayer;
Item *availableItems = getItems("./src/content/items.map");
/* act */ /* act */
// Die Funktion wird ausgeführt // Die Funktion wird ausgeführt
@ -30,4 +31,23 @@ void test_addItemToInventory(void)
TEST_ASSERT_EQUAL_INT(shopItemIndex, actualPlayer.itemInventory[0].id); TEST_ASSERT_EQUAL_INT(shopItemIndex, actualPlayer.itemInventory[0].id);
} }
void test_removeItemFromInventory(void)
{
/* arrange */
// Hier die Werte eingeben/deklarieren
int itemToRemove = 0; // index to remove (we already bought it in function above)
/* act */
// Die Funktion wird ausgeführt
Item itemBeforeRemove = actualPlayer.itemInventory[0];
actualPlayer = removeItemFromInventory(itemToRemove, actualPlayer); // then remove
Item itemAfterRemove = actualPlayer.itemInventory[0];
// OUTPUT
/* assert */
// Vergleichen mit Inhalt von game.Map File
TEST_ASSERT_NOT_EQUAL_UINT8(itemBeforeRemove.id, itemAfterRemove.id);
}
#endif // TEST #endif // TEST
Loading…
Cancel
Save