YorKnEz

Untitled

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