xxxxxxxxxx
69
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,255,255);
}
function draw() {
var won = true;
for (var r = 0; r < grid.length; r++)
for (var c = 0; c < grid[r] .length; c++)
if (grid[r][c] === true)
won = false;
if(won === true){
background(121,64,99)
fill(0)
stroke(0)
textAlign(CENTER);
textSize(40)
text("Yay, you won😄😁!!", 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(177,156,217);
else
fill(248,205,205);
rect(x, y, 80, 80);
}
}