Last active
December 4, 2015 09:54
-
-
Save pprzetacznik/e0269dc3892f17707740 to your computer and use it in GitHub Desktop.
ant.js
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
function anonymous() { | |
var world = new CAWorld({ | |
width: 192, | |
height: 128, | |
cellSize: 3 | |
}); | |
world.palette = [ | |
'68, 36, 52, 1', | |
'255, 255, 255, 1' | |
]; | |
var ant = { | |
x: world.width/2, | |
y: world.height/2, | |
direction: 0, | |
moved: false, | |
} | |
world.registerCellType('living', { | |
getColor: function () { | |
return this.alive ? 0 : 1; | |
}, | |
process: function (neighbors) { | |
if (this.x == ant.x && this.y == ant.y && !ant.moved) { | |
if (this.alive) { | |
this.alive = false; | |
ant.direction += 1; | |
if (ant.direction > 3) | |
ant.direction = 0; | |
} else { | |
this.alive = true; | |
ant.direction -= 1; | |
if (ant.direction < 0) | |
ant.direction = 3; | |
} | |
switch(ant.direction) { | |
case 0: | |
ant.x -= 1; | |
if (ant.x < 0) | |
ant.x = world.width - 1; | |
break; | |
case 1: | |
ant.y -= 1; | |
if (ant.y < 0) | |
ant.y = world.height - 1; | |
break; | |
case 2: | |
ant.x += 1; | |
if (ant.x >= world.width) | |
ant.x = 0; | |
break; | |
case 3: | |
ant.y += 1; | |
if (ant.y >= world.height) | |
ant.y = 0; | |
break; | |
} | |
ant.moved = true; | |
} | |
}, | |
reset: function () { | |
ant.moved = false; | |
} | |
}, function () { | |
//init | |
this.alive = Math.random() > 1; | |
}); | |
world.initialize([ | |
{ name: 'living', distribution: 100 } | |
]); | |
return world; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment