candyapplecorn

Easy Snake

May 20th, 2013
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.91 KB | None | 0 0
  1. //Easy Snake! Why is it easy? Because you have to hit enter to move the snake!
  2. //TRY ME
  3. //
  4. #include <iostream>
  5. #include <cstdlib>
  6. #include <ctime>
  7. #include <cmath>
  8. #include <iomanip>
  9. using namespace std;
  10.  
  11. const int ROWLENGTH=28, SELECTIONS=1, EsL=17; //EASYSNAKE_BOX_SIDE_LENGTH must be odd
  12. struct twoints {int x; int y;} coords[EsL*EsL];
  13.  
  14.  
  15. int display_menu(), display_menu_input(), movesnake(int& currentcoordinate), counter=-1;
  16. void objective(), easysnake(), header(), cls(), easysnakeheader(), easysnakestartscreen(), easysnakedirections(), easysnake_start_coordinates(), printeasysnake();
  17.  
  18.  
  19. int main()
  20. {
  21. srand (time(NULL));
  22. do
  23. {
  24. int subprogram=display_menu();
  25. switch(subprogram)
  26. {
  27. case 1:
  28. easysnake();
  29. break;
  30. }
  31. cls();
  32. } while(1);
  33. return 0;
  34. }
  35.  
  36. void easysnake()
  37. {
  38. cls();
  39. for (int its=0;its<EsL*EsL;its++)
  40. {
  41. coords[its].x=0;
  42. coords[its].y=0;
  43. }
  44. easysnakestartscreen();
  45. easysnake_start_coordinates();
  46. int currentcoordinate=static_cast<int>((EsL*EsL)/2)+1;
  47. do
  48. {
  49. objective();
  50. printeasysnake();
  51. int quit=movesnake(currentcoordinate);
  52. if (quit==1)
  53. break;
  54. }while(1);
  55. counter=0;
  56. return;
  57. }
  58.  
  59. int movesnake(int &currentcoordinate)
  60. {
  61. char wasd;
  62. int tempcoord;
  63. tempcoord=currentcoordinate;
  64. coords[currentcoordinate].x=0;
  65. int validity;
  66. do
  67. {
  68. cin >> wasd;
  69. if (wasd=='0')
  70. return 1;
  71. if (wasd=='a' || wasd == 'A')
  72. {
  73. if (tempcoord%EsL==0)
  74. return 1;
  75. currentcoordinate=tempcoord-1;
  76. coords[currentcoordinate].x=1;
  77. if (coords[currentcoordinate].y>0)
  78. return 1;
  79. coords[currentcoordinate].y=counter+2;
  80. validity=0;
  81. if (currentcoordinate<0)
  82. return 1;
  83.  
  84. }
  85. else if (wasd=='d' || wasd == 'D')
  86. {
  87. currentcoordinate=tempcoord+1;
  88. coords[currentcoordinate].x=1;
  89. if (coords[currentcoordinate].y>0)
  90. return 1;
  91. coords[currentcoordinate].y=counter+2;
  92. validity=0;
  93. if (currentcoordinate>289)
  94. return 1;
  95. if (currentcoordinate%EsL==0)
  96. return 1;
  97. }
  98. else if (wasd=='w' || wasd == 'W')
  99. {
  100. if (tempcoord<17)
  101. return 1;
  102. currentcoordinate=tempcoord-EsL;
  103. coords[currentcoordinate].x=1;
  104. if (coords[currentcoordinate].y>0)
  105. return 1;
  106. coords[currentcoordinate].y=counter+2;
  107. validity=0;
  108. }
  109. else if (wasd=='s' || wasd == 'S')
  110. {
  111. currentcoordinate=tempcoord+EsL;
  112. coords[currentcoordinate].x=1;
  113. if (coords[currentcoordinate].y>0)
  114. return 1;
  115. coords[currentcoordinate].y=counter+2;
  116. validity=0;
  117. if (currentcoordinate>289)
  118. return 1;
  119. }
  120. else if (wasd=='0')
  121. return 1;
  122. else
  123. {
  124. cout << "Invalid input. Enter w, a, s or d. Enter 0 to quit.\n";
  125. validity=1;
  126. }
  127. }while(validity!=0);
  128. for (int its=0; its<=EsL*EsL; its++)
  129. if (coords[its].y>0)
  130. coords[its].y--;
  131. cls();
  132.  
  133. }
  134. void objective()
  135. {
  136. for (int its=0; its<EsL*EsL;its++)
  137. if (coords[its].x==2)
  138. return;
  139. int randomint=1;
  140. do
  141. {
  142. randomint=rand()%(EsL*EsL);
  143. if (coords[randomint].x==1)
  144. randomint=1;
  145. }while(randomint==1);
  146. coords[randomint].x=2;
  147. counter++;
  148. return;
  149. }
  150.  
  151. void printeasysnake()
  152. {
  153. easysnakeheader();
  154. char ch=254;
  155. cout << ch;
  156. for (int its=1; its<=EsL*EsL;its++)
  157. {
  158. if (coords[its-1].y>=1&&!(coords[its-1].x==1))
  159. cout << "o";
  160. if (!(coords[its-1].x)&&coords[its-1].y==0)
  161. cout << " ";
  162. else if (coords[its-1].x==1)
  163. cout << "0";
  164. else if (coords[its-1].x==2)
  165. cout << "*";
  166. if (its==EsL*EsL)
  167. cout << ch << endl;
  168. else if (its==EsL)
  169. {
  170. cout << ch;
  171. if (counter>=1)
  172. cout << " P";
  173. cout << "\t[ *\'s eaten: ]"<< endl << ch;
  174. }
  175.  
  176. else if (its==EsL*2)
  177. {
  178. cout << ch;
  179. if (counter>=2)
  180. cout << " R";
  181. cout <<"\t[" << setw(8) << counter << " ]" << endl << ch;
  182. }
  183. else if (its==EsL*3)
  184. {
  185. cout << ch ;
  186. if (counter >=3)
  187. cout << " O";
  188. cout << endl << ch;
  189. }
  190. else if (its==EsL*4)
  191. {
  192. cout << ch ;
  193. if (counter >=4)
  194. cout << " G";
  195. cout << endl << ch;
  196. }
  197. else if (its==EsL*5)
  198. {
  199. cout << ch ;
  200. if (counter >=5)
  201. cout << " R";
  202. cout << endl << ch;
  203. }
  204. else if (its==EsL*6)
  205. {
  206. cout << ch ;
  207. if (counter >=6)
  208. cout << " A";
  209. cout << endl << ch;
  210. }
  211. else if (its==EsL*7)
  212. {
  213. cout << ch ;
  214. if (counter >=7)
  215. cout << " M";
  216. cout << endl << ch;
  217. }
  218. else if (its==EsL*8)
  219. {
  220. cout << ch ;
  221. if (counter >=8)
  222. cout << " M";
  223. cout << endl << ch;
  224. }
  225. else if (its==EsL*9)
  226. {
  227. cout << ch ;
  228. if (counter >=9)
  229. cout << " I";
  230. cout << endl << ch;
  231. }
  232. else if (its==EsL*10)
  233. {
  234. cout << ch ;
  235. if (counter >=10)
  236. cout << " N";
  237. cout << endl << ch;
  238. }
  239. else if (its==EsL*11)
  240. {
  241. cout << ch ;
  242. if (counter >=11)
  243. cout << " G";
  244. cout << endl << ch;
  245. }
  246. else if (its==EsL*12)
  247. {
  248. cout << ch ;
  249. if (counter >=12)
  250. cout << " ";
  251. cout << endl << ch;
  252. }
  253. else if (its==EsL*13)
  254. {
  255. cout << ch ;
  256. if (counter >=13)
  257. cout << " I";
  258. cout << endl << ch;
  259. }
  260. else if (its==EsL*14)
  261. {
  262. cout << ch ;
  263. if (counter >=14)
  264. cout << " N";
  265. cout << endl << ch;
  266. }
  267. else if (its==EsL*15)
  268. cout << ch << endl << ch;
  269. else if (its==EsL*16)
  270. {
  271. cout << ch ;
  272. if (counter >=16)
  273. cout << " C++";
  274. cout << endl << ch;
  275. }
  276. else if (its==EsL*17)
  277. cout << ch << endl << ch;
  278. else if (its%EsL==0 && its!=0)
  279. cout << ch << endl << ch;
  280. }
  281. easysnakeheader();
  282. }
  283.  
  284. void easysnake_start_coordinates()
  285. {
  286. coords[static_cast<int>((EsL*EsL)/2)+1].x=1;
  287. coords[static_cast<int>((EsL*EsL)/2)+1].y=1;
  288. }
  289.  
  290. void easysnakedirections()
  291. {
  292. cout << "You control the snake. \nUse WASD to control the snake.\nGuide the snake to the objectives!\nEnter 0 to quit.\n";
  293. }
  294. void easysnakestartscreen()
  295. {
  296. easysnakedirections();
  297. easysnakeheader();
  298. for (int iterations=0; iterations<EsL;iterations++)
  299. {
  300. char ch=254;
  301. cout << ch;
  302. for (int iterations=0; iterations<EsL;iterations++)
  303. {
  304. cout << " ";
  305. }
  306. cout << ch << endl;
  307. }
  308. easysnakeheader();
  309. char pause;
  310. cout << "Enter any key to continue\n";
  311. cin >> pause;
  312. }
  313.  
  314. void easysnakeheader()
  315. {
  316. char ch=254;
  317. for (int iterations=0;iterations<EsL+2; iterations++)
  318. cout << ch;
  319. cout << endl;
  320. }
  321.  
  322. void cls()
  323. {
  324. for (int iterations=0;iterations<=20;iterations++)
  325. cout << endl;
  326. }
  327. int display_menu()
  328. {
  329. header();
  330. cout << "Enter a menu choice.\n";
  331. cout << "\t1: Joe's Easy Snake\n";
  332. cout << "\t" << SELECTIONS+1 <<": EXIT\n";
  333. header();
  334. int selection=display_menu_input();
  335. return selection;
  336. }
  337.  
  338. int display_menu_input()
  339. {
  340. char input;
  341. int inputasint;
  342. cin >> input;
  343. inputasint=input;
  344. while (inputasint<49 || inputasint>49+SELECTIONS)
  345. {
  346. cout << "Bad input; enter between 1 and " << SELECTIONS+1 << endl;
  347. cin >> input;
  348. inputasint=input;
  349. }
  350. if (inputasint==49+SELECTIONS)
  351. exit (EXIT_SUCCESS);
  352. return inputasint-48;
  353. }int validity;
  354.  
  355. void header()
  356. {
  357. for (int iterations=0; iterations<ROWLENGTH; iterations++)
  358. cout << "=";
  359. cout << endl;
  360. }
Advertisement
Add Comment
Please, Sign In to add comment