#include #include #include #include int width = 110; int height = 90; int cell_size = 6; int FPS = 40; char title[124]; int *current_state; int *prev_state; #define wrap(coord,max) ((coord) < 0 ? (max) + (coord) : (coord) % (max)) #define cell_at(x,y) (prev_state[wrap(y, height) * width + wrap(x, width)]) void on_create(void); void keys(void); void set_cells(char *pattern, int x, int y); int main(int arvc, char *argv){ on_create(); InitWindow(width * cell_size, height * cell_size, "Game of Life"); SetTargetFPS(FPS); while (!WindowShouldClose()){ keys(); for (int i = 0; i < height * width; i++) prev_state[i] = current_state[i]; // update and draw BeginDrawing(); ClearBackground(BLACK); for (int x = 0; x < width; x++){ for (int y = 0; y < height; y++){ int n_neighbours = cell_at(x - 1, y - 1) + cell_at(x, y - 1) + cell_at(x + 1, y - 1) + cell_at(x - 1, y) + cell_at(x + 1, y) + cell_at(x - 1, y + 1) + cell_at(x, y + 1) + cell_at(x + 1, y + 1); if (cell_at(x,y) == 1) current_state[y * width + x] = n_neighbours == 2 || n_neighbours == 3; else current_state[y * width + x] = n_neighbours == 3; if (cell_at(x,y)) DrawRectangle(x * cell_size, y * cell_size, cell_size, cell_size, WHITE); } } EndDrawing(); } CloseWindow(); return 0; } void keys(void){ sprintf(title, "Game of Life [FPS: %d]", GetFPS()); SetWindowTitle(title); if (IsKeyPressed(KEY_KP_ADD)){ FPS += 5; if (FPS >= 300) FPS = 300; SetTargetFPS(FPS); }else if (IsKeyPressed(KEY_KP_SUBTRACT) || IsKeyPressed(KEY_SLASH)){ FPS -= 5; if (FPS < 1) FPS = 1; SetTargetFPS(FPS); } if (IsKeyPressed(KEY_SPACE)){ do { BeginDrawing(); EndDrawing(); }while (!IsKeyPressed(KEY_SPACE)); } } void set_cells(char *pattern, int x, int y){ for (int i = 0; x + i < width && *pattern; i++, pattern++) current_state[width * y + x + i] = *pattern == '#'; } void on_create(void){ current_state = calloc(height * width, sizeof(*current_state)); prev_state = calloc(height * width, sizeof(*current_state)); /* Pentomino set_cells(" ## ", 80,50); set_cells(" ## ", 80,51); set_cells(" # ", 80,52); set_cells(" ## ", 200,160); set_cells(" ## ", 200,161); set_cells(" # ", 200,162); set_cells(" ## ", 300,200); set_cells(" ## ", 300,201); set_cells(" # ", 300,202); set_cells(" ## ", 310,50); set_cells(" ## ", 310,51); set_cells(" # ", 310,52);*/ /* Gosper Glider Gun set_cells("........................#............", 60, 45); set_cells("......................#.#............", 60, 46); set_cells("............##......##............##.", 60, 47); set_cells("...........#...#....##............##.", 60, 48); set_cells("##........#.....#...##...............", 60, 49); set_cells("##........#...#.##....#.#............", 60, 50); set_cells("..........#.....#.......#............", 60, 51); set_cells("...........#...#.....................", 60, 52); set_cells("............##.......................", 60, 53);*/ // Infinite Growth // set_cells("########.#####...###......#######.#####", 150, 30); // Random srand(time(0)); for (int i = 0; i < width * height; i++) current_state[i] = rand() % 100 >= 40; }