diff --git a/a3/world.c b/a3/world.c index 644d83e..f7d54b9 100644 --- a/a3/world.c +++ b/a3/world.c @@ -1,19 +1,44 @@ -#include +#include "world.h" #include -#include "snake.h" +#include -void render(const struct state* state) { - system("clear"); +// Initializes the game world, including the snake and the placement of food +void init_world(struct state* world, int width, int height){ + world->width = width; + world->height = height; + world->sx = 1; + world->sy = 0; - // Малюємо поле - for (int y = 0; y < state->height; y++) { - for (int x = 0; x < state->width; x++) { - if (is_snake(state->snake, x, y)) { - printf("O"); - } else if (is_food_at(state, x, y)) { - printf("@"); - } else { - printf(" "); + // Initialize snake at the center of the world + world->snake = add_snake(NULL, width / 2, height / 2); + + // Place food randomly + for(int i = 0; i < FOOD_COUNT; i++){ + world->foodx[i] = rand() % (width - 2) + 1; + world->foody[i] = rand() % (height - 2) + 1; + } +} + +// Prints the current state of the game world, including snake and food +void print_world(struct state* world){ + for(int y = 0; y < world->height; y++){ + for(int x = 0; x < world->width; x++){ + if(x == 0 || y == 0 || x == world->width - 1 || y == world->height - 1){ + printf("#"); // Print walls around the game area + }else if(is_snake(world->snake, x, y)){ + printf("O"); // Print snake parts + }else{ + int food_found = 0; + for(int i = 0; i < FOOD_COUNT; i++){ + if(world->foodx[i] == x && world->foody[i] == y){ + printf("*"); // Print food + food_found = 1; + break; + } + } + if(!food_found){ + printf(" "); // Empty space + } } } printf("\n");