-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconway.c
224 lines (194 loc) · 7.69 KB
/
conway.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <string.h>
#include "conway.h"
#include "terminal.h"
// neighbor functions
/**
* Counts the neighbors of `c->grid[row,column]`
*/
unsigned int conway_count_neighbors(conway *restrict c, const unsigned int row, const unsigned int column);
bool neighbor_UL(conway *restrict c, const unsigned int row, const unsigned int column); // upper left
bool neighbor_U(conway *restrict c, const unsigned int row, const unsigned int column); // uppper
bool neighbor_UR(conway *restrict c, const unsigned int row, const unsigned int column); // upper right
bool neighbor_L(conway *restrict c, const unsigned int row, const unsigned int column); // left
bool neighbor_R(conway *restrict c, const unsigned int row, const unsigned int column); // right
bool neighbor_BL(conway *restrict c, const unsigned int row, const unsigned int column); // bottom left
bool neighbor_B(conway *restrict c, const unsigned int row, const unsigned int column); // bottom
bool neighbor_BR(conway *restrict c, const unsigned int row, const unsigned int column); // bottom right
void conway_set_cell(conway *restrict c, const unsigned int row, const unsigned int column, const bool alive)
{
c->grid[row][column] = alive;
}
void conway_timestep(conway *restrict c)
{
conway copy;
memcpy(©, c, sizeof(*c));
for (size_t i = 0; i < c->height; i++)
{
for (size_t j = 0; j < c->width; j++)
{
switch (conway_count_neighbors(©, i, j))
{
case 2:
c->grid[i][j] = copy.grid[i][j];
break;
case 3:
c->grid[i][j] = true;
break;
default:
c->grid[i][j] = false;
break;
}
}
}
}
void conway_print_grid(const conway * c)
{
terminal_goto(0,0);
for (unsigned i = 0; i < c->height; i++)
{
for (unsigned int j = 0; j < c->width; j++)
{
if (c->grid[i][j])
{
printf(CELL_CHARACTER);
}
else
{
printf(" ");
}
}
printf("|\n");
}
for (unsigned int j = 0; j < c->width; j++)
{
printf("_");
}
}
bool conway_place_glider(conway *restrict c, const unsigned int row, const unsigned int column)
{
conway_set_cell(c, row, column+1, true);
conway_set_cell(c, row+1, column+2, true);
conway_set_cell(c, row+2, column, true);
conway_set_cell(c, row+2, column+1, true);
conway_set_cell(c, row+2, column+2, true);
return true;
}
bool conway_place_gosper(conway *restrict c, const unsigned int row, const unsigned int column)
{
conway_set_cell(c, row+4, column, true);
conway_set_cell(c, row+5, column, true);
conway_set_cell(c, row+4, column+1, true);
conway_set_cell(c, row+5, column+1, true);
conway_set_cell(c, row+4, column+10, true);
conway_set_cell(c, row+5, column+10, true);
conway_set_cell(c, row+6, column+10, true);
conway_set_cell(c, row+3, column+11, true);
conway_set_cell(c, row+7, column+11, true);
conway_set_cell(c, row+2, column+12, true);
conway_set_cell(c, row+8, column+12, true);
conway_set_cell(c, row+2, column+13, true);
conway_set_cell(c, row+8, column+13, true);
conway_set_cell(c, row+5, column+14, true);
conway_set_cell(c, row+3, column+15, true);
conway_set_cell(c, row+7, column+15, true);
conway_set_cell(c, row+4, column+16, true);
conway_set_cell(c, row+5, column+16, true);
conway_set_cell(c, row+6, column+16, true);
conway_set_cell(c, row+5, column+17, true);
conway_set_cell(c, row+2, column+20, true);
conway_set_cell(c, row+3, column+20, true);
conway_set_cell(c, row+4, column+20, true);
conway_set_cell(c, row+2, column+21, true);
conway_set_cell(c, row+3, column+21, true);
conway_set_cell(c, row+4, column+21, true);
conway_set_cell(c, row+1, column+22, true);
conway_set_cell(c, row+5, column+22, true);
conway_set_cell(c, row+0, column+24, true);
conway_set_cell(c, row+1, column+24, true);
conway_set_cell(c, row+5, column+24, true);
conway_set_cell(c, row+6, column+24, true);
conway_set_cell(c, row+2, column+34, true);
conway_set_cell(c, row+3, column+34, true);
conway_set_cell(c, row+2, column+35, true);
conway_set_cell(c, row+3, column+35, true);
return true;
}
bool conway_place_LWSS(conway *restrict c, const unsigned int row, const unsigned int column)
{
conway_set_cell(c, row, column, true);
conway_set_cell(c, row+2, column, true);
conway_set_cell(c, row+3, column+1, true);
conway_set_cell(c, row+3, column+2, true);
conway_set_cell(c, row, column+3, true);
conway_set_cell(c, row+3, column+3, true);
conway_set_cell(c, row+1, column+4, true);
conway_set_cell(c, row+2, column+4, true);
conway_set_cell(c, row+3, column+4, true);
return true;
}
bool conway_place_Rpentomino(conway *restrict c, const unsigned int row, const unsigned int column)
{
conway_set_cell(c, row+1, column, true);
conway_set_cell(c, row, column+1, true);
conway_set_cell(c, row+1, column+1, true);
conway_set_cell(c, row+2, column+1, true);
conway_set_cell(c, row, column+2, true);
return true;
}
unsigned int conway_count_neighbors(conway *restrict c, const unsigned int row, const unsigned int column)
{
unsigned int neighbors = 0;
if(neighbor_UL(c, row, column)) neighbors++;
if(neighbor_U(c, row, column)) neighbors++;
if(neighbor_UR(c, row, column)) neighbors++;
if(neighbor_L(c, row, column)) neighbors++;
if(neighbor_R(c, row, column)) neighbors++;
if(neighbor_BL(c, row, column)) neighbors++;
if(neighbor_B(c, row, column)) neighbors++;
if(neighbor_BR(c, row, column)) neighbors++;
return neighbors;
}
bool neighbor_UL(conway *restrict c, const unsigned int row, const unsigned int column)
{
unsigned int neighbor_row = row != 0 ? row-1 : c->height - 1;
unsigned int neighbor_column = column != 0 ? column-1 : c->width - 1;
return c->grid[neighbor_row][neighbor_column];
}
bool neighbor_U(conway *restrict c, const unsigned int row, const unsigned int column)
{
unsigned int neighbor_row = row != 0 ? row-1 : c->height - 1;
return c->grid[neighbor_row][column];
}
bool neighbor_UR(conway *restrict c, const unsigned int row, const unsigned int column)
{
unsigned int neighbor_row = row != 0 ? row-1 : c->height - 1;
unsigned int neighbor_column = column != c->width - 1 ? column+1 : 0;
return c->grid[neighbor_row][neighbor_column];
}
bool neighbor_L(conway *restrict c, const unsigned int row, const unsigned int column)
{
unsigned int neighbor_column = column != 0 ? column-1 : c->width - 1;
return c->grid[row][neighbor_column];
}
bool neighbor_R(conway *restrict c, const unsigned int row, const unsigned int column)
{
unsigned int neighbor_column = column != c->width - 1 ? column+1 : 0;
return c->grid[row][neighbor_column];
}
bool neighbor_BL(conway *restrict c, const unsigned int row, const unsigned int column)
{
unsigned int neighbor_row = row != c->height - 1 ? row+1 : 0;
unsigned int neighbor_column = column != 0 ? column-1 : c->width - 1;
return c->grid[neighbor_row][neighbor_column];
}
bool neighbor_B(conway *restrict c, const unsigned int row, const unsigned int column)
{
unsigned int neighbor_row = row != c->height - 1 ? row+1 : 0;
return c->grid[neighbor_row][column];
}
bool neighbor_BR(conway *restrict c, const unsigned int row, const unsigned int column)
{
unsigned int neighbor_row = row != c->height - 1 ? row+1 : 0;
unsigned int neighbor_column = column != c->width - 1 ? column+1 : 0;
return c->grid[neighbor_row][neighbor_column];
}