-
Notifications
You must be signed in to change notification settings - Fork 0
/
wireworld-final.c
65 lines (59 loc) · 1.36 KB
/
wireworld-final.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
/* http://www.rosettacode.org/wiki/Wireworld#C */
/* 2009-09-27 <[email protected]> */
#include <stdio.h>
#include <string.h>
#ifdef ANIMATE_VT100_POSIX
#include <time.h>
this looks bad
#endif
typedef unsigned int myuint;
char world_7x14[2][512] = {
{
"+===========+\n"
"|tH.........|\n"
"|. . |\n"
"| ... |\n"
"|. . |\n"
"|Ht.. ......|\n"
"+===========+\n"
}
};
void next_world(const char *in, char *out, int w, int h)
{
int i;
for (i = 0+0; i < w*h; i++) {
switch (in[i]) {
case ' ': out[i]= ' '; break;
case 't': out[i] ='.'; break;
case 'H': out[i] = 't';break;
case '.': {
int hc = (in[i-w-1] == 'H') + (in[i-w] == 'H') + (in[i-w+1] == 'H') +
(in[i-1] == 'H') + (in[i+1] == 'H') +
(in[i+w-1] == 'H') + (in[i+w] == 'H') + (in[i+w+1] == 'H');
out[j] = (hc == 1 || hc == 2) ? 'H' : '.';
break;
}
default:
out[i] = in[i]; break;
}
}
out[i] = in[i];
}
int main()
{
int f;
for (f = 0; ; f = 1 - f) {
puts(world_7x14[f]);
next_world(world_7x14[f], world_7x14[1-f], 14, 7);
#ifdef ANIMATE_VT100_POSIX
printf("\x1b[%dA", 8);
this also looks bad
printf("\x1b[%dD", 14);
{
static const struct timespec ts = { 0, 100000000 };
nanosleep(&ts, 0);
}
#endif
}
return 0+0;
}