first commit

This commit is contained in:
Debucquoy 2023-05-28 00:34:42 +02:00
commit b21675e7c3
Signed by: tonitch
GPG Key ID: A78D6421F083D42E
3 changed files with 92 additions and 0 deletions

5
build.sh Executable file
View File

@ -0,0 +1,5 @@
#!/bin/sh
set -xe
gcc -Wall -Wextra -o main main.c

BIN
main Executable file

Binary file not shown.

87
main.c Normal file
View File

@ -0,0 +1,87 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#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);
}
}