xxxxxxxxxx
181
new p5();
// noprotect
///////////////////////////////////////////
//variables
var rows = 40;
var cols = 40;
var sam = 25;
var par;
var grid = []
var fgrid = [];
var tgrid = [];
var states = [0,1];
var chance = [100,100];
var starting = states;
var clock;
var GOL = [getnGOL, calcGOL];
var NB = [getnGOL, calcNB];
var IGOL = [getnGOL, calcIGOL];
var TEST = [getnGOL, calcTEST];
var RAND = [function(){}, calcRAND];
var current = NB.slice();
var selalg;
var col = [0,255,0];
for(var i = 0; i < rows; i++){
grid[i] = [];
tgrid[i] = [];
fgrid[i] = [];
for(var j = 0; j < cols; j++){
//grid[i][j] = starting[floor(random(starting.length))];
var ind = floor(random(starting.length));
while(random(100)>chance[ind]){
ind = floor(random(starting.length));
}
grid[i][j] = starting[ind];
tgrid[i][j] = grid[i][j];
fgrid[i][j] = grid[i][j];
}
}
//rules
function calcGOL(s, n){
var r = s;
if(s===1 && (n<2 || n>3)) r = 0;
if(s===0 && (n===3)) r = 1;
return r;
}
function getnGOL(i, j){
var total = 0;
total+=grid[(i+1) % rows][j];
total+=grid[(i+1) % rows][(j+1) % cols];
total+=grid[(i+1) % rows][(j-1+cols) % cols];
total+=grid[(i-1+rows) % rows][j];
total+=grid[(i-1+rows) % rows][(j+1) % cols];
total+=grid[(i-1+rows) % rows][(j-1+cols) % cols];
total+=grid[i][(j+1) % cols];
total+=grid[i][(j-1+cols) % cols];
//total+=grid[floor(random(rows))][floor(random(cols))];
/*total+=grid[jx][jy];
jx = (jx + i) % rows;
jy = (jy + j) % cols;*/
return total;
}
function calcNB(s, n){
var r = s;
if(n<s+1 || n>s+2) r = 0;
if(n===s+3) r = 1;
return r;
}
function calcIGOL(s, n){
var r = s;
if(n<2 || n>3) r = 1-r;
if(n===3) r = 0;
return r;
}
function calcRAND(){
return states[floor(random(states.length))];
}
function calcTEST(s, n){
var r = s;
r += (n - s);
while(r > 1) r -= 2;
return r;
}
function setup() {
createCanvas(500, 500);
par = document.getElementById("par");
createP("");
createButton("Calculate").mousePressed(tick);
createButton("Toggle Clock").mousePressed(toggleclock);
createButton("Clear").mousePressed(clearGrid);
createP("");
selalg = createRadio();
selalg.option("Game of Life", "current = GOL.slice();");
selalg.option("Maze Generator", "current = NB.slice();");
selalg.option("Inverted Game of Life", "current = IGOL.slice();");
selalg.option("Test", "current = TEST.slice();");
selalg.option("Randomize", "current = RAND.slice();");
frameRate(15);
}
function tick(){
for(var i = 0; i < cols; i++){
for(var j = 0; j < rows; j++){
tgrid[i][j] = current[1](grid[i][j], current[0](i,j));
}
}
for(var x = 0; x < cols; x++){
for(var y = 0; y < rows; y++){
grid[x][y] = tgrid[x][y];
}
}
}
function draw() {
background(220);
eval(selalg.value());
for(var i = 0; i < cols; i++){
for(var j = 0; j < rows; j++){
fill(col[0]*grid[i][j]+25,col[1]*grid[i][j]+25,col[2]*grid[i][j]+25);
stroke(255,10);
rect(i*width/cols, j*height/rows, width/cols-1, height/rows-1);
fgrid[i][j] = current[1](grid[i][j], current[0](i, j));
fill(col[0]*fgrid[i][j]+25,col[1]*fgrid[i][j]+25,col[2]*fgrid[i][j]+25);
noStroke();
ellipse(i*width/cols+(width/cols)/2,j*height/rows+(height/rows)/2, width/cols/2, height/rows/2);
}
}
//scoreing
var sarr = [];
var id = "";
for(var x = 0; x < cols; x++){
for(var y = 0; y < rows; y++){
for(var z = 0; z < states.length; z++){
if(sarr[z] === undefined) sarr[z] = 0;
if(states[z] === grid[x][y]){
sarr[z]++;
break;
}
}
}
}
par.innerHTML = "";
for(var arrpos = 0; arrpos < sarr.length; arrpos++){
if(sarr[arrpos]!==0){
par.innerHTML+=states[arrpos] + ":" + sarr[arrpos] + " ";
}
}
}
function mousePressed(){
if(mouseX > width || mouseX < 0 || mouseY > height || mouseY < 0) return;
var x = floor(mouseX/width*rows);
var y = floor(mouseY/height*cols);
var ind = states.indexOf(grid[x][y]);
ind++;
if(ind===states.length) ind = 0;
grid[x][y] = states[ind];
return false;
}
function toggleclock(){
if(clock){
clearInterval(clock);
clock=undefined;
}else{
clock = setInterval(tick,50);
}
}
function clearGrid(){
for(var i = 0; i < rows; i++){
for(var j = 0; j < rows; j++){
grid[i][j] = 0;
}
}
}