Conway's Game of Life: Difference between revisions
Content added Content deleted
(→Red) |
(→{{header|Lambdatalk}}: adding lambdatalk task) |
||
Line 9,452: | Line 9,452: | ||
..........##.................................................................... |
..........##.................................................................... |
||
</pre> |
</pre> |
||
=={{header|Lambdatalk}}== |
|||
Lambdatalk works in any web browsers coming with javascript and canvas. |
|||
<syntaxhighlight lang="scheme"> |
|||
{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"}} |
|||
} |
|||
calling the following javascript code |
|||
{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(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 |
|||
} |
|||
</syntaxhighlight> |
|||
Results can be seen in http://lambdaway.free.fr/lambdaspeech/?view=life |
|||
and in http://lambdaway.free.fr/lambdawalks/?view=conway |
|||
=={{header|Lua}}== |
=={{header|Lua}}== |