xxxxxxxxxx
222
new p5();
// noprotect
///////////////////////////////////////////
//variables
let rows = 25;
let cols = 25;
let sam = 50;
let par;
let par2;
let grid = []
let fgrid = [];
let tgrid = [];
for(let i = 0; i < rows; i++){
grid[i] = [];
tgrid[i] = [];
fgrid[i] = [];
for(let j = 0; j < cols; j++){
grid[i][j] = 0;
tgrid[i][j] = 0;
fgrid[i][j] = 0;
}
}
for(let a = 0; a < sam; a++){
grid[floor(random(cols))][floor(random(rows))] = 1;
grid[floor(random(cols))][floor(random(rows))] = 2;
}
for(let i = 0; i < cols; i++){
for(let j = 0; j < rows; j++){
tgrid[i][j] = grid[i][j];
fgrid[i][j] = grid[i][j];
}
}
//rules
function calc(s, n, c){
if(s===1){
if(n < 2 || n > 3) return 0;
else return c;
}else{
if(n === 3) return c;
else return 0;
}
}
function getn(i, j){
let total = 0;
let red = 0;
let blue = 0;
if(i < cols-1){
if(j < rows-1){
if(grid[i+1][j+1] === 1) red++;
if(grid[i+1][j+1] === 2) blue++;
if(grid[i+1][j+1] > 0)total++;
}if(j > 0){
if(grid[i+1][j-1] === 1) red++;
if(grid[i+1][j-1] === 2) blue++;
if(grid[i+1][j-1] > 0)total++;
}
if(grid[i+1][j] === 1) red++;
if(grid[i+1][j] === 2) blue++;
if(grid[i+1][j] > 0)total++;
}if(i > 0){
if(j < rows-1){
if(grid[i-1][j+1] === 1) red++;
if(grid[i-1][j+1] === 2) blue++;
if(grid[i-1][j+1] > 0)total++;
}if(j > 0){
if(grid[i-1][j-1] === 1) red++;
if(grid[i-1][j-1] === 2) blue++;
if(grid[i-1][j-1] > 0)total++;
}
if(grid[i-1][j] === 1) red++;
if(grid[i-1][j] === 2) blue++;
if(grid[i-1][j] > 0)total++;
}
if(j < rows-1){
if(grid[i][j+1] === 1) red++;
if(grid[i][j+1] === 2) blue++;
if(grid[i][j+1] > 0)total++;
}if(j > 0){
if(grid[i][j-1] === 1) red++;
if(grid[i][j-1] === 2) blue++;
if(grid[i][j-1] > 0)total++;
}
if(red > blue) col = 1;
else col = 2;
return [total, col];
}
function setup() {
createCanvas(500, 500);
par = document.getElementById("par");
par2 = createP("");
par2.id("par2");
createButton("Calculate").mousePressed(function(){tick()});
frameRate(15);
}
function tick(){
for(let i = 0; i < cols; i++){
for(let j = 0; j < rows; j++){
switch(grid[i][j]){
case 0:
tgrid[i][j] = calc(0, getn(i, j)[0], getn(i, j)[1]);
break;
case 1:
tgrid[i][j] = calc(1, getn(i, j)[0], 1);
break;
case 2:
tgrid[i][j] = calc(1, getn(i, j)[0], 2);
break;
}
}
}
for(let x = 0; x < cols; x++){
for(let y = 0; y < rows; y++){
grid[x][y] = tgrid[x][y];
}
}
}
function draw() {
background(220);
for(let i = 0; i < cols; i++){
for(let j = 0; j < rows; j++){
switch(grid[i][j]){
case 0:
stroke(255, 10);
fill(0);
rect(i*width/cols, j*height/rows, width/cols-1, height/rows-1);
fgrid[i][j] = calc(0, getn(i, j)[0], getn(i, j)[1]);
noStroke();
switch(fgrid[i][j]){
case 0:
fill(0);
break;
case 1:
fill(255, 50, 50);
break;
case 2:
fill(50, 50, 255);
break;
}ellipse(i*width/cols+(width/cols)/2,j*height/rows+(height/rows)/2, width/cols/2, height/rows/2);
break;
case 1:
stroke(255, 10);
fill(255, 50, 50);
rect(i*width/cols, j*height/rows, width/cols-1, height/rows-1);
fgrid[i][j] = calc(1, getn(i, j)[0], 1);
noStroke();
switch(fgrid[i][j]){
case 0:
fill(0);
break;
case 1:
fill(255, 50, 50);
break;
case 2:
fill(50, 50, 255);
break;
}ellipse(i*width/cols+(width/cols)/2,j*height/rows+(height/rows)/2, width/cols/2, height/rows/2);
break;
case 2:
stroke(255, 10);
fill(50, 50, 255);
rect(i*width/cols, j*height/rows, width/cols-1, height/rows-1);
fgrid[i][j] = calc(1, getn(i, j)[0], 2);
noStroke();
switch(fgrid[i][j]){
case 0:
fill(0);
break;
case 1:
fill(255, 50, 50);
break;
case 2:
fill(50, 50, 255);
break;
}ellipse(i*width/cols+(width/cols)/2,j*height/rows+(height/rows)/2, width/cols/2, height/rows/2);
break;
}
}
}
//scoreing
let red = 0;
let blue = 0;
let id = "";
for(let x = 0; x < cols; x++){
for(let y = 0; y < rows; y++){
if(grid[x][y] === 1){
red++;
id+=",";
}else if(grid[x][y] === 2){
blue++;
id+=";";
}else{
id+=".";
}
}
}
par.innerHTML = ("Red: " + red + ", Blue: " + blue);
//document.getElementById("par2").innerHTML = id;
}
const mousePressed = ()=>{
if(mouseX > width || mouseX < 0 || mouseY > height || mouseY < 0) return;
let x = floor(mouseX/width*cols);
let y = floor(mouseY/height*rows);
if(mouseButton === LEFT) grid[x][y]++;
else grid[x][y]--;
if(grid[x][y] > 2) grid[x][y] = 0;
if(grid[x][y] < 0) grid[x][y] = 2;
return false;
}