-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid.c
More file actions
102 lines (94 loc) · 2.11 KB
/
Copy pathgrid.c
File metadata and controls
102 lines (94 loc) · 2.11 KB
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
/**
* self-programming game of life
*
* the grid on which splife is played
*/
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "grid.h"
/**
* initialize grid G with dimension D
*
* D must be a positive, odd number
* grids are always square
*/
int grid_init (grid_t *g, uint32_t d)
{
if (d <= 0)
return 0;
g->d = d;
g->m = malloc(d * d * sizeof(uint8_t));
if (NULL == g->m)
return 0;
memset(g->m, 0, d * d * sizeof(uint8_t));
return 1;
}
/**
* release memory allocated by grid_init. idempotent.
*/
void grid_free (grid_t *g)
{
if (g->m != NULL)
free(g->m);
g->m = NULL;
g->d = 0;
}
/**
* add rows and columns to the outside edges of grid G
*
* s must be a positive even number, to evenly distribute rows/columns
* frees the old grid and allocates a new one
* maintains the grid data in the center of the new grid
*/
int grid_extend (grid_t *g, uint8_t s)
{
uint32_t d;
uint8_t *m;
uint8_t x, y;
if (s <= 0 || s & 1)
return 0;
d = g->d + s;
// uint32_t overflow check
if (d <= g->d)
return 0;
m = malloc(d * d * sizeof(uint8_t));
if (NULL == m)
return 0;
memset(m, 0, d * d * sizeof(uint8_t));
// translate g->m into center of m
for (y = 0; y < g->d; y++)
for (x = 0; x < g->d; x++)
m[(x + s) + (d * (y + s))] = g->m[x + (g->d * y)];
free(g->m);
g->m = m;
g->d = d;
return 1;
}
/**
* write a cell state value at <x,y> in grid G
*
* value written to cell is 1 (live) when v is 'truthy', which is all nonzero numbers
* otherwise zero (dead) is written
*/
int grid_write (grid_t *g, uint32_t x, uint32_t y, uint8_t v)
{
uint32_t d = g->d;
if (x >= d || y >= d)
return 0;
g->m[x + d * y] = v ? 1 : 0;
return 1;
}
/**
* read cell state value at <x,y> in grid G
*
* positions outside the grid's bounds are always zero (dead)
* this is not success/failure code
*/
uint8_t grid_read (grid_t *g, uint32_t x, uint32_t y)
{
uint32_t d = g->d;
if (x >= d || y >= d)
return 0;
return g->m[x + d * y];
}