xxxxxxxxxx
66
var grid = [
[false, false, false, false, false],
[false, false, false, false, false],
[true, false, true, false, true],
[false, false, false, false, false],
[false, false, false, false, false]
];
function setup() {
createCanvas(400, 400);
stroke(255);
}
function draw() {
let won = true;
for(let r = 0; r < grid.length; r++)
for(let c = 0; c < grid[r].length; c++)
if(grid[r][c] === true)
won = false;
if(won === true){
background(72,61,139);
fill(0);
textAlign(CENTER);
textSize(24);
text("You Win!", 200, 200);
}else
drawGrid();
}
function mousePressed() {
let col = int(mouseX / 80);
let row = int(mouseY / 80);
flipLight(row, col);
if(isValid(row - 1, col) === true)
flipLight(row - 1, col);
if(isValid(row + 1, col) === true)
flipLight(row + 1, col);
if(isValid(row, col - 1) === true)
flipLight(row, col - 1);
if(isValid(row, col + 1) === true)
flipLight(row, col + 1);
}
function flipLight(r, c){
grid[r][c] = !grid[r][c];
}
function isValid(r, c){
if(r < 0)
return false;
if(r > 4)
return false;
if(c < 0)
return false;
if(c > 4)
return false;
return true;
}
function drawGrid(){
for (var r = 0; r < grid.length; r++)
for (var c = 0; c < grid[r].length; c++) {
let x = c * 80;
let y = r * 80;
if (grid[r][c] === false)
fill(72,61,139);
else
fill(224,255,255);
rect(x, y, 80, 80);
}
}