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.

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