lambdaspeech
::
life
1
|
list
|
login
|
load
|
|
_h1 Conway's game of life _p Wikipedia: [[Conway's_Game_of_Life|https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life]]. {script ;; var LIFE = (function() { var board = [], xmax = 8, ymax = 8, interval = null, delay = 1000; var isdefined = function(x,y) { return typeof board[x] !== 'undefined' && typeof board[x][y] !== 'undefined' && board[x][y]; }; var alive = function(x,y) { var c = board[x][y], // state 0|1 n = 0; // neighbours if (isdefined(x-1,y-1)) n++; if (isdefined(x ,y-1)) n++; if (isdefined(x+1,y-1)) n++; if (isdefined(x-1,y )) n++; if (isdefined(x+1,y )) n++; if (isdefined(x-1,y+1)) n++; if (isdefined(x ,y+1)) n++; if (isdefined(x+1,y+1)) n++; if (n < 2 || n > 3) c = 0; // dead if (n === 3) c = 1; // alive return c }; var nextGen = function() { var next = []; for (var x = 0; x < xmax; x++) { next[x] = []; for (var y = 0; y < ymax; y++) next[x][y] = alive(x,y); } board = next }; var print = function(ctx,dw,dh) { for (var x = 0; x < xmax; x++) { for (var y = 0; y < ymax; y++) { ctx.fillStyle = (board[x][y])? "#fff" : "#888"; ctx.fillRect(y*dh+1,x*dw+1,dh-1,dw-1) } } }; var init = function (type,w,h) { xmax = w, ymax = h; for (var x = 0; x < xmax; x++) { board[x] = []; for (var y = 0; y < ymax; y++) { if (type === "random") { board[x][y] = Math.round(Math.random()); } else { board[x][y] = 0; if (x === Math.floor(w/2)) board[x][y] = 1; if (y === Math.floor(h/2)) board[x][y] = 1; } } } }; var run = function(ctx,dw,dh) { print(ctx,dw,dh); nextGen() }; var startstop = function(id,type) { var can = document.getElementById('life').getContext('2d'); delay = (type === "random")? 100 : 1000; if (interval === null) { id.value = "stop"; init(type,33,33); interval = window.setInterval( run, delay, can, 10, 10 ); } else { id.value = (type === "random")? "start_random" : "start_cross"; window.clearInterval( interval ); interval = null; } }; return { startstop } }) (); // end LIFE } {center {canvas {@ id="life" width="330" height="330" style="box-shadow:0 0 8px #000"}}{br} {input {@ type="button" value="start random" onclick="LIFE.startstop(this,'random')"}} {input {@ type="button" value="start cross" onclick="LIFE.startstop(this,'cross')"}} } _h2 '{lambda talk} {pre °° {center {input {@ type="button" value="start random" onclick="LIFE.startstop(this,'random')"}} {input {@ type="button" value="start cross" onclick="LIFE.startstop(this,'cross')"}} {canvas {@ id="life" width="330" height="330" style="box-shadow:0 0 8px #000"}} } °°} _h2 Javascript _p Following [[https://rosettacode.org/wiki/Conway%27s_Game_of_Life#JavaScript|https://rosettacode.org/wiki/Conway%27s_Game_of_Life#JavaScript]] {pre °° var LIFE = (function() { var board = [], xmax = 8, ymax = 8, interval = null, delay = 1000; var isdefined = function(x,y) { return typeof board[x] !== 'undefined' && typeof board[x][y] !== 'undefined' && board[x][y]; }; var alive = function(c,n) { if (n < 2 || n > 3) c = 0; if (n === 3) c = 1; return c }; var neighbours = function(x,y) { var n = 0; if (isdefined(x-1,y-1)) n++; if (isdefined(x ,y-1)) n++; if (isdefined(x+1,y-1)) n++; if (isdefined(x-1,y )) n++; if (isdefined(x+1,y )) n++; if (isdefined(x-1,y+1)) n++; if (isdefined(x ,y+1)) n++; if (isdefined(x+1,y+1)) n++; return n }; var nextGen = function() { var next = []; for (var x = 0; x < xmax; x++) { next[x] = []; for (var y = 0; y < ymax; y++) next[x][y] = alive( board[x][y], neighbours(x,y) ); } board = next }; var print = function(ctx,dw,dh) { for (var x = 0; x < xmax; x++) { for (var y = 0; y < ymax; y++) { ctx.fillStyle = (board[x][y])? "#fff" : "#888"; ctx.fillRect(y*dh+1,x*dw+1,dh-1,dw-1) } } }; var init = function (type,w,h) { xmax = w; ymax = h; delay = (type === "random")? 100 : 1000; for (var x = 0; x < xmax; x++) { board[x] = []; for (var y = 0; y < ymax; y++) { if (type === "random") { board[x][y] = Math.round(Math.random()); } else { board[x][y] = 0; if (x === Math.floor(w/2)) board[x][y] = 1; if (y === Math.floor(h/2)) board[x][y] = 1; } } } }; var run = function(ctx,dw,dh) { print(ctx,dw,dh); nextGen() }; var startstop = function(id,type) { var can = document.getElementById('life').getContext('2d'); if (interval === null) { id.value = "stop"; init(type,33,33); interval = window.setInterval( run, delay, can, 10, 10 ); } else { id.value = (type === "random")? "start_random" : "start_cross"; window.clearInterval( interval ); interval = null; } }; return { startstop } }) (); // end LIFE °°}
lambdaspeech v.20200126