Browse Source

added items with test and items.map file

remotes/origin/navigation
KRUGSON 2 years ago
parent
commit
12d6f1143a
  1. 57
      src/c/items.c
  2. 20
      src/c/items.h
  3. 8
      src/content/items.map
  4. 38
      test/c/test_items.c

57
src/c/items.c

@ -0,0 +1,57 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "nav_helper.h"
#include "items.h"
Item *getItems(char *itemsMapFile)
{
static Item getItems[maxItems];
FILE *stream;
char *line = NULL;
size_t len = 0;
ssize_t read;
stream = fopen(itemsMapFile, "r");
if (stream == NULL)
{
printf("ERROR: couldn't open or find file: ITEMS!\n");
exit(EXIT_FAILURE); // exit
}
char delimiter[] = ";";
/* print line by line from file */
int lineCounter = 0;
while ((read = getline(&line, &len, stream)) != -1)
{
// printf("Retrieved line of length %u :\n", read);
if (startsWith(line, "#") == 0)
{
char *arr[itemAttributesMax];
char *token = strtok(line, delimiter);
int countToken = 0;
while (token != NULL)
{
arr[countToken] = token;
token = strtok(NULL, ";");
countToken += 1;
}
free(token);
Item i;
i.id = atoi(arr[0]);
strcpy(i.itemName, arr[1]);
//printf(arr[2]);
i.price = atoi(arr[3]);
getItems[lineCounter] = i;
lineCounter += 1;
}
}
return getItems;
}

20
src/c/items.h

@ -0,0 +1,20 @@
#ifndef ITEMS_H
#define ITEMS_H
#define itemAttributesMax 4 // for items
#define maxItems 100 // for inventory/item list
#include <stdbool.h>
typedef struct
{
int id;
char itemName[50];
bool inShopAvailable;
int price;
} Item;
Item *getItems(char *itemsMapFile);
#endif

8
src/content/items.map

@ -0,0 +1,8 @@
# LINES WITH '#' AT BEGINNING GET IGNORED
#ID;NAME;inShopAvailable;price
1;healing potion;1;20
2;shield;1;50
3;UV flashlight;1;80
4;key for police station;0;9999
5;acid;1;9999
6;gun;0;9999

38
test/c/test_items.c

@ -0,0 +1,38 @@
#ifdef TEST
#include "unity.h"
#include "nav_helper.h"
#include "items.h"
void setUp(void)
{
}
void tearDown(void)
{
}
void test_items(void)
{
/* arrange */
// Hier die Werte eingeben/deklarieren
Item* items;
int expectedItemID = 1;
int expectedSecondItemID = 2;
/* act */
// Die Funktion wird ausgeführt
items = getItems("./src/content/items.map");
/* make visible OUTPUT */
for(int i=0; i<4;i++){
printf("%s\n", items[i].itemName);
}
/* assert */
// Vergleichen mit Inhalt von game.Map File
TEST_ASSERT_EQUAL_INT(expectedItemID, items[0].id);
TEST_ASSERT_EQUAL_INT(expectedSecondItemID, items[1].id);
}
#endif // TEST
Loading…
Cancel
Save