-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnclife.c
224 lines (197 loc) · 3.82 KB
/
nclife.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
// TODO: add menus and make the time step persistent across pauses (and less
// jittery)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <math.h>
#include <curses.h>
#include <locale.h>
void draw(bool **state)
{
for (int i = 0; i < LINES; ++i) {
for (int j = 0; j < COLS; ++j) {
if (state[i][j])
mvaddch(i, j, '*');
else
mvaddch(i, j, ' ');
}
}
}
bool lives(int y, int x, bool **state)
{
int live_neighbors = 0;
if (y != 0 && x != 0 && state[y-1][x-1])
++live_neighbors;
if (y != 0 && state[y-1][x])
++live_neighbors;
if (y != 0 && x < COLS - 1 && state[y-1][x+1])
++live_neighbors;
if (x != 0 && state[y][x-1])
++live_neighbors;
if (x < COLS - 1 && state[y][x+1])
++live_neighbors;
if (y < LINES - 1 && x != 0 && state[y+1][x-1])
++live_neighbors;
if (y < LINES - 1 && state[y+1][x])
++live_neighbors;
if (y < LINES - 1 && x < COLS - 1 && state[y+1][x+1])
++live_neighbors;
if (state[y][x]) {
if (live_neighbors < 2 || live_neighbors > 3)
return false;
} else if (live_neighbors == 3) {
return true;
} else {
return false;
}
return true;
}
enum { PAUSE, QUIT };
int game(bool **state)
{
int ch;
bool pause = false;
struct timespec tim;
tim.tv_sec = 1;
tim.tv_nsec = 0;
bool *oldstate[LINES];
for (int i = 0; i < LINES; ++i)
oldstate[i] = calloc(COLS, sizeof(bool));
while (true) {
if ((ch = getch()) != ERR) {
double doubtime = tim.tv_sec + (double) (tim.tv_nsec) / 1000000000L;
switch (ch) {
case 'p':
pause = true;
goto end;
break;
case 'q':
goto end;
break;
case '+':
case '=':
doubtime /= 1.5;
break;
case '-':
doubtime *= 1.5;
break;
}
// Technically there could be time drift here from all
// the conversions if you were to keep pressing keys
// that weren't in the case statement (or just keep
// speeding up and slowing down), but I'd be impressed
// if you could get something noticeable out of it.
tim.tv_sec = doubtime;
tim.tv_nsec = fmod(doubtime, 1) * 1000000000L;
}
for (int i = 0; i < LINES; ++i)
for (int j = 0; j < COLS; ++j)
oldstate[i][j] = state[i][j];
for (int i = 0; i < LINES; ++i) {
for (int j = 0; j < COLS; ++j) {
if (lives(i, j, oldstate))
state[i][j] = true;
else
state[i][j] = false;
}
}
getmaxyx(stdscr, LINES, COLS);
draw(state);
refresh();
nanosleep(&tim, NULL);
}
end:
for (int i = 0; i < LINES; ++i)
free(oldstate[i]);
if (pause)
return PAUSE;
else
return QUIT;
}
void toggle(int y, int x, bool **state)
{
if (state[y][x]) {
state[y][x] = false;
mvaddch(y, x, ' ');
} else {
state[y][x] = true;
mvaddch(y, x, '*');
}
move(y, x);
}
int main()
{
int ch, y, x;
bool **state;
// Various options everyone uses with curses
setlocale(LC_ALL, "");
initscr();
cbreak();
noecho();
nonl();
intrflush(stdscr, FALSE);
keypad(stdscr, TRUE);
nodelay(stdscr, TRUE);
state = malloc(LINES * sizeof(bool *));
for (int i = 0; i < LINES; ++i)
state[i] = calloc(COLS, sizeof(bool));
y = LINES / 2;
x = COLS / 2;
move(y, x);
refresh();
while (true) {
int stat;
getyx(stdscr, y, x);
ch = getch();
switch (ch) {
case KEY_LEFT:
case 'h':
move(y, --x);
break;
case KEY_DOWN:
case 'j':
move(++y, x);
break;
case KEY_UP:
case 'k':
move(--y, x);
break;
case KEY_RIGHT:
case 'l':
move(y, ++x);
break;
case 'g':
move(0, x);
break;
case 'G':
move(LINES - 1, x);
break;
case '0':
move(y, 0);
break;
case '$':
move(y, COLS - 1);
break;
case ' ':
toggle(y, x, state);
break;
case 'r':
stat = game(state);
if (stat == QUIT)
goto end;
move(y, x);
break;
case 'q':
goto end;
break;
}
refresh();
}
end:
for (int i = 0; i < LINES; ++i)
free(state[i]);
free(state);
endwin();
return 0;
}