Dungeon Crawl


SUBMITTED BY: Guest

DATE: Nov. 12, 2013, 2:46 a.m.

FORMAT: Text only

SIZE: 5.2 kB

HITS: 803

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <sstream>
  6. #include <vector>
  7. using std::cout;
  8. using std::cin;
  9. using std::endl;
  10. using std::string;
  11. using std::vector;
  12. enum Direction { None, Left, Right, Up, Down };
  13. enum Controller_t {PC , User};
  14. //Global Variables:
  15. char*board;
  16. int Height, Width;
  17. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  18. //Classes - Prototypes
  19. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  20. class board_t;
  21. class peg_t;
  22. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  23. //Classes Decleratios
  24. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  25. class peg_t
  26. {
  27. private:
  28. bool Moving;
  29. Controller_t controller;
  30. public:
  31. char Peg;
  32. peg_t (char,bool,Controller_t);
  33. Direction Move;
  34. };
  35. class board_t
  36. {
  37. private:
  38. void SetBoard(int,int);
  39. public:
  40. void SetValues(int, int);
  41. void PrintBoard();
  42. friend class peg_t;
  43. }Board;
  44. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  45. //Class Functions:
  46. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  47. //PEG_T_FUNCTIONS:
  48. peg_t::peg_t(const char a, bool b, Controller_t c)
  49. {
  50. Peg = a;
  51. Moving = b;
  52. controller = c;
  53. }
  54. //BOARD_T_FUNCTIONS:
  55. void board_t::SetValues(int a, int b)
  56. {
  57. SetBoard(a,b);
  58. }
  59. void board_t::SetBoard(int a, int b)
  60. {
  61. board = new char [a*b];
  62. for (int i = 0; i < a; i++)
  63. {
  64. for (int j = 0;j<b;j++)
  65. {
  66. board[i*b + j] = '.';
  67. }
  68. }
  69. }
  70. void board_t::PrintBoard()
  71. {
  72. for (int i = 0; i < Height; i++)
  73. {
  74. cout << "\t";
  75. for (int j = 0;j < Width ;j++)
  76. {
  77. cout << board[i*Width + j];
  78. }
  79. cout << endl;
  80. }
  81. }
  82. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  83. //Function:
  84. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  85. void DrawLine (char x)
  86. {
  87. cout << endl;
  88. for (int i = 0; i < 80;i++)
  89. {
  90. cout << x;
  91. }
  92. cout << endl;
  93. }
  94. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  95. //Main Function:
  96. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  97. int main ()
  98. {
  99. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  100. //Variables:
  101. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  102. peg_t Hero ('H',true, User);
  103. peg_t Enemy ('X', true, PC);
  104. peg_t Trap ('O', false, PC);
  105. peg_t Treasure ('T', false, PC);
  106. char Move;
  107. cout << "Please enter the Dimensions of the Board you want to play on[Min - 5*5 | Max - 8*8]: " << endl;
  108. cout << "Height: ";
  109. cin >> Height;
  110. if (Height > 8)
  111. Height = 8;
  112. if (Height < 5)
  113. Height = 5;
  114. cout << "Width: ";
  115. cin >> Width;
  116. if (Width > 8)
  117. Width = 8;
  118. if (Width < 5)
  119. Width = 5;
  120. Board.SetValues(Height,Width);
  121. int playh = 1, playw = 1;
  122. board [ (Height*Width) - 1 ] = Treasure.Peg;
  123. DrawLine('*');
  124. cout << "The target of the game is to reach the Treasure [T] without coming in contact with a Trap [O] or an Enemy[X].";
  125. cout << "The character is controlled by W,S,A & D, that alllow the character to move Up, Down, Left and Right respectively\n";
  126. DrawLine ('*');
  127. cout << endl;
  128. while (true)
  129. {
  130. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  131. //board[ (Row-1) * Width + (Column-1) ] = CHARACTER
  132. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  133. board[ (playh-1) * (Width) + (playw-1)] = Hero.Peg;
  134. Board.PrintBoard();
  135. if (board [(Height*Width)-1] != Treasure.Peg)
  136. {
  137. cout << "Congratulations. You have won the Game!!!";
  138. break;
  139. }
  140. cout << "In What direction do you want to move: ";
  141. cin >> Move;
  142. cin.sync();
  143. switch (Move)
  144. {
  145. case 'W':
  146. case 'w':
  147. Hero.Move = Up;
  148. break;
  149. case 'S':
  150. case 's':
  151. Hero.Move = Down;
  152. break;
  153. case 'A':
  154. case 'a':
  155. Hero.Move = Left;
  156. break;
  157. case 'D':
  158. case 'd':
  159. Hero.Move = Right;
  160. break;
  161. }
  162. switch (Hero.Move)
  163. {
  164. case Up:
  165. if (playh != 1)
  166. {
  167. board[ (playh-1) * Width + (playw-1)] = '.';
  168. playh -= 1;
  169. }
  170. break;
  171. case Down:
  172. if (playh != Height)
  173. {
  174. board[ (playh-1) * Width + (playw-1)] = '.';
  175. playh += 1;
  176. }
  177. break;
  178. case Left:
  179. if (playw != 1)
  180. {
  181. board[ (playh-1) * Width + (playw-1)] = '.';
  182. playw -= 1;
  183. }
  184. break;
  185. case Right:
  186. if (playw != Width)
  187. {
  188. board[ (playh-1) * Width + (playw-1)] = '.';
  189. playw += 1;
  190. }
  191. break;
  192. }
  193. DrawLine('-');
  194. }
  195. cout << endl << endl;
  196. DrawLine ('=');
  197. cout << endl;
  198. cout << "Press ENTER/RETURN to Exit . . .";
  199. cin.sync();
  200. cin.get();
  201. return 0;
  202. }

comments powered by Disqus