Created
December 7, 2012 09:55
-
-
Save bittercoder/4232223 to your computer and use it in GitHub Desktop.
no-conditionals conways rules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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