-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconway.c
417 lines (337 loc) · 8.09 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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/**************************************************
* An Implementation of Conway's Game of Life. *
*************************************************/
/*
* David Mayes
* 150012384
*
* Gavin Henderson
* 150010848
*
* AC21009
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "conway.h"
#include "cell.h"
/*
* Creates a grid of cells,
*
* (uses rows of CellGroups)
* Creates 'height' number of cellGroups, with 'width' number of cells in them
* The groups are linked together.
*/
CellGrid* createCellGrid(int grid_height, int grid_width, int newValues[grid_height][grid_width])
{
//allocate the new CellGrid in memory
CellGrid* newCellGrid;
newCellGrid = calloc(1, sizeof(CellGrid));
//set the sizes
newCellGrid->width = grid_width;
newCellGrid->height = 0; //still zero while no groups are added
//add the cellGroups to the grid. Loops until the new grid is filled
for( int i = 0; i < grid_height; i++){
//create an array with the values to put into the current grid row
int cell_group_values[grid_width];
//zero all the values
for(int j = 0; j < grid_width; j++){
cell_group_values[j] = 0;
}
for(int j = 0; j < grid_width; j++){
cell_group_values[j] = newValues[newCellGrid->height][j];
}
//create a cellGroup
CellGroup* newCellGroup = createCellGroup(cell_group_values, grid_width);
//insert it into the grid
insertCellGroup(newCellGrid, newCellGroup);
}
return newCellGrid;
}
/*
* Frees the memory used by the grid.
*
* Uses deleteCellGroup()
*/
int deleteCellGrid(CellGrid* cell_grid)
{
//check that the pointers aren't NULL
assert(cell_grid != NULL);
//if there is nothing in the grid delete it
if(cell_grid->height == 0 ){
memset(cell_grid, 0, sizeof(CellGrid));
free(cell_grid);
if(cell_grid != NULL){
return 0;
} else {
return 1;
}
//otherwise loop through all the cellgroups and delete them
} else {
CellGroup* next = cell_grid->head;
CellGroup* current = cell_grid->head;
while(cell_grid->height != 0){
current = next;
//move to the next group
if(cell_grid->height > 1){
next = next->next;
}
deleteCellGroup(current);
(cell_grid->height)--;
}
//deletes the cell grid
memset(cell_grid, 0, sizeof(CellGrid));
free(cell_grid);
if(cell_grid != NULL){
return 0;
} else {
return 1;
}
}
}
/*
* Inserts the next CellGroup
*
* Connects all of the cells together vertically
*/
void insertCellGroup(CellGrid* cell_grid, CellGroup* cell_group)
{
//check that the pointers aren't NULL
assert(cell_grid != NULL);
assert(cell_group != NULL);
//if the cell grid is empty
if( cell_grid->height == 0 ){
//make cell_group the head
cell_grid->head = cell_group;
} else {
//start at the head
CellGroup* tmpGroup = cell_grid->head;
//loop until the end of the group
for( int i = 0; i < (cell_grid->height - 1); i++){
//move to the next group
tmpGroup = tmpGroup->next;
}
//insert the new group
tmpGroup->next = cell_group;
cell_group->prev = tmpGroup;
}
//make the list loop round
cell_group->next = cell_grid->head;
(cell_grid->head)->prev = cell_group;
//increase the height
(cell_grid->height)++;
/* Connect the cells vertically */
//start at the head
Cell* cell_below = cell_group->head;
Cell* cell_above = (cell_group->prev)->head;
//connect it to the head group so it loops round
Cell* cell_head = (cell_grid->head)->head;
//loop through the cells
for(int i = 0; i < (cell_grid->width); i++){
//point the cells to each other
cell_below->up = cell_above;
cell_above->down = cell_below;
//point this group to the head
cell_head->up = cell_below;
cell_below->down = cell_head;
//move all the cells along
cell_below = cell_below->right;
cell_above = cell_above->right;
cell_head = cell_head->right;
}
}
/*
* Creates the next generation in Conway's Game of Life
*
* Generates information from the current generation,
* deletes it, then creates a new generation using
* createCellGrid().
*/
CellGrid* nextConway(CellGrid* cell_grid)
{
//check that the pointer is not NULL
assert(cell_grid != NULL);
//create template for the next generation
int values[cell_grid->height][cell_grid->width];
//zero all the values
for(int i = 0; i < (cell_grid->height); i++){
for(int j = 0; j < (cell_grid->width); j++){
//zero current
values[i][j] = 0;
}
}
//temporary cell used to loop
Cell* tmpCell = (cell_grid->head)->head;
//loop through the cells vertically
for(int i = 0; i < (cell_grid->height); i++){
//loop through the cells horizontally
for(int j = 0; j < (cell_grid->width); j++){
/* Calculate the number of living cells around the current cell */
int live_cells = 0;
//cell above
if( (tmpCell->up)->data == 1 ){
live_cells++;
}
//cell below
if( (tmpCell->down)->data == 1 ){
live_cells++;
}
//cell right
if( (tmpCell->right)->data == 1 ){
live_cells++;
}
//cell left
if( (tmpCell->left)->data == 1 ){
live_cells++;
}
//cell up-left
if( ((tmpCell->up)->left)->data == 1 ){
live_cells++;
}
//cell up-right
if( ((tmpCell->up)->right)->data == 1 ){
live_cells++;
}
//cell down-right
if( ((tmpCell->down)->right)->data == 1 ){
live_cells++;
}
//cell down-left
if( ((tmpCell->down)->left)->data == 1 ){
live_cells++;
}
/* Set the next generation's version of this cell */
//if the current cell is alive
if( tmpCell->data == 1 ){
//if it has two/three neighbours
if( ( live_cells == 2 ) || ( live_cells == 3 ) ){
//the next gen cell is alive
values[i][j] = 1;
} else {
//the next gen cell is dead
values[i][j] = 0;
}
//if the current cell is dead
} else {
//if it has exactly three neighbours
if( live_cells == 3 ){
//the next gen cell is alive
values[i][j] = 1;
} else {
//the next gen cell is dead
values[i][j] = 0;
}
}
//move to the next cell
tmpCell = tmpCell->right;
}
//move to the next row
tmpCell = tmpCell->down; //move down to the next row
}
//save the sizes before the old cellGrid is deleted
int grid_height = cell_grid->height;
int grid_width = cell_grid->width;
//delete the cellgrid
deleteCellGrid(cell_grid);
//return the new CellGrid
return createCellGrid(grid_height, grid_width, values);
}
/*
Prints the current generation of the conway
*/
void printGrid(CellGrid* myConway)
{
//Set up cell variables
Cell* currentHead = myConway->head->head;
Cell* currentCell = currentHead;
//Loop downwards
for(int i=0; i<myConway->height; i++)
{
//Loop sideways
for(int j=0; j<myConway->width; j++)
{
//Print values
if(currentCell->data == 0)
{
printf(".");
}
else
{
printf("1");
}
//Iterate sideways
currentCell = currentCell->right;
}
printf("\n");
//Iterate downwards
currentCell = currentHead->down;
currentHead = currentHead->down;
}
}
/*
Gets initial conway from the user
*/
CellGrid* getInitialGrid()
{
//Loops to get a valid length of row
bool valid=false;
int length;
while(!valid)
{
printf("Please enter the length of the rows\n");
scanf("%d",&length);
if(length>3)
{
valid=true;
}
else
{
printf("You didnt enter a valid size\n");
}
}
//Loops to get a valid height of grid
valid=false;
int height;
while(!valid)
{
printf("Please enter the number of rows\n");
scanf("%d",&height);
if(height>3)
{
valid=true;
}
else
{
printf("You didnt enter a valid size\n");
}
}
//Creates array of initial values
int initialValues[height][length];
//Loop for height
for(int i=0; i<height; i++)
{
//Loop for width
for(int j=0; j<length; j++)
{
bool valid=false;
while(!valid)
{
int currentValue = 0;
printf("(%d,%d): ",(j+1),(i+1));
scanf("%d",¤tValue);
if(currentValue==1||currentValue==0)
{
initialValues[i][j]=currentValue;
valid=true;
}
else
{
printf("You did not enter a valid input\n");
}
}
}
}
//Returns the grid
return createCellGrid(height, length, initialValues);
}