Advertisement
SIKER_98

7

Apr 27th, 2020
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <unistd.h>
  3.  
  4. using namespace std;
  5.  
  6. class Snake {
  7. public:
  8. int x, y;
  9. Snake *next;
  10.  
  11. Snake(int x, int y) {
  12. this->x = x;
  13. this->y = y;
  14. this->next = NULL;
  15. }
  16.  
  17. static void swap(int tx, int ty, Snake *tmp) {
  18. int x, y;
  19. x = tmp->x;
  20. y = tmp->y;
  21.  
  22. tmp->x = tx;
  23. tmp->y = ty;
  24. }
  25.  
  26. };
  27.  
  28. class Game {
  29. private:
  30. int board[10][10][1];
  31. int szerokosc, dlugosc;
  32. int points;
  33. int direction;
  34. Snake *head;
  35. int papuX, papuY;
  36.  
  37. public:
  38. Game(int szerokosc, int dlugosc) {
  39. this->szerokosc = szerokosc;
  40. this->dlugosc = dlugosc;
  41. this->points = 0;
  42. head = new Snake(5, 5);
  43. head->x = 5;
  44. head->y = 5;
  45. this->direction = 1;
  46. newPapu();
  47. }
  48.  
  49. void pressUp() {
  50. this->direction = 0;
  51. }
  52.  
  53. void pressDown() {
  54. this->direction = 1;
  55. }
  56.  
  57. void pressLeft() {
  58. this->direction = 2;
  59. }
  60.  
  61. void pressRight() {
  62. this->direction = 4;
  63. }
  64.  
  65. bool sprawdz() {
  66. if (head->x == szerokosc-1 && direction == 4)return false;
  67. if (head->x == 0 && direction == 2)return false;
  68. if (head->y == dlugosc-1 && direction == 0)return false;
  69. if (head->y == 0 && direction == 1)return false;
  70. return true;
  71. }
  72.  
  73. void chodzenie() {
  74. Snake tmp = *head;
  75.  
  76. switch (this->direction) {
  77. case 0:
  78. while (tmp.next != NULL) {
  79. int tx, ty;
  80. tx = tmp.x;
  81. ty = tmp.y;
  82. if (sprawdz()) {
  83. tmp.y++;
  84. tmp = *tmp.next;
  85. Snake::swap(tx, ty, &tmp);
  86. }
  87. }
  88. if (sprawdz())tmp.y++;
  89. *head = tmp;
  90. break;
  91. case 1:
  92. while (tmp.next != NULL) {
  93. int tx, ty;
  94. tx = tmp.x;
  95. ty = tmp.y;
  96. if (sprawdz()) {
  97. tmp.y--;
  98. tmp = *tmp.next;
  99. Snake::swap(tx, ty, &tmp);
  100. }
  101. }
  102. if (sprawdz())tmp.y--;
  103. *head = tmp;
  104. break;
  105. case 2:
  106. while (tmp.next != NULL) {
  107. int tx, ty;
  108. tx = tmp.x;
  109. ty = tmp.y;
  110. if (sprawdz()) {
  111. tmp.x--;
  112. tmp = *tmp.next;
  113. Snake::swap(tx, ty, &tmp);
  114. }
  115. }
  116. if (sprawdz())tmp.x--;
  117. *head = tmp;
  118. break;
  119. case 4:
  120. while (tmp.next != NULL) {
  121. int tx, ty;
  122. tx = tmp.x;
  123. ty = tmp.y;
  124. if (sprawdz()) {
  125. tmp.x++;
  126. tmp = *tmp.next;
  127. Snake::swap(tx, ty, &tmp);
  128. }
  129. }
  130. if (sprawdz())tmp.x++;
  131. *head = tmp;
  132. break;
  133. }
  134.  
  135. if (this->head->x == papuX && this->head->y == papuY) {
  136. this->points++;
  137. Snake *nowy = new Snake(papuX, papuY);
  138.  
  139. switch (direction) {
  140. case 0:
  141. nowy->y--;
  142. break;
  143. case 1:
  144. nowy->y++;
  145. break;
  146. case 2:
  147. nowy->x++;
  148. break;
  149. case 4:
  150. nowy->x--;
  151. break;
  152. }
  153. head->next = nowy;
  154. newPapu();
  155. }
  156.  
  157. }
  158.  
  159. void newPapu() {
  160. int tx, ty;
  161. tx = papuX;
  162. ty = papuY;
  163.  
  164. papuX = (tx + 4) % szerokosc;
  165. papuY = (ty + 4) % dlugosc;
  166. }
  167.  
  168. void step() {
  169. for (int i = 0; i < szerokosc; i++)
  170. for (int j = 0; j < dlugosc; j++)
  171. board[i][j][0] = 0;
  172.  
  173. chodzenie();
  174. Snake tmp = *head;
  175. while (tmp.next != NULL) {
  176. board[tmp.x][tmp.y][0] = 1;
  177. tmp = *tmp.next;
  178. }
  179. board[tmp.x][tmp.y][0] = 1;
  180. }
  181.  
  182. int getPoints() {
  183. return this->points;
  184. }
  185.  
  186. void show() {
  187. for (int i = 0; i < szerokosc; i++) {
  188. for (int j = 0; j < dlugosc; j++) {
  189. if (i == papuX && j == papuY)
  190. cout << "*";
  191. else if (board[i][j][0] == 0)
  192. cout << "-";
  193. else cout << "X";
  194. }
  195. cout << endl;
  196. }
  197. }
  198.  
  199. void setDirection(int direction) {
  200. if (direction == 0 || direction == 1 || direction == 2 || direction == 4)
  201. switch (direction) {
  202. case 0:
  203. pressUp();
  204. break;
  205. case 1:
  206. pressDown();
  207. break;
  208. case 2:
  209. pressLeft();
  210. break;
  211. case 4:
  212. pressRight();
  213. break;
  214. }
  215. }
  216.  
  217. };
  218.  
  219. void testSnake() {
  220. int steps = 500;
  221. Game *g = new Game(10, 10);
  222. g->show();
  223. int a[] = {4, 2, 5, 5, 5, 1, 5, 0, 5, 3, 4, 5, 0, 5, 5, 5, 0, 0, 4, 5, 5, 5, 2, 5, 0, 5, 5, 5, 1, 5, 5, 5, 3, 4,
  224. 5, 4, 0};
  225. for (int i = 0; i < steps; i++) {
  226. usleep(500000);
  227. int r = a[i % (sizeof(a) / sizeof(*a))];
  228. system("clear");
  229. cout << "Punkty:" << g->getPoints() << endl;
  230. if (r < 4) {
  231. g->setDirection(r);
  232. }
  233. g->step();
  234. g->show();
  235. cout << flush;
  236. }
  237. }
  238.  
  239. int main() {
  240. testSnake();
  241. return 0;
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement