{"name":"gameoflife.nu","source":"// gameoflife.nu — Conway's Life, classic two-color, big and clear\n\n& libc @ rand → i\n\n& canvas @ canvas_open i w i h → *i\n\n& canvas @ canvas_present → v\n\n& canvas @ canvas_sleep i ms → v\n\n& canvas @ canvas_should_close → i\n\n& canvas @ canvas_close → v\n\n: i CELL 8  // pixels per cell side\n: i GW 80  // grid width in cells\n: i GH 45  // grid height in cells\n: i W 640  // GW * CELL\n: i H 360  // GH * CELL\n: i FPS 15\n\n@ main → i {\n    : i ncells * GW GH\n    : i frame_ms / 1000 FPS\n\n    : *i grid_a # *i ( malloc * ncells 8 )\n    : *i grid_b # *i ( malloc * ncells 8 )\n\n    : ~ i ix 0\n    ~ < ix ncells {\n        = . grid_a ix 0\n        = . grid_b ix 0\n        = ix + ix 1\n    }\n\n    // Seed: Gosper Glider Gun + three free gliders\n    : [i seed [i |\n    29 5\n    27 6 29 6\n    17 7 18 7 25 7 26 7 39 7 40 7\n    16 8 20 8 25 8 26 8 39 8 40 8\n    5 9 6 9 15 9 21 9 25 9 26 9\n    5 10 6 10 15 10 19 10 21 10 22 10 27 10 29 10\n    15 11 21 11 29 11\n    16 12 20 12\n    17 13 18 13\n    46 25 47 26 45 27 46 27 47 27\n    61 35 62 36 60 37 61 37 62 37\n    11 30 12 31 10 32 11 32 12 32\n    ]\n    : i seed_n 102\n\n    : ~ i si 0\n    ~ < si seed_n {\n        : i si1 + si 1\n        : i sx . seed si\n        : i sy . seed si1\n        : i sidx + * sy GW sx\n        = . grid_a sidx 1\n        = si + si 2\n    }\n\n    : i BLACK 4278190080\n    : i WHITE 4294967295\n\n    : *i fb ( canvas_open W H )\n    : ~ i parity 0\n\n    ~ == 0 ( canvas_should_close ) {\n        : ~ i cy 0\n        ~ < cy GH {\n            : ~ i cx 0\n            ~ < cx GW {\n                : i idx + * cy GW cx\n                : i cur ? == parity 0 . grid_a idx . grid_b idx\n\n                : ~ i count 0\n                : ~ i dy -1\n                ~ <= dy 1 {\n                    : ~ i dx -1\n                    ~ <= dx 1 {\n                        : b is_self & == dx 0 == dy 0\n                        ? is_self {} {\n                            : i ny0 + cy dy\n                            : i nx0 + cx dx\n                            : i ny ? < ny0 0 + ny0 GH ? >= ny0 GH - ny0 GH ny0\n                            : i nx ? < nx0 0 + nx0 GW ? >= nx0 GW - nx0 GW nx0\n                            : i nidx + * ny GW nx\n                            : i nv ? == parity 0 . grid_a nidx . grid_b nidx\n                            = count + count nv\n                        }\n                        = dx + dx 1\n                    }\n                    = dy + dy 1\n                }\n\n                : b survives & == cur 1 & >= count 2 <= count 3\n                : b births & == cur 0 == count 3\n                : i alive_next ? | survives births 1 0\n\n                ? == parity 0 {\n                    = . grid_b idx alive_next\n                } {\n                    = . grid_a idx alive_next\n                }\n\n                // Paint the CELL × CELL block, no trail\n                : i color ? == alive_next 1 WHITE BLACK\n                : i px0 * cx CELL\n                : i py0 * cy CELL\n                : ~ i py 0\n                ~ < py CELL {\n                    : ~ i px 0\n                    ~ < px CELL {\n                        : i fy + py0 py\n                        : i fx + px0 px\n                        : i fidx + * fy W fx\n                        = . fb fidx color\n                        = px + px 1\n                    }\n                    = py + py 1\n                }\n\n                = cx + cx 1\n            }\n            = cy + cy 1\n        }\n\n        = parity ? == parity 0 1 0\n        ( canvas_present )\n        ( canvas_sleep frame_ms )\n    }\n\n    ( canvas_close )\n    ^ 0\n}\n","bytes":3564}