Browse Source

Merge branch 'nav' into 'main'

Nav

See merge request fdai7361/projekt!17
remotes/origin/fdai7372-main-patch-93194
fdai7361 2 years ago
parent
commit
96c182eb26
  1. 6
      src/c/commands.c
  2. 3
      src/c/helper.h
  3. 2
      src/c/items.c
  4. 23
      src/c/main.c
  5. 2
      src/c/map.h
  6. 43
      src/c/nav_helper.c
  7. 13
      src/c/nav_helper.h
  8. 61
      src/c/print_helper.c
  9. 4
      src/c/print_helper.h
  10. 223
      src/c/shop.c
  11. 38
      src/c/utils.c
  12. 8
      src/c/utils.h
  13. 1
      src/content/game_instructions.txt
  14. 5
      test/c/test_commands.c
  15. 3
      test/c/test_items.c
  16. 3
      test/c/test_map.c
  17. 3
      test/c/test_player.c
  18. 5
      test/c/test_utils.c

6
src/c/commands.c

@ -13,17 +13,17 @@ char* getPossibleCommands(Room r, int playerPosition)
strcpy(msg, ""); //assign char (string) that no leading zero result strcpy(msg, ""); //assign char (string) that no leading zero result
if (playerPosition > 0) if (playerPosition > 0)
{ {
strcat(msg, "You can move south from here\n");
strcat(msg, "> You can move south from here\n");
} }
if (playerPosition < mapMax - 1) if (playerPosition < mapMax - 1)
{ {
strcat(msg, "You can move north from here\n");
strcat(msg, "> You can move north from here\n");
} }
if (r.shopAvailable == 1) if (r.shopAvailable == 1)
{ {
strcat(msg, "You can call 'shop' to buy items.\n");
strcat(msg, "> You can call 'shop' to buy items.\n");
} }
return msg; return msg;

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)

