xxxxxxxxxx
223
class Element {
constructor(x, y) {
this.x = x;
this.y = y;
this.state = 'sq';
this.n = 0;
this.flaged = false;
}
draw(s) {
let lx = this.x * 50 + 25;
let ly = this.y * 50 + 25;
if (s == 'crf') {
push();
fill(220, 0, 0);
noStroke();
ellipse(lx, ly, 35);
pop();
} else if (s == 'sqf') {
push();
fill(220);
noStroke();
rect(lx, ly, 40);
fill(0, 0, 230);
text(this.n, lx, ly);
pop();
} else {
rect(lx, ly, 40);
}
if (this.flaged) {
push();
fill(230, 0, 0);
noStroke();
triangle(lx - 5, ly - 15, lx + 8, ly - 8, lx - 5, ly);
stroke(230, 0, 0);
line(lx - 4, ly - 1, lx - 4, ly + 10);
pop();
}
}
update(s) {
this.state = s;
}
mouseIn() {
let lx = this.x * 50 + 25;
let ly = this.y * 50 + 25;
return (abs(mouseX - lx) <= 20 && abs(mouseY - ly) <= 20);
}
}
let arr = [];
let barr = []
let timer = 0;
let timerSet = false;
let isfail = iswin = false;
let flagNo = 0;
function setup() {
createCanvas(350, 400);
background(255);
noFill();
stroke(0);
strokeWeight(2);
rectMode(CENTER);
textSize(20);
textAlign(CENTER, CENTER)
for (let y = 0; y < 8; y++) {
for (let x = 0; x < 7; x++) {
arr.push(new Element(x, y));
}
}
setBomb();
}
function draw() {
background(255);
for (let i = 0; i < arr.length; i++) {
arr[i].draw(arr[i].state);
}
for (let i = 0; i < arr.length; i++) {
if (arr[i].state == 'crf') {
isfail = true;
break;
}
}
if (isfail) {
fail();
return;
}
let tem = true;
for (let i = 0; i < barr.length; i++) {
if (!arr[barr[i]].flaged) {
tem = false;
console.log(barr[i])
break;
}
}
if (tem && barr.length == flagNo) {
iswin = true;
}
if (iswin) {
win();
return;
}
}
function mouseClicked(e) {
return false;
}
function mouseReleased() {
if (mouseButton == LEFT) {
for (let y = 0; y < 8; y++) {
for (let x = 0; x < 7; x++) {
if (arr[x + y * 7].mouseIn() && !arr[x + y * 7].flaged) {
if (arr[x + y * 7].state == 'sq') {
arr[x + y * 7].update('sqf');
} else if (arr[x + y * 7].state == 'cr') {
arr[x + y * 7].update('crf');
}
}
}
}
}
if (mouseButton == RIGHT) {
for (let y = 0; y < 8; y++) {
for (let x = 0; x < 7; x++) {
if (!arr[x + y * 7].state.includes('f')) {
if (arr[x + y * 7].mouseIn() && !arr[x + y * 7].flaged) {
arr[x + y * 7].flaged = true;
flagNo ++;
} else if (arr[x + y * 7].mouseIn() && arr[x + y * 7].flaged) {
arr[x + y * 7].flaged = false;
flagNo --;
}
}
}
}
}
return false;
}
function setBomb() {
let n = floor(random(4, 7));
let picked = [];
while (picked.length < n) {
let index = floor(random(arr.length));
while (picked.includes(index)) {
index = floor(random(arr.length));
}
picked.push(index);
arr[index].update('cr');
barr.push(index);
}
for (let y = 0; y < 8; y++) {
for (let x = 0; x < 7; x++) {
for (let xx = 0; xx < 7; xx++) {
if (arr[xx + y * 7].state == 'cr') {
arr[x + y * 7].n++;
}
}
for (let yy = 0; yy < 8; yy++) {
if (arr[x + yy * 7].state == 'cr') {
arr[x + y * 7].n++;
}
}
}
}
}
function win() {
if (!timerSet) {
timer = millis();
timerSet = true;
}
if (millis() - timer < 2000) {
push();
textSize(150);
fill(0)
text('WIN', width / 2, height / 2);
pop();
} else {
reset();
}
}
function fail() {
if (!timerSet) {
timer = millis();
timerSet = true;
}
if (millis() - timer < 2000) {
push();
textSize(150);
fill(230, 0, 0);
noStroke();
text('FAIL', width / 2, height / 2);
pop();
} else {
reset();
}
}
function reset() {
arr.length = 0;
timer = 0;
timerSet = false;
isfail = iswin = false;
barr.length = 0;
flagNo = 0;
for (let y = 0; y < 8; y++) {
for (let x = 0; x < 7; x++) {
arr.push(new Element(x, y));
}
}
setBomb();
}