Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #include <stdio.h>
  4. #include <cstdlib>
  5. #include <conio.h>
  6.  
  7. using namespace std;
  8.  
  9. char **board;
  10.  
  11. int width = 32, height = 16;
  12.  
  13. typedef struct SnakePiece {
  14. int x;
  15. int y;
  16. SnakePiece *next;
  17. } SnakePiece;
  18.  
  19. SnakePiece *head = NULL;
  20.  
  21. int headx, heady,
  22. tailx, taily,
  23. len;
  24.  
  25. int x_increment, y_increment;
  26.  
  27. void initialize_board () {
  28. //printf ("Height: ");
  29. //scanf ("%d", &height);
  30.  
  31. //printf ("Width: ");
  32. //scanf("%d", &width);
  33.  
  34. board = (char **) malloc (sizeof(char *) * height);
  35.  
  36. for (int i = 0; i < height; i++) {
  37. board[i] = (char *) malloc (sizeof(char) * width);
  38. }
  39.  
  40. for (int i = 0; i < height; i++) {
  41. for (int j = 0; j < width; j++) {
  42. board[i][j] = ' ';
  43. }
  44. }
  45.  
  46. for (int i = 0; i < width; i++) {
  47. board[height-1][i] = '#';
  48. board[0][i] = '#';
  49. }
  50.  
  51. for (int i = 0; i < height; i++) {
  52. board[i][width-1] = '#';
  53. board[i][0] = '#';
  54. }
  55. }
  56.  
  57. void show_board () {
  58. for (int i = 0; i < height; i++) {
  59. for (int j = 0; j < width; j++) {
  60. printf ("%c", board[i][j]);
  61. }
  62.  
  63. printf ("\n");
  64. }
  65. }
  66.  
  67. void add_piece (int x_data, int y_data) {
  68. SnakePiece *new_piece = (SnakePiece *) malloc (sizeof(SnakePiece));
  69.  
  70. new_piece->x = x_data;
  71. new_piece->y = y_data;
  72.  
  73. tailx = x_data;
  74. taily = y_data;
  75.  
  76. new_piece->next = head;
  77. head = new_piece;
  78.  
  79. len++;
  80. }
  81.  
  82. void initialize_snake () {
  83. headx = tailx = height/2-1;
  84. heady = taily = width/2-1;
  85.  
  86. add_piece (headx, heady);
  87.  
  88. for (int i = 0; i < 3; i++) {
  89. add_piece (tailx+1, taily);
  90. }
  91. }
  92.  
  93. void move_snake () {
  94.  
  95. SnakePiece *ptr;
  96.  
  97. ptr = head;
  98.  
  99. for (int i = 0; i < len; i++) {
  100. board[ptr->x][ptr->y] = ' ';
  101.  
  102. ptr = ptr->next;
  103. }
  104.  
  105. ptr = head;
  106.  
  107. int old_x, old_y;
  108.  
  109. for (int i = 0; i < len; i++) {
  110. if (i == len-1) {
  111. old_x = ptr->x;
  112. old_y = ptr->y;
  113.  
  114. if (x_increment == -1 && (headx + x_increment) == 0) {
  115. ptr->x = height-2;
  116.  
  117. headx = ptr->x;
  118. } else if (x_increment == 1 && (headx + x_increment) == height-1) {
  119. ptr->x = 1;
  120.  
  121. headx = ptr->x;
  122. } else if (y_increment == -1 && (heady + y_increment) == 0) {
  123. ptr->y = width-2;
  124.  
  125. heady = ptr->y;
  126. } else if (y_increment == 1 && (heady + y_increment) == width-1) {
  127. ptr->y = 1;
  128.  
  129. heady = ptr->y;
  130. } else {
  131. ptr->x += x_increment;
  132. ptr->y += y_increment;
  133.  
  134. headx = ptr->x;
  135. heady = ptr->y;
  136.  
  137. break;
  138. }
  139. }
  140.  
  141. ptr = ptr->next;
  142. }
  143.  
  144. ptr = head;
  145.  
  146. int old_x2, old_y2;
  147.  
  148. for (int i = 0; i < len-1; i++) {
  149. for (int j = 0; j < (len-1)-i; j++) {
  150. if (j == (len-2)-i) {
  151. old_x2 = ptr->x;
  152. old_y2 = ptr->y;
  153.  
  154. ptr->x = old_x;
  155. ptr->y = old_y;
  156.  
  157. old_x = old_x2;
  158. old_y = old_y2;
  159. }
  160.  
  161. ptr = ptr->next;
  162. }
  163.  
  164. ptr = head;
  165. }
  166.  
  167. tailx = ptr->x;
  168. taily = ptr->y;
  169. }
  170.  
  171. void convert_move (char c) {
  172. if (c == 'w') {
  173. x_increment = -1;
  174. y_increment = 0;
  175. } else if (c == 's') {
  176. x_increment = 1;
  177. y_increment = 0;
  178. } else if (c == 'a') {
  179. x_increment = 0;
  180. y_increment = -1;
  181. } else if (c == 'd') {
  182. x_increment = 0;
  183. y_increment = 1;
  184. }
  185. }
  186.  
  187. void show_coords() {
  188. SnakePiece *ptr;
  189.  
  190. ptr = head;
  191.  
  192. while (ptr) {
  193. printf ("%d %d\n", ptr->x, ptr->y);
  194.  
  195. ptr = ptr->next;
  196. }
  197.  
  198. /*for (int i = 0; i < len; i++) {
  199. for (int j = 0; j < len-i; j++) {
  200. if (j == (len-1)-i) {
  201. printf ("%d %d\n", ptr->x, ptr->y);
  202. }
  203.  
  204. ptr = ptr->next;
  205. }
  206.  
  207. ptr = head;
  208. }*/
  209.  
  210. printf ("\n");
  211. }
  212.  
  213. void draw_snake () {
  214. SnakePiece *ptr;
  215.  
  216. ptr = head;
  217.  
  218. int x, y;
  219.  
  220. for (int i = 0; i < len; i++) {
  221. x = ptr->x;
  222. y = ptr->y;
  223.  
  224. board[x][y] = 'o';
  225.  
  226. ptr = ptr->next;
  227. }
  228.  
  229. board[headx][heady] = '@';
  230. }
  231.  
  232. int main()
  233. {
  234. initialize_board();
  235.  
  236. initialize_snake();
  237.  
  238. char c = 'w';
  239.  
  240. convert_move(c);
  241.  
  242. while (true) {
  243. if (_kbhit()) {
  244. c = _getch();
  245.  
  246. convert_move(c);
  247. }
  248.  
  249. move_snake();
  250.  
  251. draw_snake();
  252.  
  253. show_board();
  254.  
  255. system("cls");
  256. }
  257.  
  258. return 0;
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement