xxxxxxxxxx
377
/** "MySweep" --AKA MineSweeper
*
* @Author: Allen Thoe
* Use the LEFT mouse button only (in case using Chrome--No RIGHT CLICK)
* Enjoy!!!
*/
class Cell {
constructor(x, y){
this.x = x;
this.y = y;
this.count = 0;
this.isRevealed = false;
this.isMine = false;
this.isFlagged = false;
}
show(){
if(this.isMine){
fill(0);
} else if(this.isFlagged){
fill(255,0,0);
} else {
fill(255);
}
rect(this.x*w, this.y*w, w, w);
fill(0,0,255);
if(!this.isMine && this.count != 0){
textSize(w);
if(this.count == 1){
fill(20,20,200);
} else if (this.count == 2){
fill(20, 200, 20);
} else if (this.count == 3){
fill(200,20,20);
} else if (this.count == 4){
fill(200, 20, 200);
} else if (this.count == 5){
fill(20, 200, 100);
} else {
fill(150, 20, 200);
}
text(this.count, (this.x+0.25)*w, (this.y+0.85)*w);
}
}
}
//END OF CLASS CELL
var w, cell, numMines, numCells;
var flag;
var score;
var end;
var firstPress;
var level;
var gameOn;
function setup() {
createCanvas(500, 700);
numCells = 10;
numMines = int(random(3*numCells,4*numCells));
score = -numMines;
w = width/numCells;
cell = makeCells(numCells, numCells);
fillCells();
placeMines();
flag = false;
end = 0;
firstPress = true;
}
function draw() {
if(gameOn){
background(220);
showCells();
hideCells();
fill(255,255,0);
rect(0,height*600/700, width, 100);
fill(0);
textSize(30*500/width);
text("CLICK HERE TO CREATE FLAG", width/2-225*500/width, height-40*700/height)
text("CLICK HERE TO CREATE FLAG", width/2-224*500/width, height-41*700/height)
text("CLICK HERE TO CREATE FLAG", width/2-223*500/width, height-42*700/height)
textSize(24*500/width);
fill(255,0,0);
text("***Flagged cells can still be checked***", width/2-215*500/width, height-140*700/height);
showFlag();
endGame(end);
} else {
startScreen();
}
}
function touchEnded(){
if(gameOn){
var a = int(mouseX/w);
var b = int(mouseY/w);
if(firstPress && b < cell[0].length){
for(var i = -1; i < 2; i++){
for(var j = -1; j < 2; j++){
cell[a+i][b+j].isMine = false;
}
}
countMines();
firstPress = false;
}
if(b < cell[0].length){
if(flag){
if(cell[a][b].isMine && !cell[a][b].isFlagged){
score++;
} else if(!cell[a][b].isMine && !cell[a][b].isFlagged){
score--;
}
cell[a][b].isFlagged = true;
} else {
cell[a][b].isFlagged = false;
checkCell(a,b);
}
} else {
if(flag){
flag = false;
} else {
flag = true;
}
}
} else {
if(mouseX < width/3){
level = 1;
gameOn = true;
} else if(mouseX < 2*width/3){
level = 2;
gameOn = true;
} else {
level = 3;
gameOn = true;
}
reStart();
}
}
function startScreen(){
background(0);
fill(255,255,0);
rect(0,100*700/height, width, 100*700/height);
fill(0);
textSize(50*500/width);
text("Welcome to MySweep!" , 3, 170*700/height)
text("Welcome to MySweep!" , 2, 171*700/height)
text("Welcome to MySweep!" , 1, 172*700/height)
fill(255,255,0);
rect(0,240*700/height, width, 80*700/height);
fill(255,0,0);
rect(0,260*700/height, width, 40*700/height);
fill(0);
textSize(32*500/width);
text("SELECT YOUR LEVEL!" , 105*500/width, 290*700/height)
text("SELECT YOUR LEVEL!" , 104*500/width, 291*700/height)
text("SELECT YOUR LEVEL!" , 103*500/width, 292*700/height)
fill(0,155,0);
rect(0,height/2, width/3-30*500/width, 40*700/height);
fill(0);
textSize(30*500/width);
text("EASY", 35*500/width, height/2+30*700/height)
text("EASY", 34*500/width, height/2+31*700/height)
fill(255,255,0);
rect(width/3-10*500/width,height/2, width/3-10*500/width, 40*700/height);
fill(0);
textSize(30*500/width);
text("MEDIUM", 10*500/width+width/3, height/2+30*700/height)
text("MEDIUM", 9*500/width+width/3, height/2+31*700/height)
fill(255,0,255);
rect(2*width/3,height/2, width/3, 40*700/height);
fill(0);
textSize(30*500/width);
text("THOE", 30*500/width+2*width/3, height/2+30*700/height)
text("THOE", 29*500/width+2*width/3, height/2+31*700/height)
fill(255,255,0);
rect(0,height-100*500/width, width, 100*700/height);
fill(0);
textSize(30*500/width);
text("CLICK HERE TO CREATE FLAG", width/2-215*500/width, height-40*700/height);
text("CLICK HERE TO CREATE FLAG", width/2-214*500/width, height-41*700/height);
text("CLICK HERE TO CREATE FLAG", width/2-213*500/width, height-42*700/height);
}
function reStart(){
//gameOn = false;
numCells = 10*level;
numMines = int(random(3*numCells,4*numCells));
score = -numMines;
w = width/numCells;
cell = makeCells(numCells, numCells);
fillCells();
placeMines();
flag = false;
end = 0;
firstPress = true;
}
function showFlag(){
if(flag){
fill(255,0,0);
ellipse(mouseX, mouseY, w*0.8, w*0.8);
}
}
function makeCells(cols, rows){
var arr = new Array(cols);
for(var i = 0; i < cols; i++){
arr[i] = new Array(rows);
}
return arr;
}
function fillCells(){
for(var i = 0; i < cell.length; i++){
for(var j = 0; j < cell[0].length; j++){
cell[i][j] = new Cell(i, j);
}
}
}
function showCells(){
for(var i = 0; i < cell.length; i++){
for(var j = 0; j < cell[0].length; j++){
cell[i][j].show();
}
}
}
function placeMines(){
while(numMines > 0){
a = int(random(cell.length));
b = int(random(cell[0].length));
if(!cell[a][b].isMine){
cell[a][b].isMine = true;
numMines--;
}
}
}
function hideCells(){
for(var i = 0; i < cell.length; i++){
for(var j = 0; j < cell[0].length; j++){
if(!cell[i][j].isRevealed){
fill(155);
rect(i*w, j*w, w, w);
}
if(cell[i][j].isFlagged && !cell[i][j].isRevealed){
fill(255,0,0);
rect(i*w, j*w, w, w);
}
}
}
}
function countMines(){
for(var i = 0; i < cell.length; i++){
for(var j = 0; j < cell[0].length; j++){
//LEFT THREE
if(i > 0 && j >0 && cell[i-1][j-1].isMine){
cell[i][j].count++;
}
if(i > 0 && cell[i-1][j].isMine){
cell[i][j].count++;
}
if(i > 0 && j < cell[0].length-1 && cell[i-1][j+1].isMine){
cell[i][j].count++;
}
//MIDDLE TWO
if(j > 0 && cell[i][j-1].isMine){
cell[i][j].count++;
}
if(j < cell[0].length-1 && cell[i][j+1].isMine){
cell[i][j].count++;
}
//RIGHT THREE
if(i < cell.length-1 && j >0 && cell[i+1][j-1].isMine){
cell[i][j].count++;
}
if(i < cell.length-1 && cell[i+1][j].isMine){
cell[i][j].count++;
}
if(i < cell.length-1 && j < cell[0].length-1 && cell[i+1][j+1].isMine){
cell[i][j].count++;
}
}
}
}
function checkCell(a,b){
cell[a][b].isRevealed = true;
if(cell[a][b].isMine){
end = 1;
} else if(cell[a][b].count === 0 ){
//LEFT THREE
if(a > 0 && b > 0 && !cell[a-1][b-1].isRevealed){
checkCell(a-1, b-1);
}
if(a > 0 && !cell[a-1][b].isRevealed){
checkCell(a-1, b);
}
if(a > 0 && b < cell[0].length-1 && !cell[a-1][b+1].isRevealed){
checkCell(a-1, b+1);
}
//MIDDLE TWO
if(b > 0 && !cell[a][b-1].isRevealed){
checkCell(a, b-1);
}
if(b < cell[0].length-1 && !cell[a][b+1].isRevealed){
checkCell(a, b+1);
}
//RIGHT THREE
if(a <cell.length-1 && b > 0 && !cell[a+1][b-1].isRevealed){
checkCell(a+1, b-1);
}
if(a < cell.length-1 && !cell[a+1][b].isRevealed){
checkCell(a+1, b);
}
if(a < cell.length-1 && b < cell[0].length-1 && !cell[a+1][b+1].isRevealed){
checkCell(a+1, b+1);
}
}
}
function endGame(x){
if(score == 0){
x = 2;
}
if(x == 2){
revealAll();
textSize(44*500/width)
rect(40*500/width, 100*700/height, 220*500/width, 50*700/height);
fill(255);
text("YOU WIN!!!", 40*500/width, 140*700/height);
textSize(22*500/width);
fill(0);
rect(30*500/width, 275*700/height, 300*500/width, 30*700/height);
fill(255);
text("Click anywhere to play again", 40*500/width, 300*700/height);
if(mouseIsPressed){
reStart();
}
} else if (x == 1) {
revealAll();
textSize(44)
rect(38*500/width, 100*700/height, 228*500/width, 50*700/height);
fill(255);
text("YOU LOST", 40*500/width, 140*700/height);
textSize(29*500/width);
fill(0);
rect(30*500/width, 325*700/height, 400*500/width, 45*700/height);
fill(255);
text("Click anywhere to play again", 40*500/width, 360*700/height);
if(mouseIsPressed){
reStart();
}
}
}
function revealAll(){
for(var i = 0; i < cell.length; i++){
for(var j = 0; j < cell[0].length; j++){
cell[i][j].isRevealed = true;
}
}
}