-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgol.c
149 lines (123 loc) · 3.08 KB
/
gol.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <SDL2/SDL.h>
#include <SDL2/SDL_events.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_stdinc.h>
#include <SDL2/SDL_timer.h>
#include <SDL2/SDL_video.h>
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include "./celumato.h"
typedef enum {
DED = 0,
ALIVE = 1,
} Cell;
Uint32 cell_color[2] = {
[DED] = 0x00000000,
[ALIVE] = 0xFFAABBFF,
};
typedef struct {
Cell cells[ROWS][COLS];
} Board;
void render_board(SDL_Renderer *renderer, const Board *board) {
for(int row = 0; row < ROWS; ++row) {
for(int col = 0; col < COLS; ++col) {
celumato_fill_rect(
renderer,
col * CELL_WIDTH,
row * CELL_HEIGHT,
CELL_WIDTH,
CELL_HEIGHT,
cell_color[board->cells[row][col]]);
}
}
}
void random_board(Board *board) {
for(int row = 0; row < ROWS; ++row) {
for(int col = 0; col < COLS; ++col) {
board->cells[row][col] = rand() % 2;
}
}
}
int board_nbor(const Board *board, int row0, int col0){
int result = 0;
for(int drow = -1; drow <= 1; ++drow) {
for(int dcol = -1; dcol <= 1; ++dcol) {
if(drow != 0 || dcol != 0) {
const int row = mod(row0 + drow, ROWS);
const int col = mod(col0 + dcol, COLS);
if(board -> cells[row][col] == ALIVE) {
result += 1;
}
}
}
}
return result;
}
void next_board(const Board *prev, Board *next) {
for(int row = 0; row < ROWS; ++row) {
for(int col = 0; col < COLS; ++col) {
const int nbors = board_nbor(prev, row, col);
switch (prev->cells[row][col]) {
case DED: {
next->cells[row][col] = nbors == 3 ? ALIVE : DED;
}
break;
case ALIVE: {
next->cells[row][col] = nbors == 2 || nbors == 3 ? ALIVE : DED;
}
break;
default:
assert(false && " next_board: unreachable");
}
}
}
}
void put_glider_at(Board *board, int row0, int col0) {
Cell glider[3][3] = {
{ DED, ALIVE, DED },
{ DED, DED, ALIVE },
{ ALIVE, ALIVE, ALIVE }
};
for(int drow = 0; drow < 3; ++drow) {
for(int dcol = 0; dcol < 3; ++dcol) {
const int row = mod(row0 + drow, ROWS);
const int col = mod(col0 + dcol, COLS);
board -> cells[row][col] = glider[drow][dcol];
}
}
}
Board board[2] = {0};
int fg = 0;
int main() {
scc(SDL_Init(SDL_INIT_VIDEO));
SDL_Window *window = celumato_create_window();
SDL_Renderer *renderer = celumato_create_renderer(window);
bool quit = false;
//random_board(&board[fg]);
//put_glider_at(&board[fg], 0, 0);
put_glider_at(&board[fg], 4, 4);
while(!quit) {
SDL_Event event;
while(SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT: {
quit = true;
}
break;
}
}
scc(SDL_SetRenderDrawColor(
renderer,
HEX_COLOR_UNPACK(BACKGROUND_COLOR)));
scc(SDL_RenderClear(renderer));
render_board(renderer, &board[fg]);
const int bg = 1 - fg;
next_board(&board[fg], &board[bg]);
fg = bg;
SDL_RenderPresent(renderer);
SDL_Delay(150);
}
SDL_Quit();
return 0;
}