I am making mine sweeper using the p5js editor but when I run the game it will crash if I click a block that has 0 surrounding bombs, or if I click a bomb which didn't happen till I added the part with tiles with 0 surrounding bombs. everything was working fine up till i was making it to where clicking on a tile with no surrounding bombs will make all connected tiles reveal them self. however it give me the error and then will reveal random tiles, along with breaking others by removing the number.
Run below code here
var amountOfBombs = 0;
var remainingSpaces = 100;
var hasLost = false;
var baseGrid = [];
baseGrid[0] = [1, 0, 1, 0, 1, 0, 1, 0, 1, 0];
baseGrid[1] = [0, 1, 0, 1, 0, 1, 0, 1, 0, 1];
baseGrid[2] = [1, 0, 1, 0, 1, 0, 1, 0, 1, 0];
baseGrid[3] = [0, 1, 0, 1, 0, 1, 0, 1, 0, 1];
baseGrid[4] = [1, 0, 1, 0, 1, 0, 1, 0, 1, 0];
baseGrid[5] = [0, 1, 0, 1, 0, 1, 0, 1, 0, 1];
baseGrid[6] = [1, 0, 1, 0, 1, 0, 1, 0, 1, 0];
baseGrid[7] = [0, 1, 0, 1, 0, 1, 0, 1, 0, 1];
baseGrid[8] = [1, 0, 1, 0, 1, 0, 1, 0, 1, 0];
baseGrid[9] = [0, 1, 0, 1, 0, 1, 0, 1, 0, 1];
baseGrid[10] = [1, 0, 1, 0, 1, 0, 1, 0, 1, 0];
var grid = [];
grid[0] = [1, 0, 1, 0, 1, 0, 1, 0, 1, 0];
grid[1] = [0, 1, 0, 1, 0, 1, 0, 1, 0, 1];
grid[2] = [1, 0, 1, 0, 1, 0, 1, 0, 1, 0];
grid[3] = [0, 1, 0, 1, 0, 1, 0, 1, 0, 1];
grid[4] = [1, 0, 1, 0, 1, 0, 1, 0, 1, 0];
grid[5] = [0, 1, 0, 1, 0, 1, 0, 1, 0, 1];
grid[6] = [1, 0, 1, 0, 1, 0, 1, 0, 1, 0];
grid[7] = [0, 1, 0, 1, 0, 1, 0, 1, 0, 1];
grid[8] = [1, 0, 1, 0, 1, 0, 1, 0, 1, 0];
grid[9] = [0, 1, 0, 1, 0, 1, 0, 1, 0, 1];
grid[10] = [1, 0, 1, 0, 1, 0, 1, 0, 1, 0];
function setFeild() {
for (let y = 0; y < grid.length; y++) {
for (let x = 0; x < grid[y].length; x++) {
if (grid[y][x] == 1) {
let block = new Block(x * 40, y * 40, 40, "green");
block.SelectBomb();
if (block.isBomb == true) {
block = new Block(x * 40, y * 40, 40, "green");
amountOfBombs += 1;
}
grid[y][x] = block;
block.draw();
} else if (grid[y][x] == 0) {
let block = new Block(x * 40, y * 40, 40, "lime");
block.draw();
block.SelectBomb();
if (block.isBomb == true) {
block = new Block(x * 40, y * 40, 40, "lime");
amountOfBombs += 1;
}
grid[y][x] = block;
block.draw();
}
}
}
}
function setup() {
createCanvas(400, 400);
setFeild();
}
class Block {
constructor(x, y, scale, color) {
this.x = x;
this.y = y;
this.scale = scale;
this.color = color;
this.isBomb = true;
this.nearBombs = 0;
this.hasCounted = false;
}
draw() {
fill(this.color);
noStroke();
rect(this.x, this.y, this.scale, this.scale);
}
countBombs() {
if (this.isBomb == false) {
if (this.x != 360 && this.y != 360) {
if (grid[this.y / 40 + 1][this.x / 40 + 1].isBomb == true) {
this.nearBombs++;
}
}
if (this.x != 0 && this.y != 0) {
if (grid[this.y / 40 - 1][this.x / 40 - 1].isBomb == true) {
this.nearBombs++;
}
}
if (this.x != 0 && this.y != 360) {
if (grid[this.y / 40 + 1][this.x / 40 - 1].isBomb == true) {
this.nearBombs++;
}
}
if (this.x != 360 && this.y != 0) {
if (grid[this.y / 40 - 1][this.x / 40 + 1].isBomb == true) {
this.nearBombs++;
}
}
if (this.y != 0) {
if (grid[this.y / 40 - 1][this.x / 40].isBomb == true) {
this.nearBombs++;
}
}
if (this.y != 360) {
if (grid[this.y / 40 + 1][this.x / 40].isBomb == true) {
this.nearBombs++;
}
}
if (this.x != 360) {
if (grid[this.y / 40][this.x / 40 + 1].isBomb == true) {
this.nearBombs++;
}
}
if (this.x != 0) {
if (grid[this.y / 40][this.x / 40 - 1].isBomb == true) {
this.nearBombs++;
}
}
this.hasCounted = true;
}
}
reveal(placement) {
if (this.isBomb == true) {
if (hasLost == false) {
hasLost = true;
for (var y = 0; y < grid.length; y++) {
for (var x = 0; x < grid[y].length; x++) {
grid[y][x].reveal(baseGrid[y][x]);
}
}
}
if (placement == 1) {
this.color = "#de0012";
} else {
this.color = "#e04652";
}
this.draw();
} else if (this.isBomb == false) {
if (placement == 1) {
this.color = "#d19f21";
} else {
this.color = "#edc86b";
}
if (this.hasCounted == false) {
this.countBombs();
}
if (this.nearBombs == 0) {
if (this.x != 360 && this.y != 360) {
if (grid[this.y / 40 + 1][this.x / 40 + 1].isBomb == false) {
grid[this.y / 40 + 1][this.x / 40 + 1].reveal(baseGrid[this.y / 40 + 1][this.x / 40 + 1]);
}
}
if (this.x != 0 && this.y != 0) {
if (grid[this.y / 40 - 1][this.x / 40 - 1].isBomb == false) {
grid[this.y / 40 - 1][this.x / 40 - 1].reveal(baseGrid[this.y / 40 - 1][this.x / 40 - 1]);
}
}
if (this.x != 0 && this.y != 360) {
if (grid[this.y / 40 + 1][this.x / 40 - 1].isBomb == false) {
grid[this.y / 40 + 1][this.x / 40 - 1].reveal(baseGrid[this.y / 40 + 1][this.x / 40 - 1]);
}
}
if (this.x != 360 && this.y != 0) {
if (grid[this.y / 40 - 1][this.x / 40 + 1].isBomb == false) {
grid[this.y / 40 - 1][this.x / 40 + 1].reveal(baseGrid[this.y / 40 - 1][this.x / 40 + 1]);
}
}
if (this.y != 0) {
if (grid[this.y / 40 - 1][this.x / 40].isBomb == false) {
grid[this.y / 40 - 1][this.x / 40].reveal(baseGrid[this.y / 40 - 1][this.x / 40]);
}
}
if (this.y != 360) {
if (grid[this.y / 40 + 1][this.x / 40].isBomb == false) {
grid[this.y / 40 + 1][this.x / 40].reveal(baseGrid[this.y / 40 + 1][this.x / 40]);
}
}
if (this.x != 360) {
if (grid[this.y / 40][this.x / 40 + 1].isBomb == false) {
grid[this.y / 40][this.x / 40 + 1].reveal(baseGrid[this.y / 40][this.x / 40 + 1]);
}
}
if (this.x != 0) {
if (grid[this.y / 40][this.x / 40 - 1].isBomb == false) {
grid[this.y / 40][this.x / 40 - 1].reveal(baseGrid[this.y / 40][this.x / 40 - 1]);
}
}
}
this.draw();
fill("black");
textSize(30);
if (this.nearBombs != 0) {
text(this.nearBombs.toString(), this.x + 10, this.y + 30);
}
}
if (hasLost == true) {
fill("white");
textSize(10);
text("YOU LOSE", 10, 390);
}
}
SelectBomb() {
this.ran = random(1, 100);
if (this.ran <= 15) {
this.isBomb = true;
} else {
this.isBomb = false;
}
}
}
function mousePressed() {
for (var y = 0; y < grid.length; y++) {
for (var x = 0; x < grid[y].length; x++) {
if (mouseX >= grid[y][x].x && mouseX < grid[y][x].x + grid[y][x].scale && mouseY >= grid[y][x].y && mouseY < grid[y][x].y + grid[y][x].scale) {
grid[y][x].reveal(baseGrid[y][x]);
remainingSpaces--;
if (remainingSpaces <= amountOfBombs) {
fill("white");
textSize(20);
text("YOU WIN", 10, 390);
}
}
}
}
}