Created
August 6, 2025 17:44
-
-
Save S1M0N38/9472e6fc3c7956fc25c68e8528e726de to your computer and use it in GitHub Desktop.
Simple Game of Life
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <unistd.h> | |
#define COLS 25 | |
#define ROWS 25 | |
#define ALIVE 1 | |
#define DEAD 0 | |
void printgrid(char *grid) { | |
printf("\033[H\033[2J"); | |
for (int r = 0; r < ROWS; r++) { | |
for (int c = 0; c < COLS; c++) { | |
if (grid[r * COLS + c] == DEAD) { | |
printf(" "); | |
} else { | |
// printf("**"); | |
printf("██"); | |
} | |
} | |
printf("\n"); | |
} | |
} | |
int countneighbours(char *grid, int r, int c) { | |
int count = 0; | |
for (int i = r - 1; i < r + 2; i++) { | |
for (int j = c - 1; j < c + 2; j++) { | |
if (i >= 0 && i < ROWS && j >= 0 && j < COLS) { | |
if (grid[i * COLS + j] && !(i == r && j == c)) | |
count++; | |
} | |
} | |
} | |
return count; | |
} | |
int main(void) { | |
// 1 is alive, 0 is dead | |
char grid[COLS * ROWS] = { | |
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
}; | |
while (1) { | |
// Render grid | |
printgrid(grid); | |
// Init new grid | |
char new_grid[COLS * ROWS]; | |
for (int i = 0; i < COLS * ROWS; i++) { | |
new_grid[i] = DEAD; | |
} | |
// Create new grid | |
for (int r = 0; r < ROWS; r++) { | |
for (int c = 0; c < COLS; c++) { | |
int neighbours = countneighbours(grid, r, c); | |
char alive = grid[r * COLS + c] == ALIVE; | |
// 1. Any live cell with fewer than two live neighbours dies, as if by | |
// underpopulation. | |
if (alive && neighbours < 2) { | |
new_grid[r * COLS + c] = DEAD; | |
} | |
// 2. Any live cell with two or three live neighbours lives on to the | |
// next generation. | |
else if (alive && (neighbours == 2 | neighbours == 3)) { | |
new_grid[r * COLS + c] = ALIVE; | |
} | |
// 3. Any live cell with more than three live neighbours dies, as if | |
// by overpopulation. | |
else if (alive && neighbours > 3) { | |
new_grid[r * COLS + c] = DEAD; | |
} | |
// 4. Any dead cell with exactly three live neighbours becomes a live | |
// cell, as if by reproduction. | |
else if (!alive && neighbours == 3) { | |
new_grid[r * COLS + c] = ALIVE; | |
} | |
} | |
} | |
// Update the old grid | |
for (int i = 0; i < COLS * ROWS; i++) { | |
grid[i] = new_grid[i]; | |
} | |
usleep(50000); // 50ms delay | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment