xxxxxxxxxx
57
//https://en.wikipedia.org/wiki/Langton%27s_ant
"// noprotect"
var rows;
var cols;
var scl = 2;
var array = [];
var ants = [];
var step = 0;
var div;
function setup() {
createCanvas(400, 400);
background(255);
div = createDiv('');
rows = floor(width/scl);
cols = floor(height/scl);
for(var i = 0; i < rows; i++){
array[i] = [];
for(var j = 0; j < cols; j++){
array[i][j] = color(255,255,255);
}
}
append(ants,new Ant(40,40,color(255,0,0)));
append(ants,new Ant(rows-40,40,color(0,255,0)));
append(ants,new Ant(rows-40,cols-40,color(0,0,255)));
append(ants,new Ant(40,cols-40,color(255,255,0)));
}
function mousePressed(){
background(255);
for(var i = 0; i < rows; i++){
for(var j = 0; j < cols; j++){
fill(array[i][j]);
stroke(255);
rect(i * scl,j * scl,scl,scl);
}
}
}
function draw() {
for(var m = 0; m < 500; m++){
for(var i = 0; i < ants.length; i++){
var ant = ants[i];
array[ant.position.x][ant.position.y] = ant.findColor(array);
ant.move(cols,rows);
}
}
step++;
stroke(0);
noFill();
div.html("Steps: " + step);
}