23
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)
{ {
@ -211,7 +211,6 @@ int checkMove(char userInput[20])
// print actual location and messages from game.map // print actual location and messages from game.map
void printStatus() void printStatus()
{ {
Room actualRoom = map[playerPosition];
char moveMessage[30]; char moveMessage[30];
if (lastPlayerPosition > playerPosition) if (lastPlayerPosition > playerPosition)
@ -233,16 +232,6 @@ void printStatus()
if (lastPlayerPosition != playerPosition || lastPlayerPosition == playerPosition || playerPosition == 0 && inputCounter == 0) if (lastPlayerPosition != playerPosition || lastPlayerPosition == playerPosition || playerPosition == 0 && inputCounter == 0)
{ {
printf("\n\n################################################################################\n");
printf("--> %s <--\n", moveMessage);
printf("%s\n", actualRoom.nameRoom);
printf("%s\n", actualRoom.msgRoom);
printf("\n");
char *possibleCommands = malloc(sizeof(char) * (500));
possibleCommands = getPossibleCommands(map[playerPosition], playerPosition);
printf("%s", possibleCommands);
free(possibleCommands);
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

61
src/c/print_helper.c

@ -5,6 +5,7 @@
#include <ctype.h> #include <ctype.h>
#include "print_helper.h" #include "print_helper.h"
#include "commands.h"
char *append(const char *s, char c); char *append(const char *s, char c);
@ -12,39 +13,71 @@ void printHeader(char *header)
{ {
printf("\n"); printf("\n");
int len = strlen(header); int len = strlen(header);
//set
// set
int maxSize = len * 3 + 3; // 2 spaces | left - mid - right = 3 -> 3 times length of header int maxSize = len * 3 + 3; // 2 spaces | left - mid - right = 3 -> 3 times length of header
char pHeader[maxSize]; char pHeader[maxSize];
char ch = '*'; char ch = '*';
for (int i = 0; i < maxSize; i++) for (int i = 0; i < maxSize; i++)
{ {
pHeader[i] = ch; pHeader[i] = ch;
} }
printf("%.*s\n", maxSize, pHeader); printf("%.*s\n", maxSize, pHeader);
printf("%-*s %s %-*s \n", len, "", header, len, ""); printf("%-*s %s %-*s \n", len, "", header, len, "");
printf("%.*s\n", maxSize, pHeader); printf("%.*s\n", maxSize, pHeader);
} }
void printItem(char *header, char *item, int itemID, int itemPrice) void printItem(char *header, char *item, int itemID, int itemPrice)
{ {
int len = strlen(header); int len = strlen(header);
int maxSizeForItem = (len * 3 + 3)-10;
int maxSizeForItem = (len * 3 + 3) - 10;
printf("%-*d %-*s %*d$ \n", 2, itemID, maxSizeForItem, item, 5, itemPrice); printf("%-*d %-*s %*d$ \n", 2, itemID, maxSizeForItem, item, 5, itemPrice);
} }
char *append(const char *s, char c)
void printRoomStatus(char *moveMessage, Room currentRoom, int playerPosition)
{ {
int len = strlen(s);
char buf[len + 2];
strcpy(buf, s);
buf[len] = c;
buf[len + 1] = 0;
return strdup(buf);
int len = strlen(moveMessage);
int maxSizeRoomStatus = 60; // 2 spaces | left - mid - right = 3 -> 3 times length of header
char roomStatusPlaceholder[maxSizeRoomStatus];
int show = maxSizeRoomStatus - len;
int fullLine = show * 2 + len + 2;
int paddingRoomName = (fullLine - strlen(currentRoom.nameRoom)) / 2;
int paddingRoomStory = (fullLine - strlen(currentRoom.msgRoom)) / 6;
int paddingRoomStoryPlace = fullLine - (paddingRoomStory * 2) - 2;
char seperatorLine[fullLine];
char ch = '#';
for (int i = 0; i < fullLine; i++)
{
roomStatusPlaceholder[i] = ch;
}
char sCh = '-';
for (int i = 0; i < fullLine; i++)
{
seperatorLine[i] = sCh;
}
printf("\n");
printf("%.*s\n", fullLine, seperatorLine);
printf("%.*s %s %.*s \n", show, roomStatusPlaceholder, moveMessage, show, roomStatusPlaceholder);
printf("%.*s\n", fullLine, seperatorLine);
printf("\n");
printf("%-*s%s %-*s \n\n", paddingRoomName, "", currentRoom.nameRoom, paddingRoomName, "");
printf("%-*s %-*s %-*s \n\n", paddingRoomStory, "", paddingRoomStoryPlace, currentRoom.msgRoom, paddingRoomStory, "");
printf("%.*s\n", fullLine, seperatorLine);
printf("%.*s\n", fullLine, roomStatusPlaceholder);
printf("%.*s\n\n", fullLine, seperatorLine);
char *possibleCommands = malloc(sizeof(char) * (500));
possibleCommands = getPossibleCommands(currentRoom, playerPosition);
printf("%s\n", possibleCommands);
free(possibleCommands);
printf("please enter what you want to do next:\n");
} }

4
src/c/print_helper.h

@ -1,7 +1,11 @@
#ifndef PRINT_HELPER_H #ifndef PRINT_HELPER_H
#define PRINT_HELPER_H #define PRINT_HELPER_H
#include "map.h"
void printHeader(char *header); void printHeader(char *header);
void printItem(char *header, char *item, int itemID, int itemPrice); void printItem(char *header, char *item, int itemID, int itemPrice);
void printRoomStatus(char *moveMessage, Room currentRoom, int playerPosition);
#endif #endif

223
src/c/shop.c

@ -9,141 +9,192 @@
int *openShop(Item *availableItems, Player player) int *openShop(Item *availableItems, Player player)
{ {
int mode = 0;
int mode = -1;
int *x = malloc(sizeof(int) * 2); int *x = malloc(sizeof(int) * 2);
int userModeInput;
fflush(stdout);
bool selectMode = 1;
while (selectMode == 1)
char *userModeInput = malloc(sizeof(char) * 1);
do
{ {
printf("Please type '0' for buy or '1' to sell items!\n");
while (scanf(" %d", &userModeInput) != 1)
{
printf("Invalid [index]. Please try again: ");
fflush(stdout);
}
printf("Please type '0' for buy or '1' to sell items:\n");
scanf(" %s", userModeInput);
if (userModeInput > 1 || userModeInput < 0)
if (strlen(userModeInput) == 1)
{ {
printf("Invalid [index]. Please try again: ");
}
else if (userModeInput == 0)
{
mode = 0;
selectMode = 0;
if (isdigit(userModeInput[0]))
{
int check = userModeInput[0] - '0';
if (check > 1 || check < 0)
{
printf("\nInvalid Index.\n");
}
else if (check == 0)
{
mode = 0;
}
else if (check == 1)
{
mode = 1;
}
}
else
{
printf("\nInvalid input. Input isn't a number.\n");
}
} }
else if (userModeInput == 1)
else
{ {
mode = 1;
selectMode = 0;
printf("\nOnly ONE char is needed!\n");
} }
}
} while (mode != 0 && mode != 1);
int userInput;
fflush(stdout); fflush(stdout);
bool shopIsOpen = 1;
bool shopIsOpen = true;
if (mode == 0) if (mode == 0)
{ {
while (shopIsOpen == 1)
char *userInput = malloc(sizeof(char) * 1);
do
{ {
// header
char *header = "SHOP-Items"; char *header = "SHOP-Items";
printHeader(header); printHeader(header);
// printf("%-5s %-30s %5s\n", "Index", "Name", "Price");
// items
int maxItemsShop = 0;
for (int i = 0; i < 6; i++) for (int i = 0; i < 6; i++)
{ {
//printf("%-5d %-40s %5d$\n", availableItems[i].id, availableItems[i].itemName, availableItems[i].price);
// printf("%-5d %-40s %5d$\n", availableItems[i].id, availableItems[i].itemName, availableItems[i].price);
printItem(header, availableItems[i].itemName, availableItems[i].id, availableItems[i].price); printItem(header, availableItems[i].itemName, availableItems[i].id, availableItems[i].price);
maxItemsShop += 1;
} }
// instruction
printf("\n-> to buy items type '[index of item]' \n-> write '0' to quit the shop'\n\n"); printf("\n-> to buy items type '[index of item]' \n-> write '0' to quit the shop'\n\n");
while (scanf(" %d", &userInput) != 1)
{
printf("Invalid [index]. Please try again: ");
fflush(stdout);
}
// input
scanf(" %s", userInput);
if (userInput > 0)
{
x[0] = mode;
x[1] = userInput;
return x;
// BUY ITEM added later -> addItemToInventory(userInput);
}
else if (userInput == 0)
bool finished = false;
// check input
if (strlen(userInput) == 1)
{ {
shopIsOpen = 0;
printf("Enjoy your items, have a great day!\n");
x[0] = -1;
x[1] = -1;
return x;
if (isdigit(userInput[0]))
{
int check = userInput[0] - '0';
if (check > maxItemsShop) // about max items index -> invalid
{
printf("\nInvalid [index]. Please try again: \n");
}
else if (check > 0) // all available Indizes
{
shopIsOpen = false;
x[0] = mode;
x[1] = check;
return x;
}
else if (check == 0)
{
shopIsOpen = false;
printf("Enjoy your items, have a great day!\n");
x[0] = -1;
x[1] = -1;
return x;
}
else
{
printf("Invalid [index]. Please try again. \n");
}
}
else
{
printf("\nInvalid input. Input isn't a number.\n");
}
} }
else else
{ {
printf("Invalid [index]. Please try again: ");
printf("\nToo many chars entered. Only ONE char is needed. Try again!\n");
} }
}
}
} while (shopIsOpen == true);
} //end mode = 0
else if (mode == 1) else if (mode == 1)
{ {
while (shopIsOpen == 1)
char *userInput = malloc(sizeof(char) * 1);
if (player.itemCounter > 0)//check if inventory of player is empty
{ {
if (player.itemCounter > 0)
do
{ {
// header
char *header = "Your inventory"; char *header = "Your inventory";
printHeader(header); printHeader(header);
// printf("%-5s %-30s %5s\n", "Index", "Name", "Price");
// print inventory
int maxItemsInv = 0;
for (int i = 0; i < player.itemCounter; i++) for (int i = 0; i < player.itemCounter; i++)
{ {
printItem(header, player.itemInventory[i].itemName, i+1, player.itemInventory[i].price);
printItem(header, player.itemInventory[i].itemName, i + 1, player.itemInventory[i].price);
maxItemsInv += 1;
} }
// instructions
printf("\n-> to sell items type '[index of item]' \n-> write '0' to quit the shop'\n\n"); printf("\n-> to sell items type '[index of item]' \n-> write '0' to quit the shop'\n\n");
while (scanf(" %d", &userInput) != 1)
{
printf("Invalid [index]. Please try again: ");
fflush(stdout);
}
// input
scanf(" %s", userInput);
if (userInput > 0)
{
x[0] = mode;
x[1] = userInput-1; //index begin on 1 because we will cancel with 0
return x;
// BUY ITEM added later -> addItemToInventory(userInput);
}
else if (userInput == 0)
bool finished = false;
// check input
if (strlen(userInput) == 1)
{ {
shopIsOpen = 0;
printf("Enjoy your money, have a great day!\n");
x[0] = -1;
x[1] = -1;
return x;
if (isdigit(userInput[0]))
{
int check = userInput[0] - '0';
if (check > maxItemsInv) // about max items index -> invalid
{
printf("\nInvalid [index]. Please try again: \n");
}
else if (check > 0)
{
shopIsOpen = false;
x[0] = mode;
x[1] = check - 1; // index begin on 1 because we will cancel with 0
return x;
}
else if (check == 0)
{
shopIsOpen = false;
printf("Enjoy your money, have a great day!\n");
x[0] = -1;
x[1] = -1;
return x;
}
else
{
printf("Invalid [index]. Please try again: ");
}
}
else
{
printf("\nInvalid input. Input isn't a number.\n");
}
} }
else else
{ {
printf("Invalid [index]. Please try again: ");
printf("\nToo many chars entered. Only ONE char is needed. Try again!\n");
} }
}
else
{
shopIsOpen = 0;
printf("*** Your inventory is empty *** \n\n");
x[0] = -1;
x[1] = -1;
return x;
}
} while (shopIsOpen == true);
} }
}
else //when inventory of player is empty
{
printf("*** Your inventory is empty *** \n\n");
x[0] = -1;
x[1] = -1;
return x;
}
}//end mode = 1
x[0] = -1;
return x;
} }

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

1
src/content/game_instructions.txt

@ -27,6 +27,7 @@ enter "south" to go south
alltime commands: alltime commands:
- 'inventory' -> will show your items that you have bought or found () - 'inventory' -> will show your items that you have bought or found ()
- 'commands' -> to show available comamnds list from this spot
special commands (not available at any time during the game): special commands (not available at any time during the game):
- 'shop' -> opens the shop where you can buy or sell items - 'shop' -> opens the shop where you can buy or sell items

5
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)
{ {
@ -20,7 +21,7 @@ void test_commands(void)
Room* mapData; Room* mapData;
mapData = getMap("./src/content/game.map"); mapData = getMap("./src/content/game.map");
int playerPosition = 3; //last player position in north - so you can only move to south int playerPosition = 3; //last player position in north - so you can only move to south
char* expectedString = "You can move south from here\n";
char* expectedString = "> You can move south from here\n";
/* act */ /* act */
// Die Funktion wird ausgeführt // Die Funktion wird ausgeführt

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