/
main.c
585 lines (549 loc) · 18 KB
/
main.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<ncurses.h>
#include<string.h>
struct cell_t {
bool alive; // true if alive, false if dead
int neighbors; // number of alive neighbors
};
typedef struct cell_t cell;
struct board_t {
int width;
int height;
cell** cells;
};
typedef struct board_t* board;
cell init_cell() {
cell c;
c.alive = false;
c.neighbors = 0;
return c;
}
// returns equivelent of true if given strings are
// equal to eachother
int streq(const char* s1, const char* s2) {
return !strcmp(s1, s2);
}
void set_alive(board b, int h, int w, bool alive) {
b->cells[h][w].alive = alive;
}
int is_alive(board b, int h, int w) {
return b->cells[h][w].alive;
}
void toggle_alive(board b, int h, int w) {
if(is_alive(b, h, w)) {
set_alive(b,h, w, false);
} else {
set_alive(b,h, w, true);
}
}
cell* init_row(int width) {
cell* cells = malloc(sizeof(cell)*width);
int i;
for(i = 0; i < width; i++) {
cells[i] = init_cell();
}
return cells;
}
board init_board(int height, int width) {
board b = malloc(sizeof(struct board_t));
b->width = width;
b->height = height;
/* since cells is a 2d array of cells,
each element of cells is an array of cells,
or, cell*. so we initialize cells memory
to be the size of an array of cells multiplied
by the number of rows there are
*/
b->cells = malloc(sizeof(cell*)*height);
int i;
for(i = 0; i < height; i++) {
b->cells[i] = init_row(width);
}
return b;
}
board init_board_from_chars(char* pic, int h, int w) {
board b = init_board(h, w);
int i,j;
for(i=0; i<h; i++) {
for(j=0; j<w; j++) {
if(pic[i*w+j] == '.') {
set_alive(b, i, j, false);
} else if(pic[i*w+j] == 'X') {
set_alive(b, i, j, true);
} else {
printf("FAIL %c\n", pic[i*w+j]);
}
}
}
return b;
}
board init_board_from_file(const char* file) {
FILE* f = open(file, "r");
return init_board(1,1);
}
board clone_board(board b) {
board newboard = init_board(b->height, b->width);
int i,j;
for(i=0; i<b->height; i++) {
for(j=0; j<b->width; j++) {
newboard->cells[i][j] = b->cells[i][j];
}
}
return newboard;
}
void free_board(board b) {
int i;
for(i = 0; i < b->height; i++) {
free(b->cells[i]);
}
free(b->cells);
free(b);
}
board change_board_size(board b, int diff_x, int diff_y) {
// takes a board, and some x,y values which alter the size
// of said board, growing/shrinking the boards dimentions
// according to them
// NOTE: this breaks when the board grows as cloning
// a board to a size larger than itself is undefined behavior
b->width = b->width + diff_x;
b->height = b->height + diff_y;
return clone_board(b);
}
bool alive_at(board b, int x, int y) {
if(x >= b->width)
x = 0;
if(x < 0)
x = b->width-1;
if(y >= b->height)
y = 0;
if(y < 0)
y = b->height-1;
return is_alive(b, y, x);
}
int num_neighbors(board b, int y, int x) {
int count = 0;
if(alive_at(b, x-1, y-1))
count++;
if(alive_at(b, x-1, y))
count++;
if(alive_at(b, x, y-1))
count++;
if(alive_at(b, x+1, y))
count++;
if(alive_at(b, x, y+1))
count++;
if(alive_at(b, x-1, y+1))
count++;
if(alive_at(b, x+1, y-1))
count++;
if(alive_at(b, x+1, y+1))
count++;
return count;
}
void compute_neighbors(board b) {
int i, j;
for(i=0; i<b->height; i++) {
for(j=0; j<b->width; j++) {
b->cells[i][j].neighbors = num_neighbors(b, i, j);
}
}
}
cell iterate_cell(cell c) {
if(c.alive) {
// cell dies to starvation
if(c.neighbors < 2) {
c.alive = false;
return c;
}
// cell stays alive
if(c.neighbors <= 3 && c.neighbors >= 2) {
return c;
}
// cell is overcrowded and dies
if(c.neighbors > 3) {
c.alive = false;
return c;
}
} else {
// cell becomes alive
if(c.neighbors == 3) {
c.alive = true;
return c;
}
}
return c;
}
board iterate_board(board b) {
// because the whole board needs to tick
// at the same time as a result of eachother,
// we can't modify the cells in place
// NOTE: actually we could as long as we
// only modify the alive state and not the
// neighbors state
board newboard = clone_board(b);
int i,j;
for(i=0; i<b->height; i++) {
for(j=0; j<b->width; j++) {
newboard->cells[i][j] = iterate_cell(b->cells[i][j]);
}
}
return newboard;
}
void iterate_board2(board b) {
int i,j;
for(i=0; i<b->height; i++) {
for(j=0; j<b->width; j++) {
b->cells[i][j] = iterate_cell(b->cells[i][j]);
}
}
}
board tick_board(board b) {
compute_neighbors(b);
return iterate_board(b);
}
void print_board(int y, int x, board b) {
int i;
int j;
move(y,x);
for(i = 0; i < b->height; i++) {
for(j = 0; j < b->width; j++) {
cell c = b->cells[i][j];
if(c.alive) {
printw("X ");
} else {
printw(". ");
}
}
printw("\n");
}
printw("\n");
}
void print_help() {
printf("Conways Game of life\n\n");
printf("-h,--help\tdisplay this message\n");
printf("-d\t\tDebug mode, prints the key code of a key press,\n\t\twaits for you to hit another key then exits\n");
printf("-e,--editor\tStart the interactive editor\n");
printf("--engine\tRun the engine example\n");
printf("--glider\tRun the glider example\n");
printf("--gun\t\tRun the glider gun example\n");
printf("--pulsar\tRun the pulsar example\n");
printf("--pentimino\tRun the R-pentimino example\n");
}
board create_board_from_name(const char* name) {
board b;
if(streq(name, "-h") || streq(name, "--help")) {
print_help();
exit(1);
} else if(streq(name, "--engine")) {
int engine_x = 40;
int engine_y = 30;
char engine[][40] = {"........................................",
"........................................",
"........................................",
"........................................",
"........................................",
"........................................",
"........................................",
"........................................",
"........................................",
"........................................",
"........................................",
"........................................",
"........................................",
"........................................",
"........................................",
"...........X............................",
".........X.XX...........................",
".........X.X............................",
".........X..............................",
".......X................................",
".....X.X................................",
"........................................",
"........................................",
"........................................",
"........................................",
"........................................",
"........................................",
"........................................",
"........................................",
"........................................",
};
b = init_board_from_chars(&engine[0][0], engine_y, engine_x);
} else if(streq(name, "--glider")) {
int glider_x = 20;
int glider_y = 20;
char glider[][20] = {"....................",
"...X................",
"....X...............",
"..XXX...............",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"....................",
"...................."};
b = init_board_from_chars(&glider[0][0], glider_y, glider_x);
} else if(streq(name, "--gun")) {
int gun_x = 42;
int gun_y = 20;
char gun[][42] = {"..........................................",
"..........................................",
"..........................................",
"..........................................",
"..........................X...............",
"........................X.X...............",
"..............XX......XX............XX....",
".............X...X....XX............XX....",
"..XX........X.....X...XX..................",
"..XX........X...X.XX....X.X...............",
"............X.....X.......X...............",
".............X...X........................",
"..............XX..........................",
"..........................................",
"..........................................",
"..........................................",
"..........................................",
"..........................................",
"..........................................",
".........................................."};
b = init_board_from_chars(&gun[0][0], gun_y, gun_x);
} else if(streq(name, "--pulsar")) {
int pulsar_x = 19;
int pulsar_y = 17;
char pulsar[][19] = {"...................",
"...................",
".....XXX...XXX.....",
"...................",
"...X....X.X....X...",
"...X....X.X....X...",
"...X....X.X....X...",
".....XXX...XXX.....",
"...................",
".....XXX...XXX.....",
"...X....X.X....X...",
"...X....X.X....X...",
"...X....X.X....X...",
"...................",
".....XXX...XXX.....",
"...................",
"...................",};
b = init_board_from_chars(&pulsar[0][0], pulsar_y, pulsar_x);
} else if(streq(name, "--pentimino")) {
int pen_x = 75;
int pen_y = 32;
char pen[][75] = {"...........................................................................",
"...........................................................................",
"...........................................................................",
"...........................................................................",
"...........................................................................",
"...........................................................................",
"...........................................................................",
"...........................................................................",
"...........................................................................",
"...........................................................................",
"...........................................................................",
"...........................................................................",
"...........................................................................",
"...........................................XX..............................",
"..........................................XX...............................",
"...........................................X...............................",
"...........................................................................",
"...........................................................................",
"...........................................................................",
"...........................................................................",
"...........................................................................",
"...........................................................................",
"...........................................................................",
"...........................................................................",
"...........................................................................",
"...........................................................................",
"...........................................................................",
"...........................................................................",
"...........................................................................",
"...........................................................................",
"...........................................................................",
"...........................................................................",
};
b = init_board_from_chars(&pen[0][0], pen_y, pen_x);
} else {
echo();
curs_set(1);
print_help();
endwin();
exit(1);
}
return b;
}
int get_dim(const char* prompt) {
curs_set(1);
nocbreak();
echo();
char string[5];
mvprintw(0,0, prompt);
getstr(string);
int i;
sscanf(string, "%5i", &i);
return i;
}
void print_instructions() {
printw("hit q to run simulation\tArrow keys to move\n");
printw("Enter to toggle alive/dead and move down a row\n");
printw("Space to toggle alive/dead and move right a column\n");
}
board start_editor() {
int h = get_dim("enter height: ");
clear();
int w = get_dim("enter width: ");
curs_set(1);
noecho();
cbreak();
move(20,20);
int x = 0;
int x_1 = 0;
int y = 0;
board b = init_board(h, w);
int key;
print_board(0, 0, b);
print_instructions();
move(y,x);
while((key = getch()) != 113) { // wait until user hits q
print_board(0, 0, b);
print_instructions();
move(y,x);
if(key == 10) { // enter key
toggle_alive(b,y,x_1);
print_board(0,0,b);
print_instructions();
y++;
move(y,x);
refresh();
}
if(key == 32) { // space bar
toggle_alive(b,y,x_1);
print_board(0,0,b);
print_instructions();
x+=2;
x_1++;
move(y,x);
refresh();
}
if(key == 260 && x_1 > 0) { // left arrow key
x-=2;
x_1--;
move(y,x);
}
if(key == 261 && x_1 < w-1) { // right arrow key
x+=2;
x_1++;
move(y,x);
}
if(key == 259 && y > 0) { // up arrow key
y-=1;
move(y,x);
}
if(key == 258 && y < h-1) { // down arrow key
y+=1;
move(y,x);
}
if(key == 544) { // control left
board newboard = change_board_size(b, -1, 0);
free_board(b);
b = newboard;
w--;
print_board(0,0,b);
print_instructions();
move(y,x);
refresh();
}
if(key == 559) {
board newboard = change_board_size(b, 1, 0);
free_board(b);
b = newboard;
w++;
print_board(0,0,b);
print_instructions();
move(y,x);
refresh();
}
if(key == 565) { // control up
board newboard = change_board_size(b, 0, -1);
free_board(b);
b = newboard;
h--;
print_board(0,0,b);
print_instructions();
move(y,x);
refresh();
}
if(key == 524) { // control down
board newboard = change_board_size(b, 0, 1);
free_board(b);
b = newboard;
h++;
print_board(0,0,b);
print_instructions();
move(y,x);
refresh();
}
refresh();
}
curs_set(0);
clear();
return b;
}
int main(int argc, char* argv[]) {
board b;
if(argc < 2) {
print_help();
exit(1);
}
initscr();
cbreak();
curs_set(0);
keypad(stdscr, TRUE);
noecho();
if(streq(argv[1], "-d")) {
int x = getch();
clear();
mvprintw(0,0,"%i", x);
getch();
echo();
curs_set(1);
endwin();
exit(0);
}
if(streq(argv[1], "-e") || streq(argv[1], "--editor")) {
b = start_editor();
} else {
b = create_board_from_name(argv[1]);
}
int generation = 0;
while(true) {
mvprintw(0,0,"Generation: %d", generation);
generation++;
print_board(1, 0, b);
refresh();
board b1 = tick_board(b);
free(b);
b = b1;
long one_second = 1000000;
usleep(one_second/6);
}
free_board(b);
echo();
curs_set(1);
endwin();
return 0;
}