-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameoflife.c
107 lines (92 loc) · 2.71 KB
/
gameoflife.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
#include <stdio.h>
#include <stdlib.h>
#include <ncurses.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
typedef struct {
bool alive;
} cell_t;
int main(int argc, char *argv[]) {
int max_y = 0, max_x = 0;
int num_rows, num_cols;
int delay = 150000;
initscr();
noecho();
curs_set(FALSE);
nodelay(stdscr, true);
getmaxyx(stdscr, max_y, max_x);
num_cols = max_x-2, num_rows = max_y-3;
cell_t cells[num_cols][num_rows];
srand((unsigned)time(NULL));
for (int x = 0; x < num_cols; x++) {
for (int y = 0; y < num_rows; y++) {
bool alive = (rand() % 12) == 1;
cells[x][y].alive = alive;
}
}
int gen = 0;
while(1) {
clear();
int living = 0;
for (int x = 0; x < num_cols; x++) {
for (int y = 0; y < num_rows; y++) {
cell_t* cell = &cells[x][y];
cell_t neighbors[8];
int ncount = 0;
for (int nx = x-1; nx < x+2; nx++) {
for (int ny = y-1; ny < y+2; ny++) {
if (nx == x && ny == y) {
continue;
}
if ((nx > -1 && ny > -1) && (nx < num_cols && ny < num_rows)) {
neighbors[ncount++] = cells[nx][ny];
}
}
}
int nalive = 0;
for (int i = 0; i < ncount; i++) {
if (neighbors[i].alive) {
nalive++;
}
}
/* Any live cell with two or three live neighbors survives. */
if (cell->alive) {
cell->alive = (nalive > 1 && nalive < 4);
} else {
/* Any dead cell with three live neighbors becomes a live cell. */
cell->alive = (nalive == 3);
}
/* All other live cells die in the next generation. Similarly, all other dead cells stay dead. */
if (cell->alive == true) {
living++;
mvwaddch(stdscr, y+1, x+1, '*');
}
}
}
mvprintw(max_y-1, 0, "Living: %d\tGeneration: %d", living, gen);
char* control_string = "(F)aster/(S)lower";
mvprintw(max_y-1, max_x-(strlen(control_string)), control_string);
usleep(delay);
gen = gen + 1;
refresh();
int c = getch();
switch (c) {
case 'f': {
if (delay >= 25000) {
delay -= 25000;
}
break;
}
case 's': {
if (delay <= 500000) {
delay += 25000;
}
break;
}
}
}
endwin();
return 0;
}