88 lines
1.4 KiB
C
88 lines
1.4 KiB
C
|
#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);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|