Untitled

 avatar
unknown
plain_text
a year ago
1.7 kB
2
Indexable
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

#define WIDTH 80
#define HEIGHT 25

void clear_terminal() {
    printf("\033[H\033[J");
}

void wait(float seconds) {
    usleep((int)(seconds * 1000000));
}

void init_field(int field[HEIGHT][WIDTH]) {
    for (int y = 0; y < HEIGHT; ++y) {
        for (int x = 0; x < WIDTH; ++x) {
            field[y][x] = 0;
        }
    }
}

void add_structure(int field[HEIGHT][WIDTH]) {
    int structure[5][2] = {{0,1}, {1,2}, {2,0}, {2,1}, {2,2}}; // Пример структуры (глайдер)
    int offsetX = rand() % (WIDTH - 3); // Смещение, чтобы структура умещалась в поле
    int offsetY = rand() % (HEIGHT - 3);

    for (int i = 0; i < 5; ++i) {
        int x = offsetX + structure[i][0];
        int y = offsetY + structure[i][1];
        field[y][x] = 1;
    }
}

void render(int field[HEIGHT][WIDTH]) {
    clear_terminal();
    for (int y = 0; y < HEIGHT; y++) {
        for (int x = 0; x < WIDTH; x++) {
            printf("%c", field[y][x] ? '*' : ' ');
        }
        printf("\n");
    }
}

// Функции count_neighbors и update остаются без изменений

int main() {
    srand(time(NULL)); // Инициализация генератора случайных чисел
    int field[HEIGHT][WIDTH];
    init_field(field);

    int generation = 0;
    while (1) {
        if (generation % 50 == 0) { // Каждое 50-е поколение добавляем новую структуру
            add_structure(field);
        }
        render(field);
        update(field);
        wait(0.1);
        generation++;
    }

    return 0;
}
Leave a Comment