-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.c
210 lines (164 loc) · 5.03 KB
/
board.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
#include "board.h"
static scm_t_bits board_tag;
static SCM make_board (SCM s_width, SCM s_height) {
int i;
int j;
SCM smob;
struct board *board;
int width = scm_to_int(s_width);
int height = scm_to_int(s_height);
board = (struct board *) scm_gc_malloc(sizeof(struct board), "board");
board->width = width;
board->height = height;
board->update_func = SCM_BOOL_F;
board->cell_list = SCM_EOL;
for (i = height - 1; i >= 0; i--) {
SCM row = SCM_EOL;
for (j = width - 1; j >= 0; j--) {
SCM y_offset = scm_from_int(i);
SCM x_offset = scm_from_int(j);
row = scm_cons(make_cell(x_offset, y_offset, scm_from_int(0)), row);
}
board->cell_list = scm_cons(row, board->cell_list);
}
SCM_NEWSMOB(smob, board_tag, board);
return smob;
}
SCM clear_board (SCM board_smob) {
int i;
int j;
struct board *board;
struct cell *cell;
scm_assert_smob_type(board_tag, board_smob);
board = (struct board *) SCM_SMOB_DATA(board_smob);
for (i = 0; i < board->height; i++) {
SCM row = scm_list_ref(board->cell_list, scm_from_int(i));
for (j = 0; j < board->width; j++) {
cell = (struct cell *) SCM_SMOB_DATA(scm_list_ref(row, scm_from_int(j)));
cell->status = 0;
}
}
scm_remember_upto_here_1(board_smob);
return SCM_UNSPECIFIED;
}
SCM get_cell (SCM board_smob, SCM s_x, SCM s_y) {
struct board *board;
scm_assert_smob_type(board_tag, board_smob);
board = (struct board *) SCM_SMOB_DATA(board_smob);
return scm_list_ref(scm_list_ref(board->cell_list, s_y), s_x);
}
SCM get_neighbors (SCM board_smob, SCM cell_smob) {
struct board *board;
struct cell *cell;
SCM list;
SCM neighbor;
int i;
int j;
int x;
int y;
scm_assert_smob_type(board_tag, board_smob);
scm_assert_smob_type(cell_tag, cell_smob);
board = (struct board *) SCM_SMOB_DATA(board_smob);
cell = (struct cell *) SCM_SMOB_DATA(cell_smob);
list = SCM_EOL;
for (i = -1; i < 2; i++) {
for (j = -1; j < 2; j++) {
if (i == 0 && j == 0) {
continue;
}
x = cell->x + i;
y = cell->y + j;
if (x >= 0 && x < board->width && y >= 0 && y < board->height) {
neighbor = scm_list_ref(scm_list_ref(board->cell_list, scm_from_int(y)), scm_from_int(x));
list = scm_cons(neighbor, list);
}
}
}
return list;
}
SCM get_living_neighbors (SCM board_smob, SCM cell_smob) {
SCM list; struct cell *cell;
int i;
int count;
scm_assert_smob_type(board_tag, board_smob);
scm_assert_smob_type(cell_tag, cell_smob);
list = get_neighbors(board_smob, cell_smob);
count = 0;
for (i = 0; i < scm_to_int(scm_length(list)); i++) {
cell = (struct cell *) SCM_SMOB_DATA(scm_list_ref(list, scm_from_int(i)));
if (cell->status > 0) {
count++;
}
}
return scm_from_int(count);
}
SCM apply_rule (SCM board_smob, SCM rule_func) {
SCM cell;
struct board *board;
int i;
int j;
scm_assert_smob_type(board_tag, board_smob);
board = (struct board *) SCM_SMOB_DATA(board_smob);
for (i = 0; i < board->height; i++) {
for (j = 0; j < board->width; j++) {
cell = scm_list_ref(scm_list_ref(board->cell_list, scm_from_int(j)), scm_from_int(i));
scm_call_2(rule_func, cell, get_living_neighbors(board_smob, cell));
}
}
return SCM_UNSPECIFIED;
}
SCM status_list (SCM board_smob) {
struct board *board;
struct cell *cell;
int i;
int j;
SCM cell_smob;
SCM list;
SCM row;
scm_assert_smob_type(board_tag, board_smob);
board = (struct board *) SCM_SMOB_DATA(board_smob);
list = SCM_EOL;
for (i = board->height - 1; i >= 0; i--) {
row = SCM_EOL;
for (j = board->width - 1; j >= 0; j--) {
cell_smob = scm_list_ref(scm_list_ref(board->cell_list, scm_from_int(j)), scm_from_int(i));
cell = (struct cell *) SCM_SMOB_DATA(cell_smob);
row = scm_cons(get_status(cell_smob), row);
}
list = scm_cons(row, list);
}
return list;
}
static SCM mark_board (SCM board_smob) {
struct board *board = (struct board *) SCM_SMOB_DATA(board_smob);
scm_gc_mark(board->update_func);
scm_gc_mark(board->cell_list);
return SCM_BOOL_F;
}
static size_t free_board (SCM board_smob) {
struct board *board = (struct board *) SCM_SMOB_DATA(board_smob);
scm_gc_free(board, sizeof(struct board), "board");
return 0;
}
static int print_board (SCM board_smob, SCM port, scm_print_state *pstate) {
struct board *board = (struct board *)SCM_SMOB_DATA(board_smob);
scm_puts("#<board ", port);
scm_display(scm_from_int(board->width), port);
scm_puts(":", port);
scm_display(scm_from_int(board->height), port);
scm_puts(">", port);
return 1;
}
void init_board_type (void) {
board_tag = scm_make_smob_type("board", sizeof(struct board));
scm_set_smob_mark(board_tag, mark_board);
scm_set_smob_free(board_tag, free_board);
scm_set_smob_print(board_tag, print_board);
scm_c_define_gsubr("make-board", 2, 0, 0, make_board);
scm_c_define_gsubr("clear-board", 1, 0, 0, clear_board);
scm_c_define_gsubr("get-cell", 3, 0, 0, get_cell);
scm_c_define_gsubr("get-neighbors", 2, 0, 0, get_neighbors);
scm_c_define_gsubr("get-living-neighbors", 2, 0, 0, get_living_neighbors);
scm_c_define_gsubr("apply-rule", 2, 0, 0, apply_rule);
scm_c_define_gsubr("status-list", 1, 0, 0, status_list);
}