Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.95 KB | None | 0 0
  1. #include <iostream>
  2. using std::endl;
  3. using std::cout;
  4. using std::cin;
  5. #include <string>
  6. using std::string;
  7.  
  8. #include<Windows.h>
  9. #include<conio.h>
  10.  
  11. void welcome();
  12. char getKeyPress();
  13. void printLevel(int);
  14. void setMe(int);
  15. bool isExit(int, int, int);
  16. bool isWall(int, int, int);
  17. int getPos(int, int&);
  18. int getX(int, int &);
  19. void update(int, int, int);
  20. void makeSpace(int, int, int);
  21.  
  22. const char space = ' ';
  23. const char me = '@';
  24. char lvl1[15][15] = { { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
  25. { 'X', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
  26. { '#', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
  27. { '#', ' ', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
  28. { '#', '#', ' ', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
  29. { '#', '#', '#', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
  30. { '#', '#', '#', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
  31. { '#', '#', '#', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
  32. { '#', '#', '#', ' ', '#', '#', '#', '#', '#', ' ', ' ', ' ', ' ', ' ', 'O' },
  33. { '#', '#', '#', ' ', '#', '#', '#', '#', '#', ' ', '#', '#', '#', '#', '#' },
  34. { '#', '#', '#', ' ', ' ', ' ', '#', '#', '#', ' ', '#', '#', '#', '#', '#' },
  35. { '#', '#', '#', '#', '#', ' ', '#', '#', '#', ' ', '#', '#', '#', '#', '#' },
  36. { '#', '#', '#', '#', '#', ' ', '#', '#', '#', ' ', '#', '#', '#', '#', '#' },
  37. { '#', '#', '#', '#', '#', ' ', ' ', ' ', ' ', ' ', '#', '#', '#', '#', '#' },
  38. { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' } };
  39.  
  40. char lvl2[15][15] = { { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
  41. { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
  42. { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
  43. { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
  44. { '#', ' ', ' ', ' ', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
  45. { '#', ' ', '#', '#', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
  46. { '#', ' ', '#', '#', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
  47. { '#', ' ', ' ', '#', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
  48. { '#', '#', ' ', '#', ' ', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
  49. { '#', '#', ' ', '#', ' ', '#', '#', '#', '#', '#', '#', '#', '#', ' ', 'O' },
  50. { '#', '#', ' ', '#', ' ', '#', '#', '#', '#', '#', '#', '#', '#', ' ', '#' },
  51. { '#', '#', ' ', '#', ' ', '#', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
  52. { '#', '#', ' ', '#', ' ', '#', ' ', ' ', '#', '#', '#', '#', '#', '#', '#' },
  53. { 'X', ' ', ' ', '#', ' ', ' ', ' ', '#', '#', '#', '#', '#', '#', '#', '#' },
  54. { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' } };
  55.  
  56.  
  57. int main(void) {
  58. welcome();
  59. begin:
  60. system("CLS");
  61. cout << "\n\n\n\n\n\n\n \t\t\t";
  62. cout << "PLEASE SELECT A LEVEL \n\n \t\t\t1---2---3---4---5---6\n \t\t\t";
  63. int lvl;
  64. cin >> lvl;
  65. if (lvl != 2 && lvl != 1 && lvl > 0) {
  66. cout << endl << endl;
  67. cout << "\t\t";
  68. cout << "Level not available yet, please check back later." << endl;
  69. Sleep(1000);
  70. goto begin;
  71. }
  72. system("CLS");
  73. setMe(lvl);
  74. printLevel(lvl);
  75. int x, y;
  76.  
  77. while (1) {
  78.  
  79. char move = getKeyPress();
  80.  
  81. switch (move) {
  82.  
  83. case 'u':
  84. x = getPos(lvl, y);
  85. if (!isWall(x - 1, y, lvl)) {
  86. if (isExit(x - 1, y, lvl)) {
  87. system("CLS");
  88. cout << "You Win!" << endl;
  89. Sleep(2000);
  90. goto begin;;
  91. }
  92. system("CLS");
  93. makeSpace(lvl, x, y);
  94. update(lvl, x - 1, y);
  95. }
  96. break;
  97. case 'd':
  98. x = getPos(lvl, y);
  99. if (!isWall(x + 1, y, lvl)) {
  100. if (isExit(x + 1, y, lvl)) {
  101. system("CLS");
  102. cout << "You Win!" << endl;
  103. Sleep(2000);
  104. goto begin;;
  105. }
  106. system("CLS");
  107. makeSpace(lvl, x, y);
  108. update(lvl, x + 1, y);
  109. }
  110. break;
  111. case 'l':
  112. x = getPos(lvl, y);
  113. if (!isWall(x, y - 1, lvl)) {
  114. if (isExit(x, y - 1, lvl)) {
  115. system("CLS");
  116. cout << "You Win!" << endl;
  117. Sleep(2000);
  118. goto begin;;
  119. }
  120. system("CLS");
  121. makeSpace(lvl, x, y);
  122. update(lvl, x, y - 1);
  123. }
  124. break;
  125. case 'r':
  126. x = getPos(lvl, y);
  127. if (!isWall(x, y + 1, lvl)) {
  128. if (isExit(x, y + 1, lvl)) {
  129. system("CLS");
  130. cout << "You Win!" << endl;
  131. Sleep(2000);
  132. goto begin;;
  133. }
  134. system("CLS");
  135. makeSpace(lvl, x, y);
  136. update(lvl, x, y + 1);
  137. }
  138. break;
  139. default:
  140. break;
  141. }
  142.  
  143. }
  144.  
  145. return 0;
  146. }
  147.  
  148. void welcome() {
  149. string start = "WELCOME TO MAZE RUNNER v1.0";
  150. string indev = "Currently in development, only two levels available.";
  151. string howto = "Use the arrow keys and traverse through the maze. Exit is marked 'O'.";
  152. cout << endl;
  153. cout << "\n\n\n\n\n\n\n \t\t\t";
  154. for (auto ch : start) {
  155. cout << ch;
  156. Sleep(40);
  157. } cout << endl << endl;
  158. cout << " \t ";
  159. for (auto ch : indev) {
  160. cout << ch;
  161. Sleep(40);
  162. } cout << endl << endl << endl;
  163. cout << "\t";
  164. for (auto ch : howto) {
  165. cout << ch;
  166. Sleep(40);
  167. }
  168. Sleep(1500);
  169. }
  170.  
  171.  
  172. void printLevel(int lvl) {
  173. cout << "\n\n\n\n\n";
  174. if (lvl == 1) {
  175. for (int i = 0; i != 15; ++i) {
  176. cout << endl << "\t\t\t\t";
  177. for (int j = 0; j != 15; ++j) {
  178. cout << lvl1[i][j];
  179. }
  180. } cout << endl;
  181. }
  182. if (lvl == 2) {
  183. for (int i = 0; i != 15; ++i) {
  184. cout << endl << "\t\t\t\t";
  185. for (int j = 0; j != 15; ++j) {
  186. cout << lvl2[i][j];
  187. }
  188. } cout << endl;
  189. }
  190. }
  191.  
  192. void setMe(int lvl) {
  193. int x, y;
  194. if (lvl == 1) {
  195. x = getX(lvl, y);
  196. lvl1[x][y] = me;
  197. }
  198. if (lvl == 2) {
  199. x = getX(lvl, y);
  200. lvl2[x][y] = me;
  201. }
  202. }
  203. //got this function from a CPP forum
  204. char getKeyPress() {
  205.  
  206. char key = 127;
  207.  
  208. key = _getch();
  209.  
  210. if (key == 0 || key == -32) {
  211.  
  212. key = _getch();
  213.  
  214. if (key == 72) {
  215. key = 'u';
  216. }
  217. else if (key == 75) {
  218. key = 'l';
  219. }
  220. else if (key == 77) {
  221. key = 'r';
  222. }
  223. else if (key == 80) {
  224. key = 'd';
  225. }
  226. }
  227. return key;
  228. }
  229.  
  230. bool isExit(int x, int y, int lvl) {
  231. if (lvl == 1) {
  232. if (lvl1[x][y] == 'O') {
  233. return true;
  234. }
  235. else {
  236. return false;
  237. }
  238. }
  239. if (lvl == 2) {
  240. if (lvl2[x][y] == 'O') {
  241. return true;
  242. }
  243. else {
  244. return false;
  245. }
  246. }
  247. return true;
  248. }
  249.  
  250. int getPos(int lvl, int &y) {
  251. int xCoord;
  252. if (lvl == 1) {
  253. for (int i = 0; i != 15; ++i) {
  254. for (int j = 0; j != 15; ++j) {
  255. if (lvl1[i][j] == '@') {
  256. xCoord = i;
  257. y = j;
  258. return xCoord;
  259. }
  260. }
  261. }
  262. }
  263. if (lvl == 2) {
  264. for (int i = 0; i != 15; ++i) {
  265. for (int j = 0; j != 15; ++j) {
  266. if (lvl2[i][j] == '@') {
  267. xCoord = i;
  268. y = j;
  269. return xCoord;
  270. }
  271. }
  272. }
  273. }
  274. return 0;
  275. }
  276.  
  277. bool isWall(int x, int y, int lvl) {
  278. if (lvl == 1) {
  279. if (lvl1[x][y] == '#') {
  280. cout << "\n\t\t\tCannot move! That is a wall / boundary.";
  281. Sleep(400);
  282. system("CLS");
  283. printLevel(lvl);
  284. return true;
  285. }
  286. else {
  287. return false;
  288. }
  289. }
  290. if (lvl == 2) {
  291. if (lvl2[x][y] == '#') {
  292. cout << "\n\t\t\tCannot move! That is a wall / boundary.";
  293. Sleep(400);
  294. system("CLS");
  295. printLevel(lvl);
  296. return true;
  297. }
  298. else {
  299. return false;
  300. }
  301. }
  302. return true;
  303. }
  304.  
  305. int getX(int lvl, int &y) {
  306. int xCoord;
  307. if (lvl == 1) {
  308. for (int i = 0; i != 15; ++i) {
  309. for (int j = 0; j != 15; ++j) {
  310. if (lvl1[i][j] == 'X') {
  311. xCoord = i;
  312. y = j;
  313. return xCoord;
  314. }
  315. }
  316. }
  317. }
  318. if (lvl == 2) {
  319. for (int i = 0; i != 15; ++i) {
  320. for (int j = 0; j != 15; ++j) {
  321. if (lvl2[i][j] == 'X') {
  322. xCoord = i;
  323. y = j;
  324. return xCoord;
  325. }
  326. }
  327. }
  328. }
  329. return 0;
  330. }
  331.  
  332. void update(int lvl, int x, int y) {
  333. if (lvl == 1) {
  334. lvl1[x][y] = me;
  335. printLevel(lvl);
  336. }
  337. if (lvl == 2) {
  338. lvl2[x][y] = me;
  339. printLevel(lvl);
  340. }
  341. }
  342.  
  343. void makeSpace(int lvl, int x, int y) {
  344. if (lvl == 1) {
  345. lvl1[x][y] = space;
  346. }
  347. if (lvl == 2) {
  348. lvl2[x][y] = space;
  349. }
  350. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement