// BIBs #include #include #include #include #include "map.h" #include "utils.h" #include "items.h" #include "shop.h" #include "player.h" #include "commands.h" //variables important to play the game bool gameRunning; bool acceptedRules; // declare needed variables Room *gameMap; //game Map Item *availableItems; //shop items Player actualPlayer; //player int inputCounter = 0; //need to check on some positions // content char *gameInstructionsFile = "../../src/content/game_instructions.txt"; char *gameMapFile = "../../src/content/game.map"; char *itemsMapFile = "../../src/content/items.map"; // navigation int playerPosition = 0; int lastPlayerPosition = 0; // function declarations void printInit(); void setUp(); void acceptInstructions(); void processInput(); int checkExit(); int checkMove(); void printStatus(); int main() { // init and instructions printInit(); if (acceptedRules == 1) { setUp(); char userInput[20]; // maximum defined user input length while (gameRunning == 1) // while running { // Print current status printStatus(); // User Input scanf(" %s", userInput); printf("\n"); // NEXT STEP: // Processing processInput(userInput); } } return 0; } // init dialogue void printInit() { FILE *stream; stream = getStream(gameInstructionsFile); printStreamLines(stream); fclose(stream); /* closing file */ acceptInstructions(); } // setUp void setUp() { // define variables actualPlayer.id = 1; actualPlayer.wallet = 100; gameRunning = 1; // get Content gameMap = getMap(gameMapFile); availableItems = getItems(itemsMapFile); } // accept rules and instructions for init dialogue void acceptInstructions() { char userInput[1]; bool inputState = 0; int acceptCounter = 0; while (inputState == 0) { scanf(" %c", userInput); if (strcasecmp(userInput, "y") == 0) { acceptedRules = 1; inputState = 1; // break while } else if (strcasecmp(userInput, "n") == 0) { printf("You didn't read our rules & instructions. The game will close now. \n\n"); acceptedRules = 0; inputState = 1; // break while } else { printf("Invalid Input!\n"); } } } // process user input void processInput(char userInput[20]) { if (checkExit(userInput) == 1) { gameRunning = 0; printf("!GAME EXIT!\n"); } else if (strcmp(userInput, "shop") == 0) { lastPlayerPosition = playerPosition; //playerPosition doesn't change but lastPlayerPosition needs update Room actualRoom = gameMap[playerPosition]; if (actualRoom.shopAvailable == 1) { int *result = malloc(sizeof(int) * 2); result = openShop(availableItems, actualPlayer); // result > 0 -> integer = index of item OR result = 0 -> cancel if (result[0] == 0) { actualPlayer = buyItem(availableItems, result[1], actualPlayer); } else if (result[0] == 1) { actualPlayer = sellItem(result[1], actualPlayer); } } else { printf("You can't access the shop from here."); } } else if (strcmp(userInput, "inventory") == 0) { showInventory(actualPlayer); } else if (checkMove(userInput) == 1) { printf("Wrong Input!\n"); } } // function for checking user input of exit int checkExit(char userInput[20]) { if (strcmp(userInput, "esc") == 0 || strcmp(userInput, "exit") == 0 || strcmp(userInput, "quit") == 0) { return 1; } return 0; } // check is user moved int checkMove(char userInput[20]) { Room r = gameMap[playerPosition]; inputCounter += 1; if (strcmp(userInput, "north") == 0) { lastPlayerPosition = playerPosition; if (playerPosition == (int)(mapMax - 1)) { printf("You have reached the border. You have to go in the other direction!\n"); } else { playerPosition = r.successor; } return 0; } else if (strcmp(userInput, "south") == 0) { lastPlayerPosition = playerPosition; if (playerPosition > 0) { playerPosition = r.predecessor; } else { printf("You have reached the border. You have to go in the other direction!\n"); } return 0; } else { return 1; } } // print actual location and messages from game.map void printStatus() { char moveMessage[30]; if (lastPlayerPosition > playerPosition) { strcpy(moveMessage, "You went to the south!"); } else if (lastPlayerPosition < playerPosition) { strcpy(moveMessage, "You went to the north!"); } else if (lastPlayerPosition == playerPosition && playerPosition == 0) { strcpy(moveMessage, "START"); } else if(lastPlayerPosition == playerPosition) { strcpy(moveMessage, "You didn't move"); } if (lastPlayerPosition != playerPosition || lastPlayerPosition == playerPosition || playerPosition == 0 && inputCounter == 0) { printRoomStatus(moveMessage, gameMap[playerPosition], playerPosition); } }