Advertisement
SIKER_98

3

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