-
Notifications
You must be signed in to change notification settings - Fork 0
/
conway_typedarray_sweetjs_compiled.js
101 lines (101 loc) · 3.25 KB
/
conway_typedarray_sweetjs_compiled.js
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
var COLS = 177;
var ROWS = 60;
var update_ghost = function (grid, ghost_grid) {
/*
A virtual grid that includes wrapped edges, so that we don't have to
do funky modulo arithmetic.
*/
var n, x, y;
/* Copy bottom of grid to top of ghost_grid */
for (n = 0; n < COLS; n++) {
ghost_grid[0 * COLS + (n + 1)] = grid[(ROWS - 1) * COLS + n];
}
/* Copy top of grid to bottom of ghost_grid */
for (n = 0; n < COLS; n++) {
ghost_grid[(ROWS + 2 - 1) * COLS + (n + 1)] = grid[0 * COLS + n];
}
/* Copy the rest of grid to ghost_grid */
for (y = 0; y < ROWS; y++) {
for (x = 0; x < COLS; x++) {
ghost_grid[(y + 1) * COLS + (x + 1)] = grid[y * COLS + x];
}
}
/* Wrap ghost_grid left and right columns */
for (y = 0; y < ROWS + 2; y++) {
ghost_grid[y * COLS + 0] = ghost_grid[y * COLS + (COLS + 2 - 2)];
ghost_grid[y * COLS + (COLS + 2 - 1)] = ghost_grid[y * COLS + 1];
}
};
var count_neighbors = function (x, y, ghost_grid) {
return ghost_grid[(y - 1 + 1) * COLS + (x - 1 + 1)] + ghost_grid[(y - 1 + 1) * COLS + (x + 1)] + ghost_grid[(y - 1 + 1) * COLS + (x + 1 + 1)] + ghost_grid[(y + 1) * COLS + (x - 1 + 1)] + ghost_grid[(y + 1) * COLS + (x + 1 + 1)] + ghost_grid[(y + 1 + 1) * COLS + (x - 1 + 1)] + ghost_grid[(y + 1 + 1) * COLS + (x + 1)] + ghost_grid[(y + 1 + 1) * COLS + (x + 1 + 1)];
};
var out = new Buffer((COLS + 1) * ROWS);
var pretty_print = function (grid) {
var x, y;
var out_i = 0;
for (y = 0; y <= ROWS - 1; y++) {
for (x = 0; x <= COLS - 1; x++) {
if (grid[y * COLS + x] == 0) {
out[out_i] = 32;
// " "
out_i += 1;
} else {
out[out_i] = 35;
// "#"
out_i += 1;
}
}
out[out_i] = 10;
// "\n"
out_i += 1;
}
process.stdout.write(out);
};
var next_gen = function (grid, ghost_grid) {
var neighbors, x, y;
for (y = 0; y < ROWS; y++) {
for (x = 0; x < COLS; x++) {
neighbors = count_neighbors(x, y, ghost_grid);
//printf("N %d, ", neighbors);
if (neighbors < 2 || neighbors > 3) {
grid[y * COLS + x] = 0;
} else if (neighbors == 3) {
grid[y * COLS + x] = 1;
}
}
}
update_ghost(grid, ghost_grid);
};
var make_grid = function (width, height) {
return new Int8Array(width * height);
};
var run_once = function () {
var grid = make_grid(COLS, ROWS);
var ghost_grid = make_grid(COLS + 2, ROWS + 2);
var x;
var y;
/* Generate a random grid */
for (y = 0; y < ROWS; y++) {
for (x = 0; x < COLS; x++) {
grid[y * COLS + x] = Math.floor(Math.random() * 2);
}
}
pretty_print(grid);
update_ghost(grid, ghost_grid);
var iterations = 10000;
while (iterations--) {
process.stdout.write('\n\n\n');
pretty_print(grid);
//nanosleep(&wait, NULL);
next_gen(grid, ghost_grid);
}
};
var main = function () {
run_once();
process.stderr.write('Warmed up.\n');
var start = Date.now();
run_once();
var end = Date.now();
process.stderr.write('Ran in ' + (end - start) + 'ms\n');
};
main();