Advertisement
Tkap1

Untitled

Nov 24th, 2023
926
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.16 KB | None | 0 0
  1.  
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5. typedef struct s_pos
  6. {
  7.     int x;
  8.     int y;
  9. } s_pos;
  10.  
  11. int main()
  12. {
  13.     #define board_size 8
  14.     int snake_count = 0;
  15.     int apple_count = 0;
  16.     s_pos snake[board_size * board_size] = {};
  17.     s_pos apples[board_size * board_size] = {};
  18.  
  19.     snake[snake_count].x = 4;
  20.     snake[snake_count].y = 4;
  21.     snake_count += 1;
  22.  
  23.     // apples[apple_count].x = rand() % board_size;
  24.     // apples[apple_count].y = rand() % board_size;
  25.     apples[apple_count].x = 6;
  26.     apples[apple_count].y = 4;
  27.     apple_count += 1;
  28.  
  29.     while(1) {
  30.  
  31.         // Display board
  32.         for(int y = 0; y < board_size; y++) {
  33.             for(int x = 0; x < board_size; x++) {
  34.                 int found_apple = 0;
  35.                 int found_snake = 0;
  36.                 for(int apple_i = 0; apple_i < apple_count; apple_i++) {
  37.                     if(apples[apple_i].x == x && apples[apple_i].y == y) {
  38.                         printf("9");
  39.                         found_apple = 1;
  40.                         break;
  41.                     }
  42.                 }
  43.                 if(found_apple) { continue; }
  44.                 for(int snake_i = 0; snake_i < snake_count; snake_i++) {
  45.                     if(snake[snake_i].x == x && snake[snake_i].y == y) {
  46.                         printf("1");
  47.                         found_snake = 1;
  48.                         break;
  49.                     }
  50.                 }
  51.                 if(found_snake) { continue; }
  52.  
  53.                 printf("0");
  54.             }
  55.             printf("\n");
  56.         }
  57.  
  58.         // Get input
  59.         char buffer[128] = {};
  60.         scanf(" %c", buffer);
  61.         char c = buffer[0];
  62.  
  63.         s_pos head = snake[0];
  64.  
  65.         // Move head
  66.         if(c == '8') {
  67.             head.y -= 1;
  68.             if(head.y < 0) { head.y = board_size - 1; }
  69.         }
  70.         else if(c == '2') {
  71.             head.y += 1;
  72.             if(head.y >= board_size) { head.y = 0; }
  73.         }
  74.         else if(c == '4') {
  75.             head.x -= 1;
  76.             if(head.x < 0) { head.x = board_size - 1; }
  77.         }
  78.         else if(c == '6') {
  79.             head.x += 1;
  80.             if(head.x >= board_size) { head.x = 0; }
  81.         }
  82.  
  83.         // Eat apple
  84.         for(int apple_i = 0; apple_i < apple_count; apple_i++) {
  85.             s_pos apple = apples[apple_i];
  86.             if(head.x == apple.x && head.y == apple.y) {
  87.                 apples[apple_i] = apples[apple_count - 1];
  88.                 apple_i -= 1;
  89.                 apple_count -= 1;
  90.                 snake_count += 1;
  91.             }
  92.         }
  93.  
  94.         // Make snake body follow head
  95.         for(int snake_i = snake_count - 1; snake_i >= 1; snake_i--) {
  96.             snake[snake_i] = snake[snake_i - 1];
  97.         }
  98.         snake[0] = head;
  99.  
  100.         // snake[0] = head;
  101.     }
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement