xxxxxxxxxx
100
let cnvW = 600;
let cnvH = 600;
function make2DArray(cols, rows) {
let arr = new Array(cols);
for (let i = 0; i < arr.length; i++) {
arr[i] = new Array(rows);
}
return arr;
}
let title = "BEE SWEEPER";
let grid;
let cols;
let rows;
let w = 60;
let sWeight = 4;
let gameStatus;
function setup() {
gameStatus = createDiv(title+"<br>click to reveal, left click to mark");
gameStatus.style("font-size", "32px");
gameStatus.style("font-family", "Ubuntu");
gameStatus.style("margin", "0 auto");
gameStatus.style("min-height", "2.5em");
gameStatus.style("padding", "0.5em");
gameStatus.style("text-align", "center");
gameStatus.style("color", "#ffcc00");
gameStatus.style("background-color", "#000000");
let cnv = createCanvas(cnvW, cnvH);
cnv.position((windowWidth - width) / 2, (windowHeight - height) / 2);
cnv.elt.addEventListener("contextmenu", (e) => e.preventDefault());
cols = floor(width / w);
rows = floor(height / w);
grid = make2DArray(cols, rows);
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
grid[i][j] = new Cell(i, j, w);
}
}
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
grid[i][j].countBees();
}
}
}
function gameOver(status) {
let message = (status == "lost") ? "You were stung by a bee!" : "You Won!";
gameStatus.html(`${title}<br>${message}`);
console.log("YOU " + status.toUpperCase() + "!");
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
grid[i][j].marked = false;
grid[i][j].revealed = true;
}
}
noLoop();
}
function mousePressed() {
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
if (grid[i][j].contains(mouseX, mouseY)) {
if (mouseButton === LEFT) {
grid[i][j].reveal();
if (grid[i][j].bee) {
gameOver("lost");
return false;
}
} else {
grid[i][j].toggleMarked();
}
}
}
}
if(checkGrid()) {gameOver("won")};
}
let allCells = [];
function checkGrid() {
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
allCells.push(grid[i][j]);
}
}
let result = allCells.every(x => x.revealed || (x.marked && x.bee));
return result;
}
function draw() {
background(255);
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
grid[i][j].show();
}
}
}