Advertisement
Guest User

Untitled

a guest
Jul 1st, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cstdio>
  4. #include <ctime>
  5. #include <cmath>
  6. #include <fstream>
  7. #include <conio.h>
  8. using namespace std;
  9.  
  10. int main()
  11. {
  12. srand(time(NULL));
  13. ifstream fin ("pacman.txt");
  14. int height, it, j, its=0, endgame=0, pacrow, paccollumn, exitrow, exitcolumn, numofgems=0, collectedgems=0, monsters=0, quit=0, works=0, counter4=0, randommove;
  15. fin >> height;
  16. char moves;
  17. string level;
  18. string line[33];
  19. string lines[12];
  20. int monsterrow[100];
  21. int monstercolumn[100];
  22. for ( int i=0; i<32; i++)
  23. {
  24. fin >> line[i];
  25. }
  26. cout <<"What level do you want? Choose easy, medium, hard, or extreme."<< endl;
  27. cin >> level;
  28. if (level=="medium")
  29. {
  30. for (int i=0; i<9; i++)
  31. {
  32. lines[i]=line[i];
  33. height=9;
  34. }
  35. }
  36. else if (level=="hard")
  37. {
  38. for (int i=9; i<18; i++)
  39. {
  40. height=9;
  41. lines[its]=line[i];
  42. its++;
  43. }
  44. }
  45. else if (level=="easy")
  46. {
  47. for (int i=18; i<23; i++)
  48. {
  49. height=5;
  50. lines[its]=line[i];
  51. its++;
  52. }
  53. }
  54. else if (level=="extreme")
  55. {
  56. for (int i=23; i<32; i++)
  57. {
  58. height=9;
  59. lines[its]=line[i];
  60. its++;
  61. }
  62. }
  63. for (int i=0; i<height; i++)
  64. {
  65. for (int j=0; j<lines[i].length(); j++)
  66. {
  67. if (lines[i][j]=='P')
  68. {
  69. pacrow=i;
  70. paccollumn=j;
  71. }
  72. if (lines[i][j]=='E')
  73. {
  74. exitrow=i;
  75. exitcolumn=j;
  76. }
  77. if (lines[i][j]=='G')
  78. {
  79. numofgems++;
  80. }
  81. if (lines[i][j]=='M')
  82. {
  83. monsterrow[counter4]=i;
  84. monstercolumn[counter4]=j;
  85. counter4++;
  86. }
  87. }
  88. }
  89. cout <<"Collect all the gems and get to the exit without being killed!"<< endl;
  90. while (endgame==0 && monsters==0)
  91. {
  92. if (lines[pacrow][paccollumn]=='E' && collectedgems==numofgems)
  93. {
  94. endgame=1;
  95. break;
  96. }
  97. if (lines[pacrow][paccollumn]=='E'&& collectedgems!=numofgems)
  98. {
  99. cout <<"You need to get all the gems first. That's a loss for not following instructions."<< endl;
  100. break;
  101. }
  102. if (lines[pacrow][paccollumn]=='G')
  103. {
  104. collectedgems++;
  105. }
  106. if (lines[pacrow][paccollumn]=='M')
  107. {
  108. monsters=1;
  109. break;
  110. }
  111. lines[pacrow][paccollumn]='P';
  112. //monstermoving
  113. for (int i=0; i<counter4; i++)
  114. {
  115. /*
  116. cout <<"move M from"<<i<< endl;
  117. cout <<monsterrow[i]<< endl;
  118. cout <<monstercolumn[i]<< endl;
  119. for (int j=0; j<height; j++)
  120. {
  121. cout <<lines[j]<< endl;
  122. }
  123. cout << "to" <<endl;
  124. */
  125. //cout <<"hi"<< endl;
  126. works=0;
  127. while (works==0)
  128. {
  129. //cout <<"hi"<< endl;
  130. randommove=rand()%4+1;
  131. if (randommove==1)
  132. {
  133. //cout <<"hi"<< endl;
  134. if (lines[monsterrow[i]-1][monstercolumn[i]]=='.' || lines[monsterrow[i]-1][monstercolumn[i]]=='P')
  135. {
  136. //cout <<"Hi"<< endl;
  137. works=1;
  138. lines[monsterrow[i]][monstercolumn[i]]='.';
  139. lines[monsterrow[i]-1][monstercolumn[i]]='M';
  140. monsterrow[i]--;
  141. }
  142. }
  143. if (randommove==2)
  144. {
  145. if (lines[monsterrow[i]+1][monstercolumn[i]]=='.'|| lines[monsterrow[i]+1][monstercolumn[i]]=='P')
  146. {
  147. works=1;
  148. lines[monsterrow[i]][monstercolumn[i]]='.';
  149. lines[monsterrow[i]+1][monstercolumn[i]]='M';
  150. monsterrow[i]++;
  151. }
  152. }
  153. if (randommove==3)
  154. {
  155. if (lines[monsterrow[i]][monstercolumn[i]-1]=='.'|| lines[monsterrow[i]][monstercolumn[i]-1]=='P')
  156. {
  157. works=1;
  158. lines[monsterrow[i]][monstercolumn[i]]='.';
  159. lines[monsterrow[i]][monstercolumn[i]-1]='M';
  160. monstercolumn[i]--;
  161. }
  162. }
  163. if (randommove==4)
  164. {
  165. if (lines[monsterrow[i]][monstercolumn[i]+1]=='.'|| lines[monsterrow[i]][monstercolumn[i]+1]=='P')
  166. {
  167. works=1;
  168. lines[monsterrow[i]][monstercolumn[i]]='.';
  169. lines[monsterrow[i]][monstercolumn[i]+1]='M';
  170. monstercolumn[i]++;
  171. }
  172. }
  173. }
  174. //for (int j=0; j<height; j++)
  175. //{
  176. //cout <<lines[j]<< endl;
  177. //}
  178. }
  179. for (int i=0; i<height; i++)
  180. {
  181. cout <<lines[i]<< endl;
  182. }
  183. if (lines[pacrow][paccollumn]=='M')
  184. {
  185. cout <<"Pacman was eaten"<< endl;
  186. monsters=1;
  187. break;
  188. }
  189. cout <<"Type either w, a, s, d for up, left, down, or right, or q for quit. Warning: an illegal move will allow the monsters to move but not you."<< endl;
  190. moves=getch();
  191. if (moves=='w')
  192. {
  193. if (lines[pacrow-1][paccollumn]=='.'||lines[pacrow-1][paccollumn]=='E'||lines[pacrow-1][paccollumn]=='M' || lines[pacrow-1][paccollumn]=='G')
  194. {
  195. lines[pacrow][paccollumn]='.';
  196. pacrow--;
  197. }
  198. else
  199. cout <<"Illegal move!"<< endl;
  200. }
  201. else if (moves=='a')
  202. {
  203. if (lines[pacrow][paccollumn-1]=='.'||lines[pacrow][paccollumn-1]=='E'||lines[pacrow][paccollumn-1]=='M' || lines[pacrow][paccollumn-1]=='G')
  204. {
  205. lines[pacrow][paccollumn]='.';
  206. paccollumn--;
  207. }
  208. else
  209. cout <<"Illegal move!"<< endl;
  210. }
  211. else if (moves=='s')
  212. {
  213. if (lines[pacrow+1][paccollumn]=='.'||lines[pacrow+1][paccollumn]=='E'||lines[pacrow+1][paccollumn]=='M' || lines[pacrow+1][paccollumn]=='G')
  214. {
  215. lines[pacrow][paccollumn]='.';
  216. pacrow++;
  217. }
  218. else
  219. cout <<"Illegal move!"<< endl;
  220. }
  221. else if (moves=='d')
  222. {
  223. if (lines[pacrow][paccollumn+1]=='.'||lines[pacrow][paccollumn+1]=='E'||lines[pacrow][paccollumn+1]=='M'|| lines[pacrow][paccollumn+1]=='G')
  224. {
  225. lines[pacrow][paccollumn]='.';
  226. paccollumn++;
  227. }
  228. else
  229. cout <<"Illegal move!"<< endl;
  230. }
  231. else if (moves=='q')
  232. {
  233. cout <<"Quitters never prosper, but you can quit, I guess."<< endl;
  234. quit=1;
  235. break;
  236. }
  237. else
  238. {
  239. cout <<"Illegal move! Try again"<< endl;
  240. }
  241. }
  242. if (endgame==1)
  243. cout <<"You beat the game!"<< endl;
  244. else if (quit==1)
  245. cout <<"I hope you won't quit next time!"<< endl;
  246. else
  247. cout <<"Gameover! You lost."<< endl;
  248. return 0;
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement