Browse Source

refactoring: combine util functions in one file

remotes/origin/nav
KRUGSON 2 years ago
parent
commit
d9fcbbe579
  1. 3
      src/c/helper.h
  2. 2
      src/c/items.c
  3. 12
      src/c/main.c
  4. 2
      src/c/map.h
  5. 43
      src/c/nav_helper.c
  6. 13
      src/c/nav_helper.h
  7. 38
      src/c/utils.c
  8. 8
      src/c/utils.h
  9. 3
      test/c/test_commands.c
  10. 3
      test/c/test_items.c
  11. 3
      test/c/test_map.c
  12. 3
      test/c/test_player.c
  13. 5
      test/c/test_utils.c

3
src/c/helper.h

@ -2,6 +2,9 @@
#define HELPER_H #define HELPER_H
//erlaubt es z.b. rand() zu mocken //erlaubt es z.b. rand() zu mocken
#include <stdio.h>
#include <stdlib.h>
int randomInt(); int randomInt();

2
src/c/items.c

@ -3,7 +3,7 @@
#include <string.h> #include <string.h>
#include <stdbool.h> #include <stdbool.h>
#include "nav_helper.h"
#include "utils.h"
#include "items.h" #include "items.h"
Item *getItems(char *itemsMapFile) Item *getItems(char *itemsMapFile)

12
src/c/main.c

