Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var cell = [];
- var w;
- var numCells;
- var numMines = 20;
- function setup() {
- createCanvas(400, 400);
- numCells = 16;
- w = width/numCells;
- for(var i = 0; i < numCells; i++){
- let arr = [];
- for(var j = 0; j < numCells; j++){
- arr.push(new Cell(i,j));
- }
- cell.push(arr);
- }
- textSize(26);
- while(numMines > 0){
- let x = floor(random(numCells));
- let y = floor(random(numCells));
- if(!cell[x][y].isMine){
- cell[x][y].isMine = true;
- numMines--;
- }
- }
- countMines();
- }
- function draw() {
- background(220);
- for(var i = 0; i < numCells; i++){
- for(var j = 0; j < numCells; j++){
- cell[i][j].show();
- }
- }
- }
- function countMines(){
- for(var i = 0; i < numCells; i++){
- for(var j = 0; j < numCells; j++){
- let c = 0;
- if(i>0 && j >0){
- if(cell[i-1][j-1].isMine){c++;} //ROW ABOVE (j-1)
- }
- if(j>0){
- if(cell[i][j-1].isMine){c++;}
- }
- if(i<numCells-1 && j>0){
- if(cell[i+1][j-1].isMine){c++;}
- }
- if(i>0){
- if(cell[i-1][j].isMine){c++;} //LEFT
- }
- if(i<numCells-1){
- if(cell[i+1][j].isMine){c++;} //RIGHT
- }
- if(i>0 && j < numCells-1){
- if(cell[i-1][j+1].isMine){c++;} //ROW BELOW (j+1)
- }
- if(j<numCells-1){
- if(cell[i][j+1].isMine){c++;}
- }
- if(i<numCells-1 && j < numCells-1){
- if(cell[i+1][j+1].isMine){c++;}
- }
- cell[i][j].count = c;
- }
- }
- }
- function mousePressed(){
- if(mouseX>0 && mouseX < width && mouseY >0 && mouseY<height){
- let x = floor(mouseX / w);
- let y = floor(mouseY / w);
- revealCell(x, y);
- }
- }
- function revealCell(x, y){
- cell[x][y].isRevealed = true;
- if(cell[x][y].isMine){
- revealAll();
- }
- if(cell[x][y].count == 0){
- //CELLS ABOVE
- if(x>0 && y>0){
- revealCell(x-1, y-1);
- }
- if(y>0){
- revealCell(x, y-1);
- }
- if(x+1 < numCells-1 && y>0){
- revealCell(x+1, y-1);
- }
- //Side CELLS
- if(x>0){
- if(!cell[x-1][y].isRevealed){
- revealCell(x-1, y);
- }
- }
- if(x<numCells-1){
- if(!cell[x+1][y].isRevealed){
- revealCell(x+1, y);
- }
- }
- //CELLS BELOW
- if(x>0 && y < numCells-1){
- if(!cell[x-1][y+1].isRevealed){
- revealCell(x-1, y+1);
- }
- }
- if(y<numCells-1){
- if(!cell[x][y+1].isRevealed){
- revealCell(x, y+1);
- }
- }
- if(x<numCells-1 && y<numCells-1){
- if(!cell[x+1][y+1].isRevealed){
- revealCell(x+1, y+1);
- }
- }
- }
- }
- function revealAll(){
- for(var i = 0; i < numCells; i++){
- for(var j = 0; j < numCells; j++){
- cell[i][j].isRevealed = true;
- }
- }
- }
- class Cell{
- constructor(x, y){
- this.x = x;
- this.y = y;
- this.hasMine = false;
- this.count = 0;
- this.isMine = false;
- this.isRevealed = false;
- }
- show(){
- if(this.isRevealed){
- if(this.isMine){
- fill(0,0,255);
- } else {
- fill(255);
- }
- rect(this.x*w, this.y*w, w, w);
- fill(0);
- text(this.count, this.x*w+w/4 + 2, this.y*w + 3*w/4);
- } else {
- fill(220);
- rect(this.x*w, this.y*w, w, w);
- }
- //text(this.x, this.x*w, this.y*w+w/2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement