Skip to content

Instantly share code, notes, and snippets.

@bittercoder
Created December 7, 2012 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bittercoder/4232223 to your computer and use it in GitHub Desktop.
Save bittercoder/4232223 to your computer and use it in GitHub Desktop.
no-conditionals conways rules
var evalRules = function(isAlive, neighbours) {
var isAliveAfterTick = false;
var dead = function() {
isAliveAfterTick = false;
return true;
};
var live = function() {
isAliveAfterTick = true;
return true;
};
var twoOrLessDie = function() { return (neighbours <= 2 && isAlive && dead()); };
var threeOrFourLive = function() { return ((neighbours ==3 || neighbours == 4) && isAlive && live()); };
var fourOrMoreDie= function() { return (neighbours > 4 && isAlive && dead()); };
var bringToLifeIfThreeAndDead= function() { return (neighbours == 3 && !isAlive && live()); };
var discard = twoOrLessDie() || threeOrFourLive() || fourOrMoreDie() || bringToLifeIfThreeAndDead();
console.log("evalRules, wasLive: " + isAlive + ", neighbours: " + neighbours + ", aliveAfterTick? " + isAliveAfterTick);
return isAliveAfterTick;
}
// test out the rules
for (var i=0; i<=8; i++)
{
evalRules(false, i);
evalRules(true, i);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment