Advertisement
Guest User

Untitled

a guest
Mar 6th, 2015
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.05 KB | None | 0 0
  1. #include <conio.h>
  2. #include <iostream>
  3. #include <locale.h>
  4. #include <time.h>
  5. #include <chrono>
  6. #include <thread>
  7. #include <vector>
  8. #include <iomanip>
  9. #include <array>
  10.  
  11. using std::cout; using std::cin; using std::endl; using std::string; using std::vector;
  12. const int MAX_SIZE = 20, MAX_DIF = 3;
  13. int tabuleiro[MAX_SIZE][MAX_SIZE];
  14. int x, y, moveDirection, snakeLength, newx, newy, dificulty, score, timeri, deltat;
  15. int chooseDimension(), chooseDificulty();
  16. void chooseHorizontal(), chooseVertical(), startGame(), testHit(), theGame(), placeFood(), displayBoard(), moveSnake(), createNewPiece(), correctPlace(), displayHorizontalBorder(), addScore(), setDificulty(), displayScore(), resetArray();
  17. bool error = false, food = false, lose = false, ate;
  18. vector<int> xsnake, ysnake;
  19.  
  20. int main()
  21. {
  22. char op;
  23. setlocale(LC_ALL, "");
  24. srand(time(NULL));
  25. do
  26. {
  27. system("CLS");
  28. cout << "Bem-vindo ao Snake!" << endl;
  29. cout << "Escolha as dimensões do tabuleiro:" << endl;
  30. chooseHorizontal();
  31. chooseVertical();
  32. setDificulty();
  33. startGame();
  34. theGame();
  35. deltat = time(NULL) - timeri;
  36. setlocale(LC_ALL, "");
  37. cout << "\nPerdeste! Aguentaste-te durante " << deltat << " segundos!\n";
  38. cout << "Queres começar de novo? Pressiona Y para jogar novamente. Tecla N para fechar.\n";
  39. std::this_thread::sleep_for(std::chrono::milliseconds(500));
  40. do
  41. {
  42. op = _getch();
  43. } while (!(op == 'Y' || op == 'y' || op == 'N' || op == 'n'));
  44. } while (op == 'Y' || op == 'y');
  45. return 0;
  46. }
  47.  
  48. void setDificulty()
  49. {
  50. while (true)
  51. {
  52. cout << "Dificuldade? (Maximo: " << MAX_DIF << "): ";
  53. int v;
  54. v = chooseDificulty();
  55. if (!error)
  56. {
  57. dificulty = v;
  58. break;
  59. }
  60. }
  61. return;
  62. }
  63.  
  64. int chooseDificulty()
  65. {
  66. int v = 0;
  67. cin >> v;
  68. cin.ignore(100, '\n');
  69. if (v <= 0)
  70. {
  71. cout << "Tem de escolher uma dificuldade positiva\n";
  72. error = true;
  73. }
  74. else if (v > MAX_DIF)
  75. {
  76. cout << "Usou um valor demasiado elevado.\n";
  77. error = true;
  78. }
  79. else error = false;
  80. return v;
  81. }
  82.  
  83. int chooseDimension()
  84. {
  85. int v = 0;
  86. cin >> v;
  87. cin.ignore(100, '\n');
  88. if (v <= 0)
  89. {
  90. cout << "Tem de escolher uma dimensão positiva\n";
  91. error = true;
  92. }
  93. else if (v > MAX_SIZE)
  94. {
  95. cout << "Usou um valor demasiado elevado.\n";
  96. error = true;
  97. }
  98. else error = false;
  99. return v;
  100. }
  101.  
  102. void chooseHorizontal()
  103. {
  104. while (true)
  105. {
  106. cout << "Dimensão horizontal (Maximo: " << MAX_SIZE << "): ";
  107. int v;
  108. v = chooseDimension();
  109. if (!error)
  110. {
  111. x = v - 1;
  112. break;
  113. }
  114. }
  115. return;
  116. }
  117.  
  118. void chooseVertical()
  119. {
  120. while (true)
  121. {
  122. cout << "Dimensão vertical (Maximo: " << MAX_SIZE << "): ";
  123. int v;
  124. v = chooseDimension();
  125. if (!error)
  126. {
  127. y = v - 1;
  128. break;
  129. }
  130. }
  131. return;
  132. }
  133.  
  134. void startGame()
  135. {
  136. resetArray();
  137. xsnake = {};
  138. ysnake = {};
  139. score = 0;
  140. snakeLength = 1;
  141. error = false;
  142. food = false;
  143. lose = false;
  144. int xpos, ypos;
  145. xpos = rand() % x;
  146. xsnake.push_back(xpos);
  147. ypos = rand() % y;
  148. ysnake.push_back(ypos);
  149. moveDirection = rand() % 4 + 1;
  150. tabuleiro[ypos][xpos] = 1;
  151. timeri = time(NULL);
  152. return;
  153. }
  154.  
  155. void resetArray()
  156. {
  157. for (int i = 0; i <= y; i++)
  158. for (int c = 0; c <= x; c++)
  159. tabuleiro[i][c] = 0;
  160. }
  161.  
  162. void theGame()
  163. {
  164. setlocale(LC_ALL, "C");
  165. int dif = 12 - dificulty * 3;
  166. while (true)
  167. {
  168. if (!food) placeFood();
  169. displayBoard();
  170. for (int i = 0; i <= dif; i++)
  171. {
  172. testHit();
  173. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  174. }
  175. moveSnake();
  176. if (lose) return;
  177. }
  178. }
  179.  
  180. void moveSnake()
  181. {
  182. switch (moveDirection)
  183. {
  184. case 1:
  185. newx = xsnake[0];
  186. newy = ysnake[0] - 1;
  187. break;
  188. case 2:
  189. newx = xsnake[0] + 1;
  190. newy = ysnake[0];
  191. break;
  192. case 3:
  193. newx = xsnake[0];
  194. newy = ysnake[0] + 1;
  195. break;
  196. case 4:
  197. newx = xsnake[0] - 1;
  198. newy = ysnake[0];
  199. break;
  200. }
  201. correctPlace();
  202. int elem = tabuleiro[newy][newx];
  203. if (elem == 2)
  204. {
  205. food = false;
  206. ate = true;
  207. addScore();
  208. createNewPiece();
  209. }
  210. if (elem == 1)
  211. {
  212. lose = true;
  213. return;
  214. }
  215. else ate = false;
  216. if (!ate)
  217. {
  218. tabuleiro[ysnake[snakeLength - 1]][xsnake[snakeLength - 1]] = 0;
  219. }
  220. for (int i = snakeLength - 1; i >= 1; i--)
  221. {
  222. xsnake[i] = xsnake[i - 1];
  223. ysnake[i] = ysnake[i - 1];
  224. }
  225. xsnake[0] = newx;
  226. ysnake[0] = newy;
  227. tabuleiro[newy][newx] = 1;
  228. return;
  229. }
  230.  
  231. void createNewPiece()
  232. {
  233. snakeLength++;
  234. xsnake.push_back(xsnake[snakeLength - 2]);
  235. ysnake.push_back(ysnake[snakeLength - 2]);
  236. return;
  237. }
  238.  
  239. void correctPlace()
  240. {
  241. if (newx > x) newx = 0;
  242. else if (newx < 0) newx = x;
  243. else if (newy > y) newy = 0;
  244. else if (newy < 0) newy = y;
  245. return;
  246. }
  247.  
  248. void placeFood()
  249. {
  250. int xpos, ypos;
  251. do
  252. {
  253. xpos = rand() % x;
  254. ypos = rand() % y;
  255. if (!(1 == tabuleiro[ypos][xpos]))
  256. {
  257. tabuleiro[ypos][xpos] = 2;
  258. food = true;
  259. error = false;
  260. }
  261. else error = true;
  262. } while (error);
  263. return;
  264. }
  265.  
  266. void addScore()
  267. {
  268. score = score + 10 * dificulty;
  269. return;
  270. }
  271.  
  272. void displayBoard()
  273. {
  274. system("CLS");
  275. displayHorizontalBorder();
  276. cout << endl;
  277. displayScore();
  278. displayHorizontalBorder();
  279. cout << endl;
  280. for (int i = 0; i <= y; i++)
  281. {
  282. cout << "|";
  283. for (int c = 0; c <= x; c++)
  284. {
  285. int elem = tabuleiro[i][c];
  286. if (elem == 0) cout << " ";
  287. else if (1 == elem) cout << "o";
  288. else cout << "*";
  289. }
  290. cout << "|" << endl;
  291. }
  292. displayHorizontalBorder();
  293. return;
  294. }
  295.  
  296. void displayScore()
  297. {
  298. cout << "|Score: " << std::setw(x - 6) << score << "|";
  299. cout << endl;
  300. }
  301.  
  302. void displayHorizontalBorder()
  303. {
  304. for (int i = -2; i <= x; i++)
  305. {
  306. if (-2 == i || x == i) cout << "+";
  307. else cout << "-";
  308. }
  309. }
  310.  
  311. void inputControl(char control)
  312. {
  313. switch (control)
  314. {
  315. case 'H':
  316. if (3 != moveDirection) moveDirection = 1;
  317. break;
  318. case 'M':
  319. if (4 != moveDirection) moveDirection = 2;
  320. break;
  321. case 'P':
  322. if (1 != moveDirection) moveDirection = 3;
  323. break;
  324. case 'K':
  325. if (2 != moveDirection) moveDirection = 4;
  326. break;
  327. }
  328. return;
  329. }
  330.  
  331. void testHit()
  332. {
  333. if (_kbhit())
  334. {
  335. char control;
  336. control = (_getch(), _getch());
  337. inputControl(control);
  338. }
  339. return;
  340. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement