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.

261 lines
5.1 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. char *line = NULL;
  61. size_t len = 0;
  62. ssize_t read;
  63. stream = fopen(gameInstructionsFile, "r");
  64. if (stream == NULL)
  65. {
  66. printf("ERROR: couldn't open or find file: INSTRUCTIONS !\n");
  67. exit(EXIT_FAILURE); // exit
  68. }
  69. /* print line by line from file */
  70. while ((read = getline(&line, &len, stream)) != -1)
  71. {
  72. // printf("Retrieved line of length %u :\n", read);
  73. printf("%s", line);
  74. }
  75. free(line); /* Deallocate allocated memory */
  76. fclose(stream); /* closing file */
  77. acceptInstructions();
  78. }
  79. // setUp
  80. void setUp()
  81. {
  82. // define variables
  83. actualPlayer.id = 1;
  84. actualPlayer.wallet = 100;
  85. gameRunning = 1;
  86. // get Content
  87. map = getMap(gameMapFile);
  88. availableItems = getItems(itemsMapFile);
  89. }
  90. // accept rules and instructions for init dialogue
  91. void acceptInstructions()
  92. {
  93. char userInput[1];
  94. bool inputState = 0;
  95. int acceptCounter = 0;
  96. while (inputState == 0)
  97. {
  98. scanf(" %c", userInput);
  99. if (strcasecmp(userInput, "y") == 0)
  100. {
  101. acceptedRules = 1;
  102. inputState = 1; // break while
  103. }
  104. else if (strcasecmp(userInput, "n") == 0)
  105. {
  106. printf("You didn't read our rules & instructions. The game will close now. \n\n");
  107. acceptedRules = 0;
  108. inputState = 1; // break while
  109. }
  110. else
  111. {
  112. printf("Invalid Input!\n");
  113. }
  114. }
  115. }
  116. // process user input
  117. void processInput(char userInput[20])
  118. {
  119. if (checkExit(userInput) == 1)
  120. {
  121. gameRunning = 0;
  122. printf("!GAME EXIT!\n");
  123. }
  124. else if (strcmp(userInput, "shop") == 0)
  125. {
  126. Room actualRoom = map[playerPosition];
  127. if (actualRoom.shopAvailable == 1)
  128. {
  129. int *result = malloc(sizeof(int) * 2);
  130. result = openShop(availableItems, actualPlayer); // result > 0 -> integer = index of item OR result = 0 -> cancel
  131. if (result[0] == 0)
  132. {
  133. actualPlayer = buyItem(availableItems, result[1], actualPlayer);
  134. }
  135. else if (result[0] == 1)
  136. {
  137. actualPlayer = sellItem(result[1], actualPlayer);
  138. }
  139. }
  140. else
  141. {
  142. printf("You can't access the shop from here.");
  143. }
  144. }
  145. else if (strcmp(userInput, "inventory") == 0)
  146. {
  147. showInventory(actualPlayer);
  148. }
  149. else if (checkMove(userInput) == 1)
  150. {
  151. printf("Wrong Input!\n");
  152. }
  153. }
  154. // function for checking user input of exit
  155. int checkExit(char userInput[20])
  156. {
  157. if (strcmp(userInput, "esc") == 0 || strcmp(userInput, "exit") == 0 || strcmp(userInput, "quit") == 0)
  158. {
  159. return 1;
  160. }
  161. return 0;
  162. }
  163. // check is user moved
  164. int checkMove(char userInput[20])
  165. {
  166. Room r = map[playerPosition];
  167. inputCounter += 1;
  168. if (strcmp(userInput, "north") == 0)
  169. {
  170. lastPlayerPosition = playerPosition;
  171. if (playerPosition == (int)(mapMax - 1))
  172. {
  173. printf("You have reached the border. You have to go in the other direction!\n");
  174. }
  175. else
  176. {
  177. playerPosition = r.successor;
  178. }
  179. return 0;
  180. }
  181. else if (strcmp(userInput, "south") == 0)
  182. {
  183. lastPlayerPosition = playerPosition;
  184. if (playerPosition > 0)
  185. {
  186. playerPosition = r.predecessor;
  187. }
  188. else
  189. {
  190. printf("You have reached the border. You have to go in the other direction!\n");
  191. }
  192. return 0;
  193. }
  194. else
  195. {
  196. return 1;
  197. }
  198. }
  199. // print actual location and messages from game.map
  200. void printStatus()
  201. {
  202. Room actualRoom = map[playerPosition];
  203. char moveMessage[30];
  204. if (lastPlayerPosition > playerPosition)
  205. {
  206. strcpy(moveMessage, "You went to the south!");
  207. }
  208. else if (lastPlayerPosition < playerPosition)
  209. {
  210. strcpy(moveMessage, "You went to the north!");
  211. }
  212. else if (lastPlayerPosition == playerPosition && playerPosition == 0)
  213. {
  214. strcpy(moveMessage, "START");
  215. }
  216. else
  217. {
  218. strcpy(moveMessage, "You didn't move");
  219. }
  220. if (lastPlayerPosition != playerPosition || playerPosition == 0 && inputCounter == 0)
  221. {
  222. printf("\n\n################################################################################\n");
  223. printf("--> %s <--\n", moveMessage);
  224. printf("%s\n", actualRoom.nameRoom);
  225. printf("%s\n", actualRoom.msgRoom);
  226. printf("\n");
  227. char *possibleCommands = malloc(sizeof(char) * (500));
  228. possibleCommands = getPossibleCommands(map[playerPosition], playerPosition);
  229. printf("%s", possibleCommands);
  230. free(possibleCommands);
  231. }
  232. }