You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

244 lines
4.8 KiB

  1. // BIBs
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #include <string.h>
  6. #include "map.h"
  7. #include "nav_helper.h"
  8. #include "items.h"
  9. #include "shop.h"
  10. #include "player.h"
  11. #include "commands.h"
  12. bool gameRunning;
  13. bool acceptedRules;
  14. // declare needed variables
  15. Room *map;
  16. Item *availableItems;
  17. Player actualPlayer;
  18. int inputCounter = 0;
  19. // content
  20. char *gameInstructionsFile = "../../src/content/game_instructions.txt";
  21. char *gameMapFile = "../../src/content/game.map";
  22. char *itemsMapFile = "../../src/content/items.map";
  23. // navigation
  24. int playerPosition = 0;
  25. int lastPlayerPosition = 0;
  26. // function declarations
  27. void printInit();
  28. void setUp();
  29. void acceptInstructions();
  30. void processInput();
  31. int checkExit();
  32. int checkMove();
  33. void printStatus();
  34. int main()
  35. {
  36. // init and instructions
  37. printInit();
  38. if (acceptedRules == 1)
  39. {
  40. setUp();
  41. char userInput[20]; // maximum defined user input length
  42. while (gameRunning == 1) // while running
  43. {
  44. // Print current status
  45. printStatus();
  46. // User Input
  47. scanf(" %s", userInput);
  48. printf("\n");
  49. // NEXT STEP:
  50. // Processing
  51. processInput(userInput);
  52. }
  53. }
  54. return 0;
  55. }
  56. // init dialogue
  57. void printInit()
  58. {
  59. FILE *stream;
  60. stream = getStream(gameInstructionsFile);
  61. printStreamLines(stream);
  62. fclose(stream); /* closing file */
  63. acceptInstructions();
  64. }
  65. // setUp
  66. void setUp()
  67. {
  68. // define variables
  69. actualPlayer.id = 1;
  70. actualPlayer.wallet = 100;
  71. gameRunning = 1;
  72. // get Content
  73. map = getMap(gameMapFile);
  74. availableItems = getItems(itemsMapFile);
  75. }
  76. // accept rules and instructions for init dialogue
  77. void acceptInstructions()
  78. {
  79. char userInput[1];
  80. bool inputState = 0;
  81. int acceptCounter = 0;
  82. while (inputState == 0)
  83. {
  84. scanf(" %c", userInput);
  85. if (strcasecmp(userInput, "y") == 0)
  86. {
  87. acceptedRules = 1;
  88. inputState = 1; // break while
  89. }
  90. else if (strcasecmp(userInput, "n") == 0)
  91. {
  92. printf("You didn't read our rules & instructions. The game will close now. \n\n");
  93. acceptedRules = 0;
  94. inputState = 1; // break while
  95. }
  96. else
  97. {
  98. printf("Invalid Input!\n");
  99. }
  100. }
  101. }
  102. // process user input
  103. void processInput(char userInput[20])
  104. {
  105. if (checkExit(userInput) == 1)
  106. {
  107. gameRunning = 0;
  108. printf("!GAME EXIT!\n");
  109. }
  110. else if (strcmp(userInput, "shop") == 0)
  111. {
  112. Room actualRoom = map[playerPosition];
  113. if (actualRoom.shopAvailable == 1)
  114. {
  115. int *result = malloc(sizeof(int) * 2);
  116. result = openShop(availableItems, actualPlayer); // result > 0 -> integer = index of item OR result = 0 -> cancel
  117. if (result[0] == 0)
  118. {
  119. actualPlayer = buyItem(availableItems, result[1], actualPlayer);
  120. }
  121. else if (result[0] == 1)
  122. {
  123. actualPlayer = sellItem(result[1], actualPlayer);
  124. }
  125. }
  126. else
  127. {
  128. printf("You can't access the shop from here.");
  129. }
  130. }
  131. else if (strcmp(userInput, "inventory") == 0)
  132. {
  133. showInventory(actualPlayer);
  134. }
  135. else if (checkMove(userInput) == 1)
  136. {
  137. printf("Wrong Input!\n");
  138. }
  139. }
  140. // function for checking user input of exit
  141. int checkExit(char userInput[20])
  142. {
  143. if (strcmp(userInput, "esc") == 0 || strcmp(userInput, "exit") == 0 || strcmp(userInput, "quit") == 0)
  144. {
  145. return 1;
  146. }
  147. return 0;
  148. }
  149. // check is user moved
  150. int checkMove(char userInput[20])
  151. {
  152. Room r = map[playerPosition];
  153. inputCounter += 1;
  154. if (strcmp(userInput, "north") == 0)
  155. {
  156. lastPlayerPosition = playerPosition;
  157. if (playerPosition == (int)(mapMax - 1))
  158. {
  159. printf("You have reached the border. You have to go in the other direction!\n");
  160. }
  161. else
  162. {
  163. playerPosition = r.successor;
  164. }
  165. return 0;
  166. }
  167. else if (strcmp(userInput, "south") == 0)
  168. {
  169. lastPlayerPosition = playerPosition;
  170. if (playerPosition > 0)
  171. {
  172. playerPosition = r.predecessor;
  173. }
  174. else
  175. {
  176. printf("You have reached the border. You have to go in the other direction!\n");
  177. }
  178. return 0;
  179. }
  180. else
  181. {
  182. return 1;
  183. }
  184. }
  185. // print actual location and messages from game.map
  186. void printStatus()
  187. {
  188. Room actualRoom = map[playerPosition];
  189. char moveMessage[30];
  190. if (lastPlayerPosition > playerPosition)
  191. {
  192. strcpy(moveMessage, "You went to the south!");
  193. }
  194. else if (lastPlayerPosition < playerPosition)
  195. {
  196. strcpy(moveMessage, "You went to the north!");
  197. }
  198. else if (lastPlayerPosition == playerPosition && playerPosition == 0)
  199. {
  200. strcpy(moveMessage, "START");
  201. }
  202. else
  203. {
  204. strcpy(moveMessage, "You didn't move");
  205. }
  206. if (lastPlayerPosition != playerPosition || playerPosition == 0 && inputCounter == 0)
  207. {
  208. printf("\n\n################################################################################\n");
  209. printf("--> %s <--\n", moveMessage);
  210. printf("%s\n", actualRoom.nameRoom);
  211. printf("%s\n", actualRoom.msgRoom);
  212. printf("\n");
  213. char *possibleCommands = malloc(sizeof(char) * (500));
  214. possibleCommands = getPossibleCommands(map[playerPosition], playerPosition);
  215. printf("%s", possibleCommands);
  216. free(possibleCommands);
  217. }
  218. }