KRUGSON
2 years ago
3 changed files with 133 additions and 0 deletions
-
60src/c/map.c
-
29src/c/map.h
-
44test/c/test_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; |
|||
}; |
@ -0,0 +1,29 @@ |
|||
#ifndef MAP_H |
|||
#define MAP_H |
|||
|
|||
//bibs |
|||
#include <stdio.h> |
|||
#include <stdlib.h> |
|||
#include <stdbool.h> |
|||
#include <string.h> |
|||
|
|||
#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 |
@ -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 |
Write
Preview
Loading…
Cancel
Save
Reference in new issue