Advertisement
MrThoe

Example PDF

May 18th, 2021
890
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var cell = [];
  2. var w;
  3. var numCells;
  4. var numMines = 20;
  5.  
  6. function setup() {
  7.   createCanvas(400, 400);
  8.   numCells = 16;
  9.   w = width/numCells;
  10.   for(var i = 0; i < numCells; i++){
  11.     let arr = [];
  12.     for(var j = 0; j < numCells; j++){
  13.       arr.push(new Cell(i,j));
  14.     }
  15.     cell.push(arr);
  16.   }
  17.   textSize(26);
  18.   while(numMines > 0){
  19.     let x = floor(random(numCells));
  20.     let y = floor(random(numCells));
  21.     if(!cell[x][y].isMine){
  22.       cell[x][y].isMine = true;
  23.       numMines--;
  24.     }
  25.   }
  26.   countMines();
  27. }
  28.  
  29. function draw() {
  30.   background(220);
  31.   for(var i = 0; i < numCells; i++){
  32.     for(var j = 0; j < numCells; j++){
  33.       cell[i][j].show();
  34.     }
  35.   }
  36. }
  37.  
  38. function countMines(){
  39.   for(var i  = 0; i < numCells; i++){
  40.     for(var j = 0; j < numCells; j++){
  41.       let c = 0;
  42.       if(i>0 && j >0){
  43.          if(cell[i-1][j-1].isMine){c++;}  //ROW ABOVE (j-1)
  44.       }
  45.       if(j>0){
  46.         if(cell[i][j-1].isMine){c++;}
  47.       }
  48.       if(i<numCells-1 && j>0){
  49.         if(cell[i+1][j-1].isMine){c++;}
  50.       }
  51.       if(i>0){
  52.         if(cell[i-1][j].isMine){c++;}  //LEFT
  53.       }
  54.       if(i<numCells-1){
  55.         if(cell[i+1][j].isMine){c++;}  //RIGHT
  56.       }
  57.       if(i>0 && j < numCells-1){
  58.         if(cell[i-1][j+1].isMine){c++;}  //ROW BELOW (j+1)
  59.       }
  60.       if(j<numCells-1){
  61.         if(cell[i][j+1].isMine){c++;}
  62.       }
  63.       if(i<numCells-1 && j < numCells-1){
  64.         if(cell[i+1][j+1].isMine){c++;}
  65.       }
  66.       cell[i][j].count = c;
  67.     }
  68.   }
  69. }
  70.  
  71. function mousePressed(){
  72.   if(mouseX>0 && mouseX < width && mouseY >0 && mouseY<height){
  73.     let x = floor(mouseX / w);
  74.     let y = floor(mouseY / w);
  75.     revealCell(x, y);
  76.   }
  77. }
  78.  
  79. function revealCell(x, y){
  80.   cell[x][y].isRevealed = true;
  81.   if(cell[x][y].isMine){
  82.     revealAll();
  83.   }
  84.   if(cell[x][y].count == 0){
  85.     //CELLS ABOVE
  86.     if(x>0 && y>0){
  87.       revealCell(x-1, y-1);
  88.     }
  89.     if(y>0){
  90.       revealCell(x, y-1);
  91.     }
  92.     if(x+1 < numCells-1 && y>0){
  93.       revealCell(x+1, y-1);
  94.     }
  95.    
  96.     //Side CELLS
  97.     if(x>0){
  98.       if(!cell[x-1][y].isRevealed){
  99.         revealCell(x-1, y);
  100.       }
  101.     }
  102.     if(x<numCells-1){
  103.       if(!cell[x+1][y].isRevealed){
  104.         revealCell(x+1, y);
  105.       }
  106.     }
  107.    
  108.     //CELLS BELOW
  109.     if(x>0 && y < numCells-1){
  110.       if(!cell[x-1][y+1].isRevealed){
  111.         revealCell(x-1, y+1);
  112.       }
  113.     }
  114.     if(y<numCells-1){
  115.       if(!cell[x][y+1].isRevealed){
  116.         revealCell(x, y+1);
  117.       }
  118.     }
  119.     if(x<numCells-1 && y<numCells-1){
  120.       if(!cell[x+1][y+1].isRevealed){
  121.         revealCell(x+1, y+1);
  122.       }
  123.     }
  124.   }
  125. }
  126.  
  127. function revealAll(){
  128.   for(var i = 0; i < numCells; i++){
  129.     for(var j = 0; j < numCells; j++){
  130.       cell[i][j].isRevealed = true;
  131.     }
  132.   }
  133. }
  134. class Cell{
  135.   constructor(x, y){
  136.     this.x = x;
  137.     this.y = y;
  138.     this.hasMine = false;
  139.     this.count = 0;
  140.     this.isMine = false;
  141.     this.isRevealed = false;
  142.   }
  143.  
  144.   show(){
  145.    
  146.     if(this.isRevealed){
  147.       if(this.isMine){
  148.         fill(0,0,255);
  149.       } else {
  150.         fill(255);
  151.       }
  152.       rect(this.x*w, this.y*w, w, w);
  153.       fill(0);
  154.       text(this.count, this.x*w+w/4 + 2, this.y*w + 3*w/4);
  155.     } else {
  156.       fill(220);
  157.       rect(this.x*w, this.y*w, w, w);
  158.     }
  159.     //text(this.x, this.x*w, this.y*w+w/2);
  160.   }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement