conways_gol/gol.c
2024-12-08 17:21:17 +01:00

135 lines
3.3 KiB
C

#include <stdbool.h>
#ifdef WIN
#include "./raylib-5.5_win64_mingw-w64/include/raylib.h"
#else
#include "./raylib-5.5_linux_amd64/include/raylib.h"
#endif
#define SIZE 100
#define WIDTH 1000
#define HEIGHT 1000
#define TITLE "Conway's Game of Life"
#define FPS 15
void initialize_grid(bool grid[SIZE][SIZE]){
for(int i = 0; i < SIZE; i++){
for(int j = 0; j < SIZE; j++){
grid[i][j] = false;
}
}
}
void mirror_grid(bool grid[SIZE][SIZE], bool mirror[SIZE][SIZE]){
for(int i = 0; i < SIZE; i++){
for(int j = 0; j < SIZE; j++){
mirror[i][j] = grid[i][j];
}
}
}
void simulate_grid(bool grid[SIZE][SIZE], bool mirror[SIZE][SIZE]){
mirror_grid(grid, mirror);
for(int i = 0; i < SIZE; i++){
for(int j = 0; j < SIZE; j++){
int neighbours = 0;
int nx_prawo = i == SIZE-1 ? 0 : i + 1;
int ny_prawo = j;
int nx_lewo = i == 0 ? SIZE-1 : i - 1;
int ny_lewo = j;
int nx_gora = i;
int ny_gora = j == 0 ? SIZE-1 : j - 1;
int nx_dol = i;
int ny_dol = j == SIZE-1 ? 0 : j + 1;
neighbours = mirror[nx_prawo][ny_prawo] == true ? neighbours + 1 : neighbours;
neighbours = mirror[nx_lewo][ny_lewo] == true ? neighbours + 1 : neighbours;
neighbours = mirror[nx_gora][ny_gora] == true ? neighbours + 1 : neighbours;
neighbours = mirror[nx_dol][ny_dol] == true ? neighbours + 1 : neighbours;
neighbours = mirror[nx_prawo][ny_gora] == true ? neighbours + 1 : neighbours;
neighbours = mirror[nx_prawo][ny_dol] == true ? neighbours + 1 : neighbours;
neighbours = mirror[nx_lewo][ny_gora] == true ? neighbours + 1 : neighbours;
neighbours = mirror[nx_lewo][ny_dol] == true ? neighbours + 1 : neighbours;
int state = neighbours < 2 ? 1 : neighbours == 2 ? 2 : neighbours > 3 ? 3 : neighbours == 3 ? 4 : 0;
switch (state)
{
case 1:
grid[i][j] = false;
break;
case 2:
grid[i][j] = grid[i][j];
break;
case 3:
grid[i][j] = false;
break;
case 4:
grid[i][j] = true;
break;
default:
break;
}
}
}
}
void draw_grid(bool grid[SIZE][SIZE], int cellw, int cellh){
BeginDrawing();
ClearBackground((Color){ 0, 24, 24, 255 });
for(int i = 0; i < SIZE; i++){
for(int j = 0; j < SIZE; j++){
if(grid[i][j]){
DrawRectangle(cellw*i, cellh*j, cellw, cellh, WHITE);
}
}
}
EndDrawing();
}
int main()
{
bool grid[SIZE][SIZE];
bool mirror[SIZE][SIZE];
initialize_grid(grid);
mirror_grid(grid, mirror);
grid[SIZE/2][SIZE/2] = true; //drawing famous glider
grid[(SIZE/2)+1][(SIZE/2)+1] = true;
grid[(SIZE/2)-1][(SIZE/2)+2] = true;
grid[(SIZE/2)+0][(SIZE/2)+2] = true;
grid[(SIZE/2)+1][(SIZE/2)+2] = true;
//grid[(SIZE/2)][(SIZE/2)] = true; //funny pattern
//grid[(SIZE/2)+1][(SIZE/2)] = true;
//grid[(SIZE/2)+1][(SIZE/2)-1] = true;
//grid[(SIZE/2)+1][(SIZE/2)-2] = true;
//grid[(SIZE/2)-1][(SIZE/2)] = true;
//grid[(SIZE/2)-1][(SIZE/2)-1] = true;
//grid[(SIZE/2)-1][(SIZE/2)-2] = true;
InitWindow(WIDTH, HEIGHT, TITLE);
SetTargetFPS(FPS);
int cellw = WIDTH/SIZE;
int cellh = HEIGHT/SIZE;
while(!WindowShouldClose()){
simulate_grid(grid, mirror);
draw_grid(grid, cellw, cellh);
}
return 0;
}