@ -5,7 +5,7 @@
#include <string.h> #include <string.h>
#include "map.h" #include "map.h"
#include "nav_helper.h"
#include "utils.h"
#include "items.h" #include "items.h"
#include "shop.h" #include "shop.h"
#include "player.h" #include "player.h"
@ -16,7 +16,7 @@ bool gameRunning;
bool acceptedRules; bool acceptedRules;
// declare needed variables // declare needed variables
Room *map; //game Map
Room *gameMap; //game Map
Item *availableItems; //shop items Item *availableItems; //shop items
Player actualPlayer; //player Player actualPlayer; //player
int inputCounter = 0; //need to check on some positions int inputCounter = 0; //need to check on some positions
@ -86,7 +86,7 @@ void setUp()
gameRunning = 1; gameRunning = 1;
// get Content // get Content
map = getMap(gameMapFile);
gameMap = getMap(gameMapFile);
availableItems = getItems(itemsMapFile); availableItems = getItems(itemsMapFile);
} }
@ -129,7 +129,7 @@ void processInput(char userInput[20])
{ {
lastPlayerPosition = playerPosition; //playerPosition doesn't change but lastPlayerPosition needs update lastPlayerPosition = playerPosition; //playerPosition doesn't change but lastPlayerPosition needs update
Room actualRoom = map[playerPosition];
Room actualRoom = gameMap[playerPosition];
if (actualRoom.shopAvailable == 1) if (actualRoom.shopAvailable == 1)
{ {
int *result = malloc(sizeof(int) * 2); int *result = malloc(sizeof(int) * 2);
@ -172,7 +172,7 @@ int checkExit(char userInput[20])
// check is user moved // check is user moved
int checkMove(char userInput[20]) int checkMove(char userInput[20])
{ {
Room r = map[playerPosition];
Room r = gameMap[playerPosition];
inputCounter += 1; inputCounter += 1;
if (strcmp(userInput, "north") == 0) if (strcmp(userInput, "north") == 0)
{ {
@ -232,6 +232,6 @@ void printStatus()
if (lastPlayerPosition != playerPosition || lastPlayerPosition == playerPosition || playerPosition == 0 && inputCounter == 0) if (lastPlayerPosition != playerPosition || lastPlayerPosition == playerPosition || playerPosition == 0 && inputCounter == 0)
{ {
printRoomStatus(moveMessage, map[playerPosition], playerPosition);
printRoomStatus(moveMessage, gameMap[playerPosition], playerPosition);
} }
} }

2
src/c/map.h

@ -7,7 +7,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#include "nav_helper.h"
#include "utils.h"
//defs //defs
#define mapMax 4 // for map (adjust to txt count of rooms -> game.map) #define mapMax 4 // for map (adjust to txt count of rooms -> game.map)

43
src/c/nav_helper.c

@ -1,43 +0,0 @@
//bibs
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
//headers
#include "nav_helper.h"
bool startsWith(const char *a, const char *b)
{
if (strncmp(a, b, strlen(b)) == 0)
return 1;
return 0;
};
FILE *getStream(char *filePath)
{
FILE *stream;
stream = fopen(filePath, "r");
if (stream == NULL)
{
printf("ERROR: couldn't open or find file: %s !\n", filePath);
exit(EXIT_FAILURE); // exit
}
return stream;
}
void printStreamLines(FILE *stream){
char *line = NULL;
size_t len = 0;
ssize_t read;
/* print line by line from file */
while ((read = getline(&line, &len, stream)) != -1)
{
// printf("Retrieved line of length %u :\n", read);
printf("%s", line);
}
free(line); /* Deallocate allocated memory */
}

13
src/c/nav_helper.h

@ -1,13 +0,0 @@
#ifndef NAV_HELPER_H
#define NAV_HELPER_H
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
bool startsWith(const char *a, const char *b);
FILE *getStream(char *filePath);
void printStreamLines(FILE *stream);
#endif

38
src/c/utils.c

@ -1,3 +1,4 @@
#include "utils.h" #include "utils.h"
#include "helper.h" #include "helper.h"
@ -7,9 +8,44 @@ int randomIntRange(int min, int max)
return (value % (max - min + 1)) + min; return (value % (max - min + 1)) + min;
} }
int map(int x, int in_min, int in_max, int out_min, int out_max) int map(int x, int in_min, int in_max, int out_min, int out_max)
{ {
//vgl Arduino map() https://www.arduino.cc/reference/en/language/functions/math/map/ //vgl Arduino map() https://www.arduino.cc/reference/en/language/functions/math/map/
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
bool startsWith(const char *a, const char *b)
{
if (strncmp(a, b, strlen(b)) == 0)
return 1;
return 0;
};
FILE *getStream(char *filePath)
{
FILE *stream;
stream = fopen(filePath, "r");
if (stream == NULL)
{
printf("ERROR: couldn't open or find file: %s !\n", filePath);
exit(EXIT_FAILURE); // exit
}
return stream;
}
void printStreamLines(FILE *stream){
char *line = NULL;
size_t len = 0;
ssize_t read;
/* print line by line from file */
while ((read = getline(&line, &len, stream)) != -1)
{
// printf("Retrieved line of length %u :\n", read);
printf("%s", line);
}
free(line); /* Deallocate allocated memory */
} }

8
src/c/utils.h

@ -1,7 +1,15 @@
#ifndef UTILS_H #ifndef UTILS_H
#define UTILS_H #define UTILS_H
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
int randomIntRange(int min, int max); int randomIntRange(int min, int max);
int map(int x, int in_min, int in_max, int out_min, int out_max); int map(int x, int in_min, int in_max, int out_min, int out_max);
bool startsWith(const char *a, const char *b);
FILE *getStream(char *filePath);
void printStreamLines(FILE *stream);
#endif #endif

3
test/c/test_commands.c

@ -3,7 +3,8 @@
#include "unity.h" #include "unity.h"
#include "commands.h" #include "commands.h"
#include "map.h" #include "map.h"
#include "nav_helper.h"
#include "utils.h"
#include "helper.h"
void setUp(void) void setUp(void)
{ {

3
test/c/test_items.c

@ -1,8 +1,9 @@
#ifdef TEST #ifdef TEST
#include "unity.h" #include "unity.h"
#include "nav_helper.h"
#include "items.h" #include "items.h"
#include "utils.h"
#include "helper.h"
void setUp(void) void setUp(void)
{ {

3
test/c/test_map.c

@ -2,7 +2,8 @@
#include "unity.h" #include "unity.h"
#include "map.h" #include "map.h"
#include "nav_helper.h"
#include "utils.h"
#include "helper.h"
void setUp(void) void setUp(void)

3
test/c/test_player.c

@ -3,7 +3,8 @@
#include "unity.h" #include "unity.h"
#include "player.h" #include "player.h"
#include "items.h" #include "items.h"
#include "nav_helper.h"
#include "utils.h"
#include "helper.h"
Item *availableItems; Item *availableItems;
Player actualPlayer; Player actualPlayer;

5
test/c/test_nav_helper.c → test/c/test_utils.c

@ -1,7 +1,8 @@
#ifdef TEST #ifdef TEST
#include "unity.h" #include "unity.h"
#include "nav_helper.h"
#include "utils.h"
#include "helper.h"
void setUp(void) void setUp(void)
{ {
@ -11,7 +12,7 @@ void tearDown(void)
{ {
} }
void test_nav_helper(void)
void test_startsWith(void)
{ {
/* arrange */ /* arrange */
// Hier die Werte eingeben // Hier die Werte eingeben
Loading…
Cancel
Save