Skip to content

Instantly share code, notes, and snippets.

@jeantil
Created February 24, 2011 18:13
Show Gist options
  • Select an option

  • Save jeantil/842590 to your computer and use it in GitHub Desktop.

Select an option

Save jeantil/842590 to your computer and use it in GitHub Desktop.
dojo softeam 3
<!DOCTYPE html>
<html>
<head>
<title>Game Of Life</title>
<script src="lib/processing.js"></script>
<script src="gameoflife.js"></script>
<!-- tests -->
<script src="lib/jquery.js"></script>
<script src="test/qunit.js"></script>
<link href="test/qunit.css" rel="stylesheet"/>
<script src="test/test.js"></script>
</head>
<body style="background-color:black">
<div style="width:400px;float:right;">
<h1 id="qunit-header"></h1>
<ol id="qunit-tests"></ol>
</div>
<button id="start">Start</button><br/>
<canvas id="cv" width="300px" height="300px" style="border:1px solid grey"></canvas>
</body>
</html>
var game={
pixels:['0100','0010','1110','0000'],
init:function(p){
this.pixels=p;
},
step:function(){
var next=['0000','0000','0000','0000'];
for( y=0; y<3; y++ ){
for( x=0; x<3; x++ ){
var n = this.neighbor(x,y);
switch(n){
case 2 :
if( on(x,y)) next[y][x]= 1;break;
case 3 :
if(!on(x,y)) next[y][x]= 1;break;
}
}
}
this.pixels = next;
},
neighbor:function(x,y){
var n=0
var w=3
var h=3
left = x==0 ? w : x-1 // if we are on a edge (0), we go to the other side of the grid
right= x==w ? 0 : x+1
up = y==0 ? h : y-1
down = y==h ? 0 : y+1
if (on( left , up )) n++
if (on( x , up )) n++
if (on( right, up )) n++
if (on( left , y )) n++
if (on( right, y )) n++
if (on( left , down )) n++
if (on( x , down )) n++
if (on( right, down )) n++
return n;
}
}
function on(x,y){
return (game.pixels[y][x]==='1') ? true:false
}
window.onload=function(){
$('#start').click(function(){
var timer = setInterval(function(){
if ( game.tour < 21 ) game.step()
else clearInterval( timer )
}, 500);
})
}
$(document).ready(function(){
test("Game of life",function(){ok(true)})
test("Test Init",function(){game.init(['0100','0010','1110','0000'])})
test("Test Pixels",function(){
game.init(['0100','0010','1110','0000'])
same(['0100','0010','1110','0000'],game.pixels)
})
test("Test Pixels diff",function(){
game.init(['0100','0000','1110','0000'])
same(['0100','0000','1110','0000'],game.pixels)
})
// Jean lève le point : que se passet'il si on applique step sasn init avant
test("Test next Step",function(){game.step()})
test("A lonely cell dies",function(){
game.init(['1000','0000','0000','0000']);
game.step();
same(['0000','0000','0000','0000'],game.pixels)
})
// Question de Jean : a-t'on vraiment besoin d'un Dieu pour définir les règles?
// Philippe : j'ai une solution en tête qui implique d'exposer publiquement les voisines d'une cellule
// On peut prendre une autre voie, mais on n'a plus de direction
// il reste un quart d'heure, on passe en mode turbo
// Tout le monde constate : Le test "Voisins" est un trop gros pas
test("A cell has neighbors",function(){
same(game.neighbor(0,0),0)
})
test("The 0,0 cell has 1 neighbor",function(){
game.init(['0100','0000','0000','0000']);
same(game.neighbor(0,0),1)
})
test("A dead cell with 3 neighbors, lives",function(){
game.init(['1100','1000','0000','0000']);
game.step();
same(game.pixels,['1100','1100','0000','0000'])
})
//test("Test de recette (en utilisant un vaisseau)", function(){
//game.init(['0100','0010','1110','0000']);
//game.step();
//same(game.pixels,['0000','1010','0110','0100'])})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment