Browse Source

refactoring: improved map output&input handl. shop

remotes/origin/nav
KRUGSON 2 years ago
parent
commit
c6e7ed22bd
  1. 6
      src/c/commands.c
  2. BIN
      src/c/main
  3. 13
      src/c/main.c
  4. 61
      src/c/print_helper.c
  5. 4
      src/c/print_helper.h
  6. 223
      src/c/shop.c
  7. 1
      src/content/game_instructions.txt
  8. 2
      test/c/test_commands.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
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)
{
strcat(msg, "You can move north from here\n");
strcat(msg, "> You can move north from here\n");
}
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;

BIN
src/c/main

13
src/c/main.c

@ -211,7 +211,6 @@ int checkMove(char userInput[20])
// print actual location and messages from game.map
void printStatus()
{
Room actualRoom = map[playerPosition];
char moveMessage[30];
if (lastPlayerPosition > playerPosition)
@ -233,16 +232,6 @@ void printStatus()
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, map[playerPosition], playerPosition);
}
}

61
src/c/print_helper.c

@ -5,6 +5,7 @@
#include <ctype.h>
#include "print_helper.h"
#include "commands.h"
char *append(const char *s, char c);
@ -12,39 +13,71 @@ void printHeader(char *header)
{
printf("\n");
int len = strlen(header);
//set
// set
int maxSize = len * 3 + 3; // 2 spaces | left - mid - right = 3 -> 3 times length of header
char pHeader[maxSize];
char ch = '*';
for (int i = 0; i < maxSize; i++)
{
pHeader[i] = ch;
}
printf("%.*s\n", maxSize, pHeader);
printf("%-*s %s %-*s \n", len, "", header, len, "");
printf("%.*s\n", maxSize, pHeader);
}
void printItem(char *header, char *item, int itemID, int itemPrice)
{
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);
}
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
#define PRINT_HELPER_H
#include "map.h"
void printHeader(char *header);
void printItem(char *header, char *item, int itemID, int itemPrice);
void printRoomStatus(char *moveMessage, Room currentRoom, int playerPosition);
#endif

223
src/c/shop.c

@ -9,141 +9,192 @@
int *openShop(Item *availableItems, Player player)
{
int mode = 0;
int mode = -1;
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);
bool shopIsOpen = 1;
bool shopIsOpen = true;
if (mode == 0)
{
while (shopIsOpen == 1)
char *userInput = malloc(sizeof(char) * 1);
do
{
// header
char *header = "SHOP-Items";
printHeader(header);
// printf("%-5s %-30s %5s\n", "Index", "Name", "Price");
// items
int maxItemsShop = 0;
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);
maxItemsShop += 1;
}
// instruction
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
{
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)
{
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";
printHeader(header);
// printf("%-5s %-30s %5s\n", "Index", "Name", "Price");
// print inventory
int maxItemsInv = 0;
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");
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
{
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;
}

1
src/content/game_instructions.txt

@ -27,6 +27,7 @@ enter "south" to go south
alltime commands:
- '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):
- 'shop' -> opens the shop where you can buy or sell items

2
test/c/test_commands.c

@ -20,7 +20,7 @@ void test_commands(void)
Room* mapData;
mapData = getMap("./src/content/game.map");
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 */
// Die Funktion wird ausgeführt

Loading…
Cancel
Save