-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathB9.c
More file actions
224 lines (205 loc) · 6.44 KB
/
Copy pathB9.c
File metadata and controls
224 lines (205 loc) · 6.44 KB
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
#include "life.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdatomic.h>
#include <limits.h>
#include "termin.h"
// The keys that are used for cursor movement
// Other keys that are used are:
// b, B, space for birth9
// -, + to slow down and accelerate
// q, Q to quit
#define GO_UP 'k'
#define GO_DOWN 'l'
#define GO_RIGHT ';'
#define GO_LEFT 'j'
#define GO_HOME 'h'
// We translate escape sequences that we may receive to these standard
// characters for further processing.
#define ESCAPE '\e'
char const*const termin_trans[UCHAR_MAX+1] = {
[GO_UP] = ESC_UP,
[GO_DOWN] = ESC_DOWN,
[GO_RIGHT] = ESC_FRWD,
[GO_LEFT] = ESC_BKWD,
[GO_HOME] = ESC_HOME,
};
static
int update_thread(void* Lv) {
life*restrict L = Lv;
size_t changed = 1;
size_t birth9 = 0;
while (!L->finished && changed) {
// Blocks until there is work
mtx_lock(&L->mtx);
while (!L->finished && (L->accounted < L->iteration))
life_wait(&L->upda, &L->mtx);
// VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
if (birth9 != L->birth9) life_torus(L);
life_count(L);
changed = life_update(L);
life_torus(L);
birth9 = L->birth9;
L->iteration++;
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
cnd_signal(&L->acco);
cnd_signal(&L->draw);
mtx_unlock(&L->mtx);
life_sleep(1.0/L->frames);
}
return 0;
}
static
int draw_thread(void* Lv) {
life*restrict L = Lv;
size_t x0 = 0;
size_t x1 = 0;
fputs(ESC_CLEAR ESC_CLRSCR, stdout);
while (!L->finished) {
// Blocks until there is work
mtx_lock(&L->mtx);
while (!L->finished
&& (L->iteration <= L->drawn)
&& (x0 == L->x0)
&& (x1 == L->x1)) {
life_wait(&L->draw, &L->mtx);
}
// VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
if (L->n0 <= 30) life_draw(L);
else life_draw4(L);
L->drawn++;
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
mtx_unlock(&L->mtx);
x0 = L->x0;
x1 = L->x1;
// No need to draw too quickly
life_sleep(1.0/40);
}
return 0;
}
// This number of consecutive states that are already known (that is,
// hashed in) decides that this sequence of states is cyclic
enum { repetition = 10, };
static
int account_thread(void* Lv) {
life*restrict L = Lv;
while (!L->finished) {
// Blocks until there is work
mtx_lock(&L->mtx);
while (!L->finished && (L->accounted == L->iteration))
life_wait(&L->acco, &L->mtx);
// VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
life_account(L);
if ((L->last + repetition) < L->accounted) {
L->finished = true;
}
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
cnd_signal(&L->upda);
mtx_unlock(&L->mtx);
}
return 0;
}
static
int input_thread(void* Lv) {
termin_unbuffered();
life*restrict L = Lv;
enum { len = 32, };
char command[len];
do {
int c = getchar();
command[0] = c;
switch(c) {
case GO_LEFT : life_advance(L, 0, -1); break;
case GO_RIGHT: life_advance(L, 0, +1); break;
case GO_UP : life_advance(L, -1, 0); break;
case GO_DOWN : life_advance(L, +1, 0); break;
case GO_HOME : L->x0 = 1; L->x1 = 1; break;
case ESCAPE :
ungetc(termin_translate(termin_read_esc(len, command)), stdin);
continue;
case '+': if (L->frames < 128) L->frames++; continue;
case '-': if (L->frames > 1) L->frames--; continue;
case ' ':
case 'b':
case 'B':
mtx_lock(&L->mtx);
// VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
life_birth9(L); /*@\label{lab:birth9}*/
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
cnd_signal(&L->draw);
mtx_unlock(&L->mtx);
continue;
case 'q':
case 'Q':
case EOF: goto FINISH;
}
cnd_signal(&L->draw); /*@\label{lab:signal}*/
} while (!(L->finished || feof(stdin)));
FINISH:
L->finished = true;
return 0;
}
enum { n = 60, m = 160 };
bool M[n][m] = {
{ 0 },
{ 0 },
{ 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0},
{ 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0},
{ 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0},
{ 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0},
{ 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0},
{ 0 },
{ 0 },
{ 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0 },
{ 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0},
{ 0, 0, 0, 0, 1, 0, 1, 0, 1,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 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0 },
{ 0 },
};
int main(int argc, char* argv[argc+1]) {
/* Uses command-line arguments for the size of the board */
size_t n0 = 30;
size_t n1 = 80;
if (argc > 1) n0 = strtoull(argv[1], 0, 0);
if (argc > 2) n1 = strtoull(argv[2], 0, 0);
/* Create an object that holds the game's data. */
life L = LIFE_INITIALIZER;
life_init(&L, n0, n1, M);
/* Creates four threads that all operate on that same object
and collects their IDs in "thrd" */
thrd_t thrd[4];
thrd_create(&thrd[0], update_thread, &L);
thrd_create(&thrd[1], draw_thread, &L);
thrd_create(&thrd[2], input_thread, &L);
thrd_create(&thrd[3], account_thread, &L);
/* Waits for the update thread to terminate */
thrd_join(thrd[0], 0);
/* Tells everybody that the game is over */
L.finished = true;
ungetc('q', stdin);
/* Waits for the other threads */
thrd_join(thrd[1], 0);
thrd_join(thrd[2], 0);
thrd_join(thrd[3], 0);
/* Puts the board in a nice final picture */
L.iteration = L.last;
life_draw(&L);
life_destroy(&L);
}