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.

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