-
Notifications
You must be signed in to change notification settings - Fork 4
/
game.c
318 lines (265 loc) · 7.52 KB
/
game.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
/*
* Copyright (C) 2009 Raphael Kubo da Costa <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <pcre.h>
#include <pthread.h>
#include <string.h>
#include "config.h"
#include "game.h"
#include "mem.h"
/**
* Returns the string matched by the first subgroup pattern in a regular expression.
*
* This function is useful if the regular expression is used primarily to find
* one single pattern inside the given string.
* Please note that it's up to the programmer to specify a regular expression with
* at least one subgroup.
*
* @param pattern The regular expression to use.
* @param subject The string to be searched.
*
* @return The matched string.
*/
static char *__re_get_first_match(const char *pattern, const char *subject)
{
int erroffset;
const char *error;
char *match;
int ovector[30];
int rc;
pcre *re;
re = pcre_compile(pattern, PCRE_NEWLINE_LF, &error, &erroffset, NULL);
if (re == NULL)
return NULL;
rc = pcre_exec(re, NULL, subject, strlen(subject), 0, 0, ovector, 30);
if (rc <= 0)
return NULL;
match = MEM_ALLOC_N(char, ovector[3] - ovector[2] + 1);
strncpy(match, subject + ovector[2], ovector[3] - ovector[2]);
pcre_free(re);
return match;
}
/**
* Parses the custom board file format.
*
* @param game Pointer to a Game structure.
* @param board The file to read.
*
* @retval 0 The file was parsed correctly.
* @retval 1 The file could not be parsed.
*/
static int __parse_custom_format(Game *game, FILE * board)
{
char boardline_re[20];
char *endptr;
char header_line[16];
size_t i, j;
char *line;
char *s;
/* First line - "Rows:NNN" */
fgets(header_line, 16, board);
s = __re_get_first_match("^Rows:(\\d{1,10})$", header_line);
if (!s) {
free(s);
return 1;
}
game->rows = (size_t) strtol(s, &endptr, 10);
if (*endptr != '\0') {
free(s);
return 1;
} else {
free(s);
}
/* Second line - "Cols:NNN" */
fgets(header_line, 16, board);
s = __re_get_first_match("^Cols:(\\d{1,10})$", header_line);
if (!s) {
free(s);
return 1;
}
game->cols = (size_t) strtol(s, &endptr, 10);
if (*endptr != '\0') {
free(s);
return 1;
} else {
free(s);
}
/* Allocate memory for the board */
if (game->board)
free(game->board);
game->board = MEM_ALLOC_N(char, game->cols * game->rows);
/* Read game->rows lines describing the board */
sprintf(boardline_re, "^([#.]{%u})$", game->cols);
line = MEM_ALLOC_N(char, game->cols + 2);
for (i = 0; i < game->rows; i++) {
fgets(line, game->cols + 2, board);
s = __re_get_first_match(boardline_re, line);
if (!s) {
free(line);
free(s);
return 1;
}
for (j = 0; j < game->cols; j++) {
if (s[j] == '#')
game_set_alive(game, i, j);
else
game_set_dead(game, i, j);
}
free(s);
}
free(line);
return 0;
}
/**
* Analyzes a particular part of the game board and update its state to the next generation.
*
* @param t Pointer to a __ThreadInfo structure.
*/
static void *__process_slice(void *t)
{
char live_count;
size_t row, col;
__ThreadInfo *tinfo = (__ThreadInfo*)t;
for (col = tinfo->col; (col < (tinfo->col + tinfo->width)) && (col < tinfo->game->cols); col++) {
for (row = 0; row < tinfo->game->rows; row++) {
live_count = 0;
/* Count the living neighbour cells */
if (game_is_alive(tinfo->game, row, col+1)) live_count++;
if (game_is_alive(tinfo->game, row+1, col)) live_count++;
if (game_is_alive(tinfo->game, row+1, col+1)) live_count++;
if (row > 0) {
if (game_is_alive(tinfo->game, row-1, col)) live_count++;
if (game_is_alive(tinfo->game, row-1, col+1)) live_count++;
}
if (col > 0) {
if (game_is_alive(tinfo->game, row, col-1)) live_count++;
if (game_is_alive(tinfo->game, row+1, col-1)) live_count++;
}
if ((row > 0) && (col > 0))
if (game_is_alive(tinfo->game, row-1, col-1)) live_count++;
/* Apply the game's rules to the current cell */
if ((live_count < 2) || (live_count > 3))
tinfo->new_board[row * tinfo->game->cols + col] = 0;
else if (live_count == 3)
tinfo->new_board[row * tinfo->game->cols + col] = 1;
else
tinfo->new_board[row * tinfo->game->cols + col] = tinfo->game->board[row * tinfo->game->cols + col];
}
}
return NULL;
}
void game_free(Game *game)
{
if (game) {
if (game->board)
free(game->board);
free(game);
}
}
int game_is_alive(Game *game, size_t row, size_t col)
{
assert(game);
assert(game->board);
if ((row >= game->rows) || (col >= game->cols))
return 0;
return game->board[row * game->cols + col] == 1;
}
int game_is_dead(Game *game, size_t row, size_t col)
{
return !game_is_alive(game, row, col);
}
Game *game_new(void)
{
Game *game = MEM_ALLOC(Game);
game->board = NULL;
game->cols = 0;
game->rows = 0;
return game;
}
int game_parse_board(Game *game, GameConfig *config)
{
FILE *board;
int exit_code;
long input_file_pos;
assert(game);
assert(config);
assert(config->input_file);
board = config->input_file;
input_file_pos = ftell(board);
fseek(board, 0, SEEK_SET);
exit_code = __parse_custom_format(game, board);
fseek(board, input_file_pos, SEEK_SET);
return exit_code;
}
void game_print_board(Game *game)
{
size_t col, row;
assert(game);
assert(game->board);
for (row = 0; row < game->rows; row++) {
for (col = 0; col < game->cols; col++) {
printf("%c", game_is_alive(game, row, col) ? '#' : '.');
}
printf("\n");
}
}
void game_set_alive(Game *game, size_t row, size_t col)
{
assert(game);
assert(game->board);
assert(row < game->rows);
assert(col < game->cols);
game->board[row * game->cols + col] = 1;
}
void game_set_dead(Game *game, size_t row, size_t col)
{
assert(game);
assert(game->board);
assert(row < game->rows);
assert(col < game->cols);
game->board[row * game->cols + col] = 0;
}
int game_tick(Game *game)
{
char *new_board;
int retval = 0;
size_t slice_count;
__ThreadInfo *tinfo;
size_t tnum = 0;
slice_count = (game->cols / BOARD_SLICE_WIDTH) + (game->cols % BOARD_SLICE_WIDTH ? 1 : 0);
new_board = MEM_ALLOC_N(char, game->rows * game->cols);
tinfo = MEM_ALLOC_N(__ThreadInfo, slice_count);
for (tnum = 0; tnum < slice_count; tnum++) {
tinfo[tnum].col = tnum * BOARD_SLICE_WIDTH;
tinfo[tnum].game = game;
tinfo[tnum].new_board = new_board;
tinfo[tnum].width = BOARD_SLICE_WIDTH;
if (pthread_create(&tinfo[tnum].tid, NULL, &__process_slice, &tinfo[tnum])) {
fprintf(stderr, "Error while creating thread %u. Waiting for other threads to finish.\n", tnum);
retval = 1;
}
}
for (tnum = 0; tnum < slice_count; tnum++) {
if (pthread_join(tinfo[tnum].tid, NULL))
retval = 1;
}
/* Make game use the new board and drop the old one */
free(game->board);
game->board = new_board;
free(tinfo);
return retval;
}