-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlifelib.c
287 lines (264 loc) · 5.75 KB
/
lifelib.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"lifelib.h"
int *gol_cells_needed_to_live;
int *gol_cells_needed_to_spawn;
cellgrid *create_cellgrid(int max_x, int max_y)
{
int x,columns,lines,gridsize;
cellgrid *newgrid;
newgrid=malloc(sizeof(cellgrid));
newgrid->max_x=max_x;
newgrid->max_y=max_y;
newgrid->iteration=0;
newgrid->live_cells=0;
newgrid->prev=NULL;
//build grid for cells
columns=max_x+1;
lines=(max_y/8)+1;
gridsize=columns*lines;
newgrid->grid=malloc(sizeof(char *)*columns);
newgrid->grid[0]=malloc(gridsize);
memset(newgrid->grid[0],0,gridsize);
for(x=1;x<=max_x;x++){
newgrid->grid[x]=newgrid->grid[x-1]+lines;
}
return newgrid;
}//end create_cellgrid
void free_cellgrid(cellgrid *oldgrid)
{
int x;
if(oldgrid==NULL) return;
free_cellgrid(oldgrid->prev);
free(oldgrid->grid[0]);
free(oldgrid->grid);
free(oldgrid);
}//end free_cellgrid
void set_cell(cellgrid *cgrid,int xloc,int yloc,int value)
{
static char ybitflag[]={128,64,32,16,8,4,2,1};
char valuetoset;
int ybyte,ybit;
if(get_cell(cgrid,xloc,yloc)==value)
return;
//set up locations to wrap around if off the grid
if(xloc>cgrid->max_x){
xloc=xloc-(cgrid->max_x+1);
}
if(yloc>cgrid->max_y){
yloc=yloc-(cgrid->max_y+1);
}
if(xloc<0){
xloc+=(cgrid->max_x+1);
}
if(yloc<0){
yloc+=(cgrid->max_y+1);
}
ybyte=yloc/8;
ybit=yloc%8;
//printf("byte:%d bit:%d\n",ybyte,ybit);
if(value==TRUE){
valuetoset=ybitflag[ybit];
cgrid->live_cells++;
}
else{
valuetoset=0;
cgrid->live_cells--;
}
cgrid->grid[xloc][ybyte]|=valuetoset;
}//set_cell
int get_cell(cellgrid *cgrid,int xloc,int yloc)
{
static char ybitflag[]={128,64,32,16,8,4,2,1};
char flag;
unsigned char value;
int ybyte,ybit;
//set up locations to wrap around if off the grid
if(xloc>cgrid->max_x){
xloc=xloc-(cgrid->max_x+1);
}
if(yloc>cgrid->max_y){
yloc=yloc-(cgrid->max_y+1);
}
if(xloc<0){
xloc+=(cgrid->max_x+1);
}
if(yloc<0){
yloc+=(cgrid->max_y+1);
}
ybyte=yloc/8;
ybit=yloc%8;
flag=ybitflag[ybit];
value=cgrid->grid[xloc][ybyte]&flag;
if(value>0){
return TRUE;
}
else{
return FALSE;
}
}//end get_cell
int count_live_cell_neighbors(cellgrid *cgrid, int xloc, int yloc)
{
int x,y;
int count=0;
for(x=xloc-1;x<=xloc+1;x++){
for(y=yloc-1;y<=yloc+1;y++){
if(x==xloc&&y==yloc) continue;
if(get_cell(cgrid,x,y)==TRUE){
count++;
}
}//end for (y increment)
}//end for (x increment)
return count;
}//end count_live_cell_neighbors
void gol_set_rules(int *cellsneededtolive, int *cellsneededtospawn)
{
gol_cells_needed_to_live=cellsneededtolive;
gol_cells_needed_to_spawn=cellsneededtospawn;
}
void gol_set_default_rules()
{
int live[]={2,3,-1};
int spawn[]={3,-1};
int *plive, *pspawn;
plive=malloc(sizeof(int)*3);
pspawn=malloc(sizeof(int)*2);
memcpy(plive,live,sizeof(int)*3);
memcpy(pspawn,spawn,sizeof(int)*2);
gol_set_rules(plive,pspawn);
}
void gol_do_iteration(cellgrid **cgrid)
{
int x,y,i;
int max_x,max_y;
int neighbors;
int value;
cellgrid *oldgrid;
cellgrid *newgrid;
max_x=(*cgrid)->max_x;
max_y=(*cgrid)->max_y;
oldgrid=(*cgrid);
newgrid=create_cellgrid(max_x,max_y);
for(x=0;x<=max_x;x++)
{
for(y=0;y<=max_y;y++)
{
neighbors=count_live_cell_neighbors(oldgrid,x,y);
value=get_cell(oldgrid,x,y);
if(value==TRUE){
for(i=0;gol_cells_needed_to_live[i]!=-1;i++)
{
if(neighbors==gol_cells_needed_to_live[i]){
set_cell(newgrid,x,y,TRUE);
break;
}
}}//end if
else{
for(i=0;gol_cells_needed_to_spawn[i]>0;i++){
if(neighbors==gol_cells_needed_to_spawn[i]){
//printf("spawning %d,%d : %d\n",x,y,neighbors);
set_cell(newgrid,x,y,TRUE);
break;
}
}//end for
}//end else
}//end for (y increment)
}//end for (x increment)
newgrid->iteration=++(oldgrid->iteration);
(*cgrid)=newgrid;
newgrid->prev=oldgrid;
//delete 7th previous iteration
x=0;
while(newgrid->prev!=NULL)
{
x++;
//printf("%d\n",x);
if(x==7){
if(newgrid->prev->prev!=NULL)
{
free_cellgrid(newgrid->prev->prev);
}
free_cellgrid(newgrid->prev);
newgrid->prev=NULL;
break;
}
newgrid=newgrid->prev;
}//end while
}
void print_cellgrid(cellgrid *cgrid)
{
int x,y;
static char dispchars[]={'*','O'};
for(y=0;y<=cgrid->max_y;y++){
for(x=0;x<=cgrid->max_x;x++){
putchar(dispchars[get_cell(cgrid,x,y)]);
}
putchar('|');
putchar('\n');
}
}
void generate_random_cells(cellgrid *cgrid)
{
int x,y;
srand(time(NULL));
for(x=0;x<=cgrid->max_x;x++){
for(y=0;y<=cgrid->max_y;y++){
if(rand()<RAND_MAX/3){
set_cell(cgrid,x,y,TRUE);
}
}
}
}
void generate_pulsar(cellgrid *cgrid)
{
draw_blinker(cgrid,2,0,TRUE);
draw_blinker(cgrid,0,2,FALSE);
draw_blinker(cgrid,5,2,FALSE);
draw_blinker(cgrid,2,5,TRUE);
//draw_blinker(cgrid,6,2,FALSE);
//draw_blinker(cgrid,2,6,TRUE);
draw_blinker(cgrid,0,8,FALSE);
draw_blinker(cgrid,8,0,TRUE);
draw_blinker(cgrid,7,2,FALSE);
draw_blinker(cgrid,2,7,TRUE);
draw_blinker(cgrid,8,5,TRUE);
draw_blinker(cgrid,5,8,FALSE);
draw_blinker(cgrid,2,12,TRUE);
draw_blinker(cgrid,12,2,FALSE);
draw_blinker(cgrid,7,8,FALSE);
draw_blinker(cgrid,8,7,TRUE);
draw_blinker(cgrid,8,12,TRUE);
draw_blinker(cgrid,12,8,FALSE);
}
void draw_blinker(cellgrid *cgrid,int x,int y,int horizontal)
{
int n;
for(n=0;n<3;n++)
{
set_cell(cgrid,x,y,TRUE);
if(horizontal){
x++;
}else{
y++;
}
}
}
int gol_is_repeating(cellgrid *cgrid)
{
cellgrid *tmpgrid;
int lines,columns,gridsize;
columns=cgrid->max_x+1;
lines=(cgrid->max_y/8)+1;
gridsize=columns*lines;
tmpgrid=cgrid->prev;
int x=0;
for(;tmpgrid!=NULL;tmpgrid=tmpgrid->prev){
x++;
//printf("%d\n",x);
if(memcmp(cgrid->grid[0],tmpgrid->grid[0],gridsize)==0){
return TRUE;
}
}
return FALSE;
}//end gol_is_repeating