Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.25 KB | None | 0 0
  1. // DnD.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <locale.h>
  8. #include <conio.h>
  9. #include <string.h>
  10. #include <time.h>
  11.  
  12. #define MAX_NAME_PLAYER 100
  13. #define PLAYER_INITIAL_ENERGY 100
  14. #define PLAYER_INITIAL_CELL 0
  15. #define PLAYER_INITIAL_OBJECT -1
  16. #define PLAYER_INITIAL_TREASURE -1
  17.  
  18. #define MONSTER_NAME 10
  19. #define MONSTER_INITIAL_ENERGY 150
  20. #define MONSTER_INITIAL_CELL 9
  21.  
  22. #define MAX_CELL_DESCRIPTION 200
  23. #define MAX_CELLS 20
  24. #define MAX_LIN 100
  25.  
  26. #define MAX_OBJECT_NAME 100
  27.  
  28. struct Player
  29. {
  30. char name[MAX_NAME_PLAYER];
  31. int energy;
  32. int cell;
  33. int object;
  34. int treasure;
  35. };
  36.  
  37. struct Monster
  38. {
  39. char name[MONSTER_NAME];
  40. int energy;
  41. int cell;
  42. };
  43.  
  44. struct Cell
  45. {
  46. int north;
  47. int south;
  48. int east;
  49. int west;
  50. int up;
  51. int down;
  52. char description1[MAX_CELL_DESCRIPTION];
  53. char description2[MAX_CELL_DESCRIPTION];
  54.  
  55. int object;
  56. int treasure;
  57. };
  58.  
  59. struct Object
  60. {
  61. char name[MAX_OBJECT_NAME];
  62. int damage;
  63. };
  64.  
  65. int CheckEndGame(struct Player *pPlayer, struct Monster *pMonster);
  66. int CheckPlayerLocation(struct Player *pPlayer);
  67. int GetArrowKeys();
  68. int InitializeMap(struct Cell cells[]);
  69. void Fight(struct Player *pPlayer, struct Monster *pMonster);
  70. void InitializeMonster(struct Monster *pMonster);
  71. void InitializeObjects(struct Object *pObject);
  72. void InitializePlayer(struct Player *pPlayer);
  73. void MoveMonster(struct Monster *pMonster);
  74. void PrintMap(struct Cell cells[], int nCells);
  75. void PrintMonster(struct Monster *pMonster);
  76. void PrintObjects(struct Object *pObject);
  77. void PrintPlayer(struct Player *pPlayer);
  78. void PrintPlayerLocation(struct Player *pPlayer, struct Cell cells[]);
  79.  
  80. int main()
  81. {
  82. struct Player player;
  83. struct Monster monster;
  84. struct Cell cells[MAX_CELLS];
  85. struct Object object;
  86. int nCells;
  87.  
  88. // Initialize Player
  89. InitializePlayer(&player);
  90. PrintPlayer(&player);
  91.  
  92. //Initialize Monster
  93. InitializeMonster(&monster);
  94. MoveMonster(&monster);
  95. PrintMonster(&monster);
  96.  
  97. // Initialize Map
  98. nCells = InitializeMap(cells);
  99. PrintMap(cells, nCells);
  100.  
  101. // Initialize Objects
  102. InitializeObjects(&object);
  103. PrintObjects(&object);
  104.  
  105. if (CheckEndGame(&player, &monster) == 0)
  106. {
  107. while (CheckEndGame(&player, &monster) != -1 && CheckEndGame(&player, &monster) != 1)
  108. {
  109. int PlayerLocation = player.cell;
  110. MoveMonster(&monster);
  111. PrintPlayerLocation(&player, cells);
  112. int arrow = GetArrowKeys();
  113. if (arrow == 0)
  114. {
  115. if (cells[PlayerLocation].north != -1)
  116. player.cell = cells[player.cell].north;
  117. }
  118. else if (arrow == 1)
  119. {
  120. if (cells[PlayerLocation].south != -1)
  121. player.cell = cells[PlayerLocation].south;
  122. }
  123. else if (arrow == 2)
  124. {
  125. if (cells[PlayerLocation].east != -1)
  126. player.cell = cells[PlayerLocation].east;
  127. }
  128. else if (arrow == 3)
  129. {
  130. if (cells[PlayerLocation].west != -1)
  131. player.cell = cells[PlayerLocation].west;
  132. }
  133. if (player.cell == monster.cell)
  134. {
  135. printf("\nYou have met with the monster!\n Let's Fight!\n");
  136. Fight(&player, &monster);
  137. PrintPlayer(&player);
  138. PrintMonster(&monster);
  139. }
  140. }
  141. }
  142. else if (CheckEndGame(&player, &monster) == -1)
  143. {
  144. printf("You Lost!");
  145. }
  146. else if (CheckEndGame(&player, &monster) == 1)
  147. {
  148. printf("You Won!");
  149. }
  150. else
  151. {
  152. PrintMonster(&monster);
  153. }
  154.  
  155.  
  156. printf("\nGood Luck %s.\n", player.name);
  157. system("pause");
  158. return 0;
  159. }
  160.  
  161. void InitializePlayer(struct Player *pPlayer)
  162. {
  163. printf("Hello Adventurer! What's your name?\n");
  164. scanf("%s", pPlayer->name);
  165. pPlayer->energy = PLAYER_INITIAL_ENERGY;
  166. pPlayer->cell = PLAYER_INITIAL_CELL;
  167. pPlayer->object = PLAYER_INITIAL_OBJECT;
  168. pPlayer->treasure = PLAYER_INITIAL_TREASURE;
  169. }
  170.  
  171. void PrintPlayer(struct Player *pPlayer)
  172. {
  173. printf("\n*** Player ***\n");
  174. printf("Name: %s\n", pPlayer->name);
  175. printf("Energy: %d\n", pPlayer->energy);
  176. printf("Cell: %d\n", pPlayer->cell);
  177. printf("Object: %d\n", pPlayer->object);
  178. printf("Treasure: %d\n", pPlayer->treasure);
  179. }
  180.  
  181. void InitializeMonster(struct Monster *pMonster)
  182. {
  183. strcpy(pMonster->name, "Owlbear");
  184. pMonster->energy = MONSTER_INITIAL_ENERGY;
  185. pMonster->cell = MONSTER_INITIAL_CELL;
  186. }
  187.  
  188. void PrintMonster(struct Monster *pMonster)
  189. {
  190. printf("\n*** Monster ***\n");
  191. printf("Name: %s\n", pMonster->name);
  192. printf("Energy: %d\n", pMonster->energy);
  193. printf("Cell: %d\n", pMonster->cell);
  194. }
  195.  
  196. int InitializeMap(struct Cell cells[])
  197. {
  198. FILE *f;
  199. char l[MAX_LIN];
  200. char *filename = (char*)"C:/Users/bruno/Desktop/DnD/DnD/map.txt";
  201.  
  202. f = fopen(filename, "r");
  203. if (f == NULL) {
  204. printf("Could not open file %s", filename);
  205. return 1;
  206. }
  207. int count = 0;
  208. int i = 0;
  209.  
  210. while (fgets(l, MAX_LIN, f) != NULL) {
  211. if (count == 0)
  212. {
  213. sscanf(l, "%d %d %d %d %d %d %d %d", &cells[i].north, &cells[i].south, &cells[i].east,
  214. &cells[i].west, &cells[i].up, &cells[i].down,
  215. &cells[i].object, &cells[i].treasure);
  216. }
  217. if (count == 1)
  218. {
  219. strcpy(cells[i].description1, l);
  220. }
  221. if (count == 2)
  222. {
  223. strcpy(cells[i].description2, l);
  224. }
  225. if(count == 3)
  226. {
  227. count = -1;
  228. i++;
  229. }
  230. count++;
  231. }
  232. fclose(f);
  233. return 10;
  234. }
  235.  
  236. void PrintMap(struct Cell cells[], int nCells)
  237. {
  238. for (int i = 0; i < nCells; i++) {
  239. printf("\nCell: %d \nNorth: %d \nSouth: %d \nEast: %d \nWest: %d \nUp: %d \nDown: %d \nObject: %d \nTreasure: %d \nDescription: %s \n",
  240. i, cells[i].north, cells[i].south, cells[i].east, cells[i].west,
  241. cells[i].up, cells[i].down, cells[i].object, cells[i].treasure, cells[i].description1, cells[i].description2);
  242. }
  243. }
  244.  
  245. void InitializeObjects(struct Object *pObject)
  246. {
  247. strcpy(pObject[0].name, "Ring of Contrariness");
  248. pObject[0].damage = 30;
  249. strcpy(pObject[1].name, "Bountiful Spade");
  250. pObject[1].damage = 50;
  251. strcpy(pObject[2].name, "Axe of the Dwarvish Lords");
  252. pObject[2].damage = 60;
  253.  
  254. }
  255.  
  256. void PrintObjects(struct Object *pObject)
  257. {
  258. for (int i = 0; i < 3; i++)
  259. {
  260. printf("\nObject: %d \nName: %s \nDamage: %d\n", i, pObject[i].name, pObject[i].damage);
  261. }
  262. }
  263.  
  264. int CheckEndGame(struct Player *pPlayer, struct Monster *pMonster)
  265. {
  266. if (pPlayer->energy <= 0)
  267. {
  268. return -1;
  269. }
  270. else if (pMonster->energy <= 0)
  271. {
  272. return 1;
  273. }
  274. else
  275. {
  276. return 0;
  277. }
  278. }
  279.  
  280. void MoveMonster(struct Monster *pMonster)
  281. {
  282. time_t t;
  283. srand(time(&t));
  284. pMonster->cell = rand() % 9 + 1;
  285. }
  286.  
  287. void PrintPlayerLocation(struct Player *pPlayer, struct Cell cells[])
  288. {
  289. printf("\nPlayer's Location: Room %d\n%s\n", pPlayer->cell, cells[pPlayer->cell].description1);
  290. }
  291.  
  292. int GetArrowKeys()
  293. {
  294. int arrow;
  295. unsigned char a;
  296. printf("\nPlease press an arrow key to proceed.\n");
  297. a = getch();
  298. //for detect the function\arrow keys
  299. //we must call the getch() again
  300. //testing if a is '0' or '0xE0'
  301. if (a == 0 || a == 0xE0) a = getch();
  302.  
  303. if (a == 72)
  304. {
  305. arrow = 0;
  306. printf("UP\n");
  307. }
  308. else if (a == 80)
  309. {
  310. arrow = 1;
  311. printf("DOWN\n");
  312. }
  313. else if (a == 75)
  314. {
  315. arrow = 2;
  316. printf("LEFT\n");
  317. }
  318. else if (a == 77)
  319. {
  320. arrow = 3;
  321. printf("RIGHT\n");
  322. }
  323. else
  324. {
  325. printf("%d\n", (int)a);
  326. }
  327. return arrow;
  328. }
  329.  
  330. void Fight(struct Player *pPlayer, struct Monster *pMonster)
  331. {
  332. pPlayer->energy = pPlayer->energy - 50;
  333. pMonster->energy = pMonster->energy - 100;
  334. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement