diff --git a/src/c/map.c b/src/c/map.c new file mode 100644 index 0000000..83c3cdc --- /dev/null +++ b/src/c/map.c @@ -0,0 +1,60 @@ +//headers +#include "map.h" + +Room *getMap(char *gameMapFile) +{ + static Room fillMap[mapMax]; + + FILE *stream; + char *line = NULL; + size_t len = 0; + ssize_t read; + + stream = fopen(gameMapFile, "r"); + if (stream == NULL) + { + printf("ERROR: couldn't open or find file: MAP!\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[roomAttributesMax]; + char *token = strtok(line, delimiter); + int countToken = 0; + while (token != NULL) + { + arr[countToken] = token; + token = strtok(NULL, ";"); + + countToken += 1; + } + free(token); + + Room r; + r.id = atoi(arr[0]); + strcpy(r.nameRoom, arr[1]); + strcpy(r.msgRoom, arr[2]); + r.successor = atoi(arr[3]); + r.predecessor = atoi(arr[4]); + memcpy(r.items, arr[5], sizeof arr[5]); + r.shopAvailable = atoi(arr[6]); + + fillMap[lineCounter] = r; + lineCounter += 1; + } + } + + free(line); /* Deallocate allocated memory */ + fclose(stream); /* closing file */ + + return fillMap; +}; \ No newline at end of file diff --git a/src/c/map.h b/src/c/map.h new file mode 100644 index 0000000..dd31690 --- /dev/null +++ b/src/c/map.h @@ -0,0 +1,29 @@ +#ifndef MAP_H +#define MAP_H + +//bibs +#include +#include +#include +#include + +#include "nav_helper.h" + +//defs +#define mapMax 4 // for map (adjust to txt count of rooms -> game.map) +#define roomAttributesMax 7 // for room struct (adjust to txt count of room-attributes -> game.map) + +typedef struct Room +{ + int id; + char nameRoom[20]; + char msgRoom[150]; + int successor; + int predecessor; + char items[10]; + bool shopAvailable; +} Room; + +Room *getMap(char *gameMapFile); + +#endif // MAP_H \ No newline at end of file diff --git a/test/c/test_map.c b/test/c/test_map.c new file mode 100644 index 0000000..dcda939 --- /dev/null +++ b/test/c/test_map.c @@ -0,0 +1,44 @@ +#ifdef TEST + +#include "unity.h" +#include "map.h" +#include "nav_helper.h" + +void setUp(void) +{ +} + +void tearDown(void) +{ +} + +void test_map(void) +{ + /* arrange */ + // Hier die Werte eingeben + Room* map; + + /* act */ + // Die Funktion wird ausgeführt + map = getMap("./src/content/game.map"); + + for(int i=0; i<4;i++){ + printf("%s\n", map[i].nameRoom); + } + + /* assert */ + // Vergleichen mit Inhalt von game.Map File + int officeExpectedSuccessor = 1; + int officeExpectedID = 0; + + int fdExpectedSuccessor = 3; + int fdExpectedID = 2; + + TEST_ASSERT_EQUAL_INT(officeExpectedSuccessor, map[0].successor); + TEST_ASSERT_EQUAL_INT(officeExpectedID, map[0].id); + + TEST_ASSERT_EQUAL_INT(fdExpectedSuccessor, map[2].successor); + TEST_ASSERT_EQUAL_INT(fdExpectedID, map[2].id); +} + +#endif // TEST