diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0ea42b4 --- /dev/null +++ b/Makefile @@ -0,0 +1,3 @@ +default: + clang gol.c -L raylib-5.5_linux_amd64/lib -l:libraylib.a -lm + diff --git a/gol.c b/gol.c index 39c0477..3afabdd 100644 --- a/gol.c +++ b/gol.c @@ -1,10 +1,16 @@ #include +#include "./raylib-5.5_linux_amd64/include/raylib.h" -#define SIZE 512 +#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; i < SIZE; i++){ + for(int j = 0; j < SIZE; j++){ grid[i][j] = false; } } @@ -12,7 +18,7 @@ void initialize_grid(bool grid[SIZE][SIZE]){ void mirror_grid(bool grid[SIZE][SIZE], bool mirror[SIZE][SIZE]){ for(int i = 0; i < SIZE; i++){ - for(int j = 0; i < SIZE; i++){ + for(int j = 0; j < SIZE; j++){ mirror[i][j] = grid[i][j]; } } @@ -22,20 +28,20 @@ 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; i < SIZE; i++){ + for(int j = 0; j < SIZE; j++){ int neighbours = 0; - int nx_prawo = i + 1 > SIZE ? 0 + (i + 1 - SIZE) : i + 1; + int nx_prawo = i == SIZE ? 0 : i + 1; int ny_prawo = j; - int nx_lewo = i - 1 < 0 ? SIZE - (i - 1 + SIZE) : i - 1; + int nx_lewo = i == 0 ? SIZE : i - 1; int ny_lewo = j; int nx_gora = i; - int ny_gora = j + 1 > SIZE ? 0 + (j + 1 - SIZE) : j + 1; + int ny_gora = j == 0 ? SIZE : j - 1; int nx_dol = i; - int ny_dol = j - 1 < 0 ? SIZE - (j - 1 + SIZE) : j - 1; + int ny_dol = j == SIZE ? 0 : j + 1; neighbours = mirror[nx_prawo][ny_prawo] == true ? neighbours + 1 : neighbours; neighbours = mirror[nx_lewo][ny_lewo] == true ? neighbours + 1 : neighbours; @@ -47,7 +53,7 @@ void simulate_grid(bool grid[SIZE][SIZE], bool mirror[SIZE][SIZE]){ 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 || neighbours == 3 ? 2 : neighbours > 3 ? 3 : neighbours == 3 ? 4 : 0; + int state = neighbours < 2 ? 1 : neighbours == 2 ? 2 : neighbours > 3 ? 3 : neighbours == 3 ? 4 : 0; switch (state) { @@ -71,14 +77,53 @@ void simulate_grid(bool grid[SIZE][SIZE], bool mirror[SIZE][SIZE]){ } +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); - simulate_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; }