Advertisement
SIKER_98

4

Apr 27th, 2020
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.04 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. static void swap(int tx, int ty, Snake &tmp) {
  12. int x, y;
  13. x = tmp.x;
  14. y = tmp.y;
  15.  
  16. tmp.x = tx;
  17. tmp.y = ty;
  18. }
  19.  
  20. };
  21.  
  22. class Game {
  23. private:
  24. int board[10][10][1];
  25. int szerokosc, dlugosc;
  26. int points;
  27. int direction;
  28. Snake head;
  29.  
  30. public:
  31. Game(int szerokosc, int dlugosc) {
  32. this->szerokosc = szerokosc;
  33. this->dlugosc = dlugosc;
  34. this->points = 0;
  35. Snake *head = new Snake();
  36. head->x = 5;
  37. head->y = 5;
  38. }
  39.  
  40. void pressUp() {
  41. this->direction = 0;
  42. }
  43.  
  44. void pressDown() {
  45. this->direction = 1;
  46. }
  47.  
  48. void pressLeft() {
  49. this->direction = 2;
  50. }
  51.  
  52. void pressRight() {
  53. this->direction = 4;
  54. }
  55.  
  56.  
  57. void chodzenie() {
  58. Snake tmp = head;
  59.  
  60. switch (this->direction) {
  61. case 0:
  62. while (tmp.next != NULL) {
  63. int tx, ty;
  64. tx = tmp.x;
  65. ty = tmp.y;
  66.  
  67. tmp.y++;
  68. tmp = *tmp.next;
  69. Snake::swap(tx, ty, tmp);
  70. }
  71. tmp.y++;
  72. break;
  73. case 1:
  74. while (tmp.next != NULL) {
  75. int tx, ty;
  76. tx = tmp.x;
  77. ty = tmp.y;
  78.  
  79. tmp.y--;
  80. tmp = *tmp.next;
  81. Snake::swap(tx, ty, tmp);
  82. }
  83. tmp.y--;
  84. break;
  85. case 2:
  86. while (tmp.next != NULL) {
  87. int tx, ty;
  88. tx = tmp.x;
  89. ty = tmp.y;
  90.  
  91. tmp.x--;
  92. tmp = *tmp.next;
  93. Snake::swap(tx, ty, tmp);
  94. }
  95. tmp.x--;
  96. break;
  97. case 4:
  98. while (tmp.next != NULL) {
  99. int tx, ty;
  100. tx = tmp.x;
  101. ty = tmp.y;
  102.  
  103. tmp.x++;
  104. tmp = *tmp.next;
  105. Snake::swap(tx, ty, tmp);
  106. }
  107. tmp.x++;
  108. break;
  109. }
  110.  
  111. }
  112.  
  113. void step() {
  114. for (int i = 0; i < 10; i++)
  115. for (int j = 0; j < 10; j++)
  116. board[i][j][0] = 0;
  117.  
  118. chodzenie();
  119. Snake tmp = head;
  120. while (head.next != NULL) {
  121. board[tmp.x][tmp.y][0] = 1;
  122. tmp = *tmp.next;
  123. }
  124. board[tmp.x][tmp.y][0] = 1;
  125. }
  126.  
  127. int getPoints() {
  128. return this->points;
  129. }
  130.  
  131. void show() {
  132. for (int i = 0; i < 10; i++) {
  133. for (int j = 0; j < 10; j++) {
  134. if (board[i][j][0] == 0)
  135. cout << " ";
  136. else cout << "X";
  137. }
  138. cout << endl;
  139. }
  140. }
  141.  
  142. void setDirection(int direction) {
  143. switch (direction) {
  144. case 0:
  145. pressUp();
  146. break;
  147. case 1:
  148. pressDown();
  149. break;
  150. case 2:
  151. pressLeft();
  152. break;
  153. case 4:
  154. pressRight();
  155. break;
  156. }
  157. }
  158.  
  159. };
  160.  
  161. void testSnake() {
  162. int steps = 50;
  163. Game *g = new Game(10, 10);
  164. g->show();
  165. 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,
  166. 5, 5, 0};
  167. for (int i = 0; i < steps; i++) {
  168. usleep(500000);
  169. int r = a[i % (sizeof(a) / sizeof(*a))];
  170. system("clear");
  171. cout << "Punkty:" << g->getPoints() << endl;
  172. if (r < 4) {
  173. g->setDirection(r);
  174. }
  175. g->step();
  176. g->show();
  177. cout << flush;
  178. }
  179. }
  180.  
  181. int main() {
  182. testSnake();
  183. return 0;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement