Click to reveal all blocks that are not bombs. You click on a bomb, you die. You find all the blocks that are not a bomb, you win. Numbers indicate the number of bombs around each block. Use it to strategize. Press 'F' to flag any block that you suspect as a bomb.
A fork of Emojisweeper by Sinan Ascioglu
xxxxxxxxxx
let rows = 10;
let cols = rows;
let cellW = 40;
let cellH = cellW;
let cells = [];
let mineToCellRatio = 0.15; //increase it to make it harder
//emojis used
const CELL = '🌁';
const EMPTY = '🟧';
const MINE = '💣';
const PIN = '📍';
const DIGITS = ['⬜️', '1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣', '6️⃣', '7️⃣', '8️⃣'];
function setup() {
background(255);
createCanvas(cellW * rows, cellH * cols);
textSize(cellH - 1);
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
let newCell = new Cell(i, j);
//decide whether it is a mine or not
newCell.mine = Math.random(0, 1) < mineToCellRatio;
cells.push(newCell);
}
}
//set mines around each cell
cells.forEach(c => {
//find neighboring cells
let neighbors = getNeighbors(c);
let reducer = (accumulator, currentValue) => accumulator + currentValue;
c.minesAround = neighbors.map(n => n.mine).reduce(reducer); //add all mine values to find total
});
}
function draw() {
background(255);
translate(1, cellH - 3);
cells.forEach(function(c) {
c.draw();
});
}
function getNeighbors(cell) {
return cells.filter(n => {
return (n.i >= cell.i - 1) && (n.i <= cell.i + 1) && (n.j >= cell.j - 1) && (n.j <= cell.j + 1);
});
}
function revealCell(cell) {
cell.revealed = true;
if (cell.mine) { //end game
cells.forEach(c => {
c.revealed = true;
});
noLoop();
return;
}
if (cell.minesAround == 0) { //recursively reveal neighbors
let neighbors = getNeighbors(cell);
neighbors.forEach(n => {
if (!n.revealed) {
revealCell(n);
}
});
}
}
function gameWon(){
DIGITS[0] = '😃';
cells.forEach(function(c) {
c.revealed = true;
});
}
function gameLost(){
DIGITS[0] = '😱';
cells.forEach(function(c) {
c.revealed = true;
});
}
function mousePressed() {
//find the cell pressed on
let cell = cells.find(c => {
return (c.x < mouseX) && (c.x + cellW > mouseX) && (c.y < mouseY) && (c.y + cellH > mouseY);
});
if (cell) {
if (cell.pinned) {
return; //do not allow revealing
}
revealCell(cell);
if(cell.mine){
gameLost();
}else{
//check if game is won
let cellsLeft = cells.filter(c => {
return !c.mine && !c.revealed;
}).length;
if(cellsLeft == 0){
gameWon();
}
}
}
}
function keyPressed() {
if (key == 'f') {
//find the cell pressed on
let cell = cells.find(c => {
return (c.x < mouseX) && (c.x + cellW > mouseX) && (c.y < mouseY) && (c.y + cellH > mouseY);
});
if (cell) {
cell.pinned = !cell.pinned;
}
}
}