Advertisement
SIKER_98

6

Apr 27th, 2020
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.74 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][15][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. return true; // jest robione
  67. }
  68.  
  69. void chodzenie() {
  70. Snake tmp = *head;
  71.  
  72. switch (this->direction) {
  73. case 0:
  74. while (tmp.next != NULL) {
  75. int tx, ty;
  76. tx = tmp.x;
  77. ty = tmp.y;
  78. if (sprawdz()) {
  79. tmp.y++;
  80. tmp = *tmp.next;
  81. Snake::swap(tx, ty, &tmp);
  82. }
  83. }
  84. if (sprawdz())tmp.y++;
  85. *head = tmp;
  86. break;
  87. case 1:
  88. while (tmp.next != NULL) {
  89. int tx, ty;
  90. tx = tmp.x;
  91. ty = tmp.y;
  92. if (sprawdz()) {
  93. tmp.y--;
  94. tmp = *tmp.next;
  95. Snake::swap(tx, ty, &tmp);
  96. }
  97. }
  98. if (sprawdz())tmp.y--;
  99. *head = tmp;
  100. break;
  101. case 2:
  102. while (tmp.next != NULL) {
  103. int tx, ty;
  104. tx = tmp.x;
  105. ty = tmp.y;
  106. if (sprawdz()) {
  107. tmp.x--;
  108. tmp = *tmp.next;
  109. Snake::swap(tx, ty, &tmp);
  110. }
  111. }
  112. if (sprawdz())tmp.x--;
  113. *head = tmp;
  114. break;
  115. case 4:
  116. while (tmp.next != NULL) {
  117. int tx, ty;
  118. tx = tmp.x;
  119. ty = tmp.y;
  120. if (sprawdz()) {
  121. tmp.x++;
  122. tmp = *tmp.next;
  123. Snake::swap(tx, ty, &tmp);
  124. }
  125. }
  126. if (sprawdz())tmp.x++;
  127. *head = tmp;
  128. break;
  129. }
  130.  
  131. if (this->head->x == papuX && this->head->y == papuY) {
  132. this->points++;
  133. Snake *nowy = new Snake(papuX, papuY);
  134.  
  135. switch (direction) {
  136. case 0:
  137. nowy->y--;
  138. break;
  139. case 1:
  140. nowy->y++;
  141. break;
  142. case 2:
  143. nowy->x++;
  144. break;
  145. case 4:
  146. nowy->x--;
  147. break;
  148. }
  149. head->next = nowy;
  150. newPapu();
  151. }
  152.  
  153. }
  154.  
  155. void newPapu() {
  156. int tx, ty;
  157. tx = papuX;
  158. ty = papuY;
  159.  
  160. papuX = (tx + 4) % szerokosc;
  161. papuY = (ty + 4) % dlugosc;
  162. }
  163.  
  164. void step() {
  165. for (int i = 0; i < szerokosc; i++)
  166. for (int j = 0; j < dlugosc; j++)
  167. board[i][j][0] = 0;
  168.  
  169. chodzenie();
  170. Snake tmp = *head;
  171. while (tmp.next != NULL) {
  172. board[tmp.x][tmp.y][0] = 1;
  173. tmp = *tmp.next;
  174. }
  175. board[tmp.x][tmp.y][0] = 1;
  176. }
  177.  
  178. int getPoints() {
  179. return this->points;
  180. }
  181.  
  182. void show() {
  183. for (int i = 0; i < szerokosc; i++) {
  184. for (int j = 0; j < dlugosc; j++) {
  185. if (i == papuX && j == papuY)
  186. cout << "*";
  187. else if (board[i][j][0] == 0)
  188. cout << "-";
  189. else cout << "X";
  190. }
  191. cout << endl;
  192. }
  193. }
  194.  
  195. void setDirection(int direction) {
  196. if (direction == 0 || direction == 1 || direction == 2 || direction == 4)
  197. switch (direction) {
  198. case 0:
  199. pressUp();
  200. break;
  201. case 1:
  202. pressDown();
  203. break;
  204. case 2:
  205. pressLeft();
  206. break;
  207. case 4:
  208. pressRight();
  209. break;
  210. }
  211. }
  212.  
  213. };
  214.  
  215. void testSnake() {
  216. int steps = 50;
  217. Game *g = new Game(10, 15);
  218. g->show();
  219. int a[] = {5, 2, 5, 5, 5, 1, 5, 5, 5, 3, 5, 5, 5, 5, 5, 5, 0, 5, 5, 5, 5, 5, 2, 5, 5, 5, 5, 5, 1, 5, 5, 5, 3, 5,
  220. 5, 5, 0};
  221. for (int i = 0; i < steps; i++) {
  222. usleep(1000000);
  223. int r = a[i % (sizeof(a) / sizeof(*a))];
  224. system("clear");
  225. cout << "Punkty:" << g->getPoints() << endl;
  226. if (r < 4) {
  227. g->setDirection(r);
  228. }
  229. g->step();
  230. g->show();
  231. cout << flush;
  232. }
  233. }
  234.  
  235. int main() {
  236. testSnake();
  237. return 0;
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement