#include #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, bool gamestate){ 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); } } } if(gamestate == false){ DrawRectangle((GetMouseX()/cellw)*cellw, (GetMouseY()/cellh)*cellh, cellw, cellh, WHITE); DrawText("LMB: Draw, RMB: Erase, C: Clear the screen, Space: Start/Stop the game, ESC: Exit", WIDTH*0.05, HEIGHT*0.9, 20, WHITE); } EndDrawing(); } int main() { bool gamestate = false; bool grid[SIZE][SIZE]; bool mirror[SIZE][SIZE]; initialize_grid(grid); mirror_grid(grid, mirror); InitWindow(WIDTH, HEIGHT, TITLE); SetTargetFPS(FPS); int cellw = WIDTH/SIZE; int cellh = HEIGHT/SIZE; while(!WindowShouldClose()){ if(gamestate == true){ simulate_grid(grid, mirror); draw_grid(grid, cellw, cellh, gamestate); } else{ if(IsMouseButtonDown(0)){ grid[GetMouseX()/10][GetMouseY()/10] = true; } if(IsMouseButtonDown(1)){ grid[GetMouseX()/10][GetMouseY()/10] = false; } if(IsKeyPressed(KEY_C)){ initialize_grid(grid); } draw_grid(grid, cellw, cellh, gamestate); mirror_grid(grid, mirror); } if(IsKeyPressed(KEY_SPACE)){ gamestate = gamestate == true ? false : true; } if(IsKeyPressed(KEY_ESCAPE)){ CloseWindow(); } } return 0; }