xxxxxxxxxx
79
let grid = [];
let colors = [];
function setup() {
createCanvas(400, 400);
textAlign(CENTER, CENTER);
textSize(width/16);
scramble();
}
function scramble(){
colors = [];
for(let i = 0; i < floor(random(14)) + 2; i++) colors.push([floor(random(256)), floor(random(256)), floor(random(256))]);
grid = [];
for(let i = 0; i < 8; i++){
grid[i] = [];
for(let j = 0; j < 8; j++){
grid[i][j] = floor(random(colors.length));
}
}
}
function draw() {
background(0);
//translate(1, 1);
//scale(1 - 2/width, 1 - 2/height);
stroke(0);
for(let i = 0; i < 8; i++){
for(let j = 0; j < 8; j++){
fill(colors[grid[i][j]]); strokeWeight(1);
rect(j * width/8, i * height/8, width/8, height/8);
fill(255); strokeWeight(3);
text(grid[i][j] + "", j * width/8 + width/16, i * height/8 + height/16);
}
}
}
function mousePressed(e){
const mj = floor(mouseX*8/width), mi = floor(mouseY*8/height);
if(mi < 0 || mi > 7 || mj < 0 || mj > 7) return /*false*/;
if(e.button > 0) incR(mj);
if(e.button < 2) incC(mi);
if(e.button === 1) add(mi, mj, -1);
//return false;
}
function keyPressed(){
scramble();
return false;
}
function incR(mj){
for(let i = 0; i < 8; i++) add(i, mj);
}
function incC(mi){
for(let j = 0; j < 8; j++) add(mi, j);
}
function add(i, j, x = 1){
grid[i][j] = mod(grid[i][j] + x, colors.length);
}
function mod(a, b){
return ((a % b) + b) % b;
}