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.

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