xxxxxxxxxx
66
//https://en.wikipedia.org/wiki/Langton%27s_ant
var rows;
var cols;
var scl = 5;
var array = [];
var ant;
var direction = ['n','e','s','w'];
var spot;
function setup() {
createCanvas(400, 400);
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] = 255;
}
}
spot = 0;
ant = createVector(20,20,direction[spot]);
}
function draw() {
background(255);
var color;
if(array[ant.x][ant.y] == 40){
if(spot == 3){
spot = 0;
} else{
spot++;
}
color = 240;
} else{
if(spot == 0){
spot = 3;
}else{
spot--;
}
color = 40;
}
array[ant.x][ant.y] = color;
ant.z = direction[spot];
if(ant.z == 'n'){
ant.x ++;
}
if(ant.z == 'e'){
ant.y --;
}
if(ant.z == 's'){
ant.x --;
}
if(ant.z == 'w'){
ant.y ++;
}
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);
}
}
}