Advertisement
YorKnEz

Untitled

Oct 4th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 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[width-1][i] = '#';
  17. }
  18.  
  19. for (int i = 0; i < height; i++) {
  20. board[i][0] = '#';
  21. board[i][height-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 width, int height) {
  42. head->x = (width/2)-1;
  43. head->y = (height/2)-1;
  44.  
  45. for (int i = 1; i <= 4; i++) {
  46. SnakePiece *piece = (SnakePiece *) malloc (sizeof(SnakePiece));
  47.  
  48. piece->x = head->x;
  49. piece->y = (head->y)+i;
  50. piece->next = head;
  51.  
  52. head = piece;
  53. }
  54. }
  55.  
  56. void show_snake (SnakePiece *head, char **board) {
  57. SnakePiece *ptr;
  58. ptr = head;
  59.  
  60. while (ptr != NULL) {
  61. printf ("o", board[ptr->y][ptr->x]);
  62.  
  63. ptr = ptr->next;
  64. }
  65. }
  66.  
  67. void generate_fruit (char **board, int width, int height) {
  68. int fruitx, fruity;
  69.  
  70. fruitx = rand() % width;
  71. fruity = rand() % height;
  72.  
  73. board[fruity][fruitx] = 'x';
  74. }
  75.  
  76. int main()
  77. {
  78. char **board;
  79.  
  80. int width, height;
  81.  
  82. SnakePiece *head;
  83.  
  84. scanf("%d", &width, &height);
  85.  
  86. board = (char **) malloc (width * sizeof(char *));
  87.  
  88. for (int i = 0; i < width; i++) {
  89. board[i] = (char *) malloc (height * sizeof(char));
  90. }
  91.  
  92. initialize_board(board, width, height);
  93.  
  94. bool fruit_eaten = 1;
  95.  
  96. while (true) {
  97. if (fruit_eaten) {
  98. generate_fruit(board, width, height);
  99.  
  100. fruit_eaten = 0;
  101. }
  102.  
  103. //initialize_snake(head, width, height);
  104.  
  105.  
  106.  
  107. //show_snake(head, board4);
  108.  
  109. show_board(board, width, height);
  110.  
  111. system ("cls");
  112. }
  113.  
  114. return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement