commit b21675e7c36acd7095e1efdcf370d304ac291b17 Author: Debucquoy Date: Sun May 28 00:34:42 2023 +0200 first commit diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..4aac112 --- /dev/null +++ b/build.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +set -xe + +gcc -Wall -Wextra -o main main.c diff --git a/main b/main new file mode 100755 index 0000000..fb53280 Binary files /dev/null and b/main differ diff --git a/main.c b/main.c new file mode 100644 index 0000000..c9b2724 --- /dev/null +++ b/main.c @@ -0,0 +1,87 @@ +#include +#include +#include +#include +#include + +#define WIDTH 100 +#define HEIGHT 100 + +#define true 1 +#define false 0 + +#define emod(x, y) ((((x) % (y)) + (y)) % (y)) +typedef unsigned char bool ; + +bool grid[HEIGHT][WIDTH], step[HEIGHT][WIDTH]; + +bool randb(){ + return rand() % 2; +} + +void gen_grid(){ + for(int i = 0; i < HEIGHT; i++){ + for(int j = 0; j < HEIGHT; j++){ + grid[i][j] = randb(); + } + } +} + +void draw_grid(){ + for(int i = 0; i < HEIGHT; i++){ + for(int j = 0; j < HEIGHT; j++){ + if(grid[i][j]){ + printf("\033[107m■ "); + }else{ + printf("\033[49m "); + } + } + printf("\033[0m\n"); + } +} + +void step_grid(){ + for(int i = 0; i < HEIGHT; i++){ + for(int j = 0; j < WIDTH; j++){ + + int count = 0; + for(int di = i-1; di < i+2; di++){ + for(int dj = j-1; dj < j+2; dj++){ + if(di == i && dj == j){ + continue; + } + if(grid[emod(di, HEIGHT)][emod(dj, WIDTH)]){ + count++; + } + } + } + step[i][j] = false; + if(count == 3 || (count == 2 && grid[i][j])){ + step[i][j] = true; + } + + } + } + memcpy(grid, step, sizeof(char) * HEIGHT * WIDTH); +} + +struct timespec ts = { 0, 100 * 1000}; + +int main(void) +{ + srand(time(0)); + gen_grid(); + + while(true){ + ts.tv_nsec= 10 * 1000 * 1000; + for(int i = 0; i < HEIGHT; i++){ + printf("\n"); + } + draw_grid(); + /* getchar(); */ + step_grid(); + nanosleep(&ts, NULL); + } +} + +