YorKnEz

Untitled

Oct 7th, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4.  
  5. #include <cstdlib>
  6.  
  7. typedef struct SnakePiece {
  8. int x;
  9. int y;
  10. SnakePiece *next;
  11. } SnakePiece;
  12.  
  13. void initialize_board (char **board, int width, int height) {
  14. for (int i = 0; i < width; i++) {
  15. board[0][i] = '#';
  16. board[height-1][i] = '#';
  17. }
  18.  
  19. for (int i = 0; i < height; i++) {
  20. board[i][0] = '#';
  21. board[i][width-1] = '#';
  22. }
  23.  
  24. for (int i = 1; i < height-1; i++) {
  25. for (int j = 1; j < width-1; j++) {
  26. board[i][j] = ' ';
  27. }
  28. }
  29. }
  30.  
  31. void show_board (char **board, int width, int height) {
  32. for (int i = 0; i < height; i++) {
  33. for (int j = 0; j < width; j++) {
  34. printf ("%c", board[i][j]);
  35. }
  36.  
  37. printf ("\n");
  38. }
  39. }
  40.  
  41. void initialize_snake (SnakePiece **head, int &len, int width, int height) {
  42. SnakePiece *new_piece = (SnakePiece *) malloc (sizeof(SnakePiece));
  43.  
  44. //for (int i = 0; i < 3; i++) {
  45.  
  46. new_piece->x = (width/2)-1;
  47. new_piece->y = (height/2)-1;
  48. new_piece->next = *head;
  49.  
  50. *head = new_piece;
  51.  
  52. len++;
  53.  
  54. //}
  55. }
  56.  
  57. void show_snake (SnakePiece *head, char **board) {
  58.  
  59. SnakePiece *ptr;
  60.  
  61. ptr = head;
  62.  
  63. while (ptr != NULL) {
  64. board[ptr->y][ptr->x] = 'o';
  65.  
  66. ptr = ptr->next;
  67. }
  68. }
  69.  
  70. void update_snake (SnakePiece *head, char **board, int x, int y) {
  71. SnakePiece *ptr;
  72.  
  73. ptr = head;
  74.  
  75. board[ptr->y][ptr->x] = ' ';
  76.  
  77. ptr = ptr->next;
  78.  
  79. while (ptr != NULL) {
  80. ptr->x += x;
  81. ptr->y += y;
  82.  
  83. ptr = ptr->next;
  84. }
  85. }
  86.  
  87. void move_snake (SnakePiece *head, char **board, char c, int width, int height) {
  88. if (c == 'a' && head->x > 1) {
  89. head->x += -1;
  90.  
  91. update_snake(head, board, -1, 0);
  92. }
  93. }
  94.  
  95. void generate_fruit (char **board, int width, int height) {
  96. int fruitx, fruity;
  97.  
  98. fruitx = rand() % width;
  99. fruity = rand() % height;
  100.  
  101. board[fruity][fruitx] = 'x';
  102. }
  103.  
  104. int main()
  105. {
  106. char **board;
  107.  
  108. int width, height;
  109.  
  110. SnakePiece *head = NULL;
  111.  
  112. scanf("%d %d", &height, &width);
  113.  
  114. board = (char **) malloc (height * sizeof(char *));
  115.  
  116. for (int i = 0; i < height; i++) {
  117. board[i] = (char *) malloc (width * sizeof(char));
  118. }
  119.  
  120. initialize_board(board, width, height);
  121.  
  122. int len = 0;
  123.  
  124. initialize_snake(&head, len, width, height);
  125.  
  126. bool fruit_eaten = 1;
  127.  
  128. char c;
  129.  
  130. while (true) {
  131. if (fruit_eaten) {
  132. generate_fruit(board, width, height);
  133.  
  134. fruit_eaten = 0;
  135. }
  136.  
  137. if(kbhit()) {
  138. c = getch();
  139.  
  140. move_snake (head, board, c, width, height);
  141. }
  142.  
  143. show_snake(head, board);
  144.  
  145. show_board(board, width, height);
  146.  
  147. system ("cls");
  148. }
  149.  
  150. return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment