1 /* A pretty dumb implementation of Conway's rules. Really only used
2 * for testing the driver
10 #define E_UNIMPLEMENTED -1
11 #define E_ILLEGAL_COORDINATES -2
12 #define E_UNINITIALIZED -3
14 #define E_MALFORMED_INPUT -5
16 static const char *ALIVE
= ".";
17 static const char *DEAD
= " ";
19 static char *alive_stat
= NULL
;
20 static char *nigh_count
= NULL
;
21 static char *freeze_alive_stat
= NULL
;
22 static char *freeze_nigh_count
= NULL
;
23 static int g_width
, g_height
;
25 static void n_add(int x
, int y
, int mod
)
28 if (y
- 1 >= 0) nigh_count
[g_width
* (y
- 1) + (x
- 1)] += mod
;
29 nigh_count
[g_width
* (y
) + (x
- 1)] += mod
;
30 if (y
+ 1 < g_height
) nigh_count
[g_width
* (y
+ 1) + (x
- 1)] += mod
;
33 if (y
- 1 >= 0) nigh_count
[g_width
* (y
- 1) + (x
)] += mod
;
34 if (y
+ 1 < g_height
) nigh_count
[g_width
* (y
+ 1) + (x
)] += mod
;
36 if (x
+ 1 < g_width
) {
37 if (y
- 1 >= 0) nigh_count
[g_width
* (y
- 1) + (x
+ 1)] += mod
;
38 nigh_count
[g_width
* (y
) + (x
+ 1)] += mod
;
39 if (y
+ 1 < g_height
) nigh_count
[g_width
* (y
+ 1) + (x
+ 1)] += mod
;
44 int con_initialize_f(FILE * ifp
){
45 int i_width
, i_height
;
50 if (fscanf(ifp
, " %d x %d ", &i_width
, &i_height
) != 2)
51 return E_MALFORMED_INPUT
;
53 if (i_width
<= 0 || i_height
<= 0)
54 return E_MALFORMED_INPUT
;
58 alive_stat
= calloc(g_width
* g_height
, sizeof(*alive_stat
));
59 nigh_count
= calloc(g_width
* g_height
, sizeof(*nigh_count
));
60 freeze_alive_stat
= malloc(g_width
* g_height
* sizeof(*alive_stat
));
61 freeze_nigh_count
= malloc(g_width
* g_height
* sizeof(*nigh_count
));
64 for (j
= 0; j
< g_height
; ++j
) {
65 for (i
= 0; i
< g_width
; ++i
) {
68 } while (c
!= ' ' && c
!= '.' && c
!= EOF
);
71 return E_MALFORMED_INPUT
;
83 int con_initialize_wh(int width
, int height
)
85 if (width
<= 0 || height
<= 0)
86 return E_MALFORMED_INPUT
;
88 alive_stat
= calloc(width
* height
, sizeof(*alive_stat
));
89 nigh_count
= calloc(width
* height
, sizeof(*nigh_count
));
90 freeze_alive_stat
= malloc(width
* height
* sizeof(*alive_stat
));
91 freeze_nigh_count
= malloc(width
* height
* sizeof(*nigh_count
));
98 static const char *title
= "Alive? [0/1]";
100 int con_input_titles(int * const n
, const char * const ** const titles
)
107 int con_set_val(int x
, int y
, const char ** vals
)
112 return E_UNINITIALIZED
;
114 if (x
< 0 || y
< 0 || x
>= g_width
|| y
>= g_height
|| !vals
|| !vals
[0])
115 return E_ILLEGAL_COORDINATES
;
117 v
= (vals
[0][0] == '1' || vals
[0][0] == '.');
119 if (!v
&& alive_stat
[y
* g_width
+ x
]) {
121 alive_stat
[y
* g_width
+ x
] = 0;
122 } else if (v
&& !alive_stat
[y
* g_width
+ x
]) {
124 alive_stat
[y
* g_width
+ x
] = 1;
129 int con_set_p(int x
, int y
, void *val
){ return E_UNIMPLEMENTED
; }
134 return E_UNINITIALIZED
;
142 return E_UNINITIALIZED
;
153 return E_UNINITIALIZED
;
155 memcpy(freeze_alive_stat
, alive_stat
,
156 g_width
* g_height
* sizeof (*alive_stat
));
157 memcpy(freeze_nigh_count
, nigh_count
,
158 g_width
* g_height
* sizeof (*nigh_count
));
160 for (y
= 0; y
< g_height
; ++y
) {
161 for (x
= 0; x
< g_width
; ++x
) {
162 if (freeze_alive_stat
[m
]
163 && (freeze_nigh_count
[m
] < 2 || freeze_nigh_count
[m
] > 3)) {
166 } else if (!freeze_alive_stat
[m
] && freeze_nigh_count
[m
] == 3) {
175 int con_rep_at(int x
, int y
, const char ** const rep
)
178 return E_UNINITIALIZED
;
183 if (x
< 0 || y
< 0 || x
>= g_width
|| y
>= g_height
)
184 return E_ILLEGAL_COORDINATES
;
186 if (alive_stat
[y
* g_width
+ x
])
194 int con_teardown(void)
200 if (freeze_alive_stat
)
201 free(freeze_alive_stat
);
202 if (freeze_nigh_count
)
203 free(freeze_nigh_count
);
208 const char *con_errstring(int errno
)
211 case 0: return "No error";
212 case E_UNIMPLEMENTED
: return "Unimplemented function";
213 case E_UNINITIALIZED
: return "Uninitialized";
214 case E_ILLEGAL_COORDINATES
: return "Illegal coordinates";
215 case E_NULLREP
: return "Null pointer passed";
216 case E_MALFORMED_INPUT
: return "Malformed input file";
217 default: return "Unknown error";