Advertisement
Zelkins

blocksweeper

Jun 10th, 2017
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>blocksweeper</title>
  5. </head>
  6. <body>
  7.  
  8. <canvas id='gameCanvas' width='800' height='800'></canvas>
  9. <script type="text/javascript">
  10.  
  11. var firstClick;
  12. var blockWidth = 32;
  13. var blocks;
  14. var showEndScreen = false;
  15.  
  16.  
  17. window.onload = function() {
  18.     canvas = document.getElementById('gameCanvas');
  19.     ctx = canvas.getContext('2d');
  20.  
  21.     canvas.addEventListener('mousedown', mouseDown, false);
  22.     createGrid();
  23.  
  24.     var fps = 30;
  25.     setInterval(draw, 1000/fps);
  26. }
  27.  
  28. function draw() {
  29.     drawBackgroundGrid();
  30.  
  31.     for(var i = 0; i < blocks.length; i++)
  32.         for(var j = 0; j < blocks[0].length; j++)
  33.             blocks[i][j].show();
  34. }
  35.  
  36. function mouseDown (evt) {
  37.     var mousePos = calculateMousePos(evt);
  38.     for(var i = 0; i < blocks.length; i++)
  39.         for(var j = 0; j < blocks.length; j++)
  40.             if(mousePos.x >= blocks[i][j].x+1 && mousePos.x <= blocks[i][j].x + blocks[i][j].width-1)
  41.                 if(mousePos.y >= blocks[i][j].y+1 && mousePos.y <= blocks[i][j].y + blocks[i][j].height-1)
  42.                     if(evt.which == 1 && !blocks[i][j].isFlagged) {
  43.                         blocks[i][j].hidden = false;
  44.                         if(firstClick)
  45.                             createMines(i, j);
  46.                         console.log(blocks[i][j]);
  47.                         showAdjacent(blocks[i][j]);
  48.                     } else if(evt.which == 3) {
  49.                         if(blocks[i][j].hidden)
  50.                             blocks[i][j].isFlagged = !blocks[i][j].isFlagged;
  51.                     }
  52. }
  53.  
  54. function calculateMousePos (evt) {
  55.     var canvasBounds = canvas.getBoundingClientRect();
  56.     var root = document.documentElement;
  57.  
  58.     var mouseX = evt.clientX - canvasBounds.left - root.scrollLeft;
  59.     var mouseY = evt.clientY - canvasBounds.top - root.scrollTop;
  60.  
  61.     return {
  62.         x: mouseX,
  63.         y: mouseY
  64.     }
  65. }
  66.  
  67. function showAdjacent(block) {
  68.    
  69.     if(block.minesNextTo != 0)
  70.         return;
  71.  
  72.     console.log(block.row+','+block.col);
  73.     block.uncovered = true;
  74.     block.hidden = false;
  75.  
  76.     console.log(block.adjacentBlocks.length);
  77.  
  78.     for(i = 0; i < block.adjacentBlocks.length; i++)
  79.         block.adjacentBlocks[i].hidden = false;
  80.  
  81.     for(i = 0; i < block.adjacentBlocks.length; i++) {
  82.         if(!block.adjacentBlocks[i].uncovered)
  83.             showAdjacent(block.adjacentBlocks[i]);
  84.     }
  85.  
  86. }
  87.  
  88. function Block(x, y, row, col) {
  89.     this.x = x;
  90.     this.y = y;
  91.     this.width = blockWidth;
  92.     this.height = blockWidth;
  93.     this.isMine = false;
  94.     this.hidden = true;
  95.     this.isFlagged = false;
  96.     this.minesNextTo = 0;
  97.     this.adjacentBlocks = [];
  98.     this.uncovered = false;
  99.     this.row = row;
  100.     this.col = col;
  101.  
  102.     this.show = function() {
  103.         if(!this.hidden) {
  104.             if(this.isMine) {
  105.                 ctx.fillStyle = 'rgb(75, 75, 75)';
  106.                 ctx.beginPath();
  107.                 ctx.arc(this.x+this.width/2+.5, this.y+this.height/2, this.width/2-3, 0, 2*Math.PI);
  108.                 ctx.closePath();
  109.                 ctx.fill();
  110.             } else {
  111.                 if(this.minesNextTo > 0) {
  112.                     ctx.fillStyle = 'blue';
  113.                     ctx.textAlign = 'center';
  114.                     ctx.font = 'bold 25px Courier';
  115.                     ctx.fillText(this.minesNextTo, this.x+this.width/2, this.y+this.width/1.35);
  116.                 }
  117.             }
  118.         } else {
  119.             ctx.fillStyle = 'rgb(150, 150, 150)';
  120.             ctx.fillRect(this.x, this.y, this.width, this.height);
  121.             ctx.fillStyle = 'rgb(235, 235, 235)';
  122.             ctx.fillRect(this.x+3, this.y+3, this.width-6, this.height-6);
  123.             if(this.isFlagged) {
  124.                 ctx.fillStyle = 'black';
  125.                 ctx.fillRect(this.x+10, this.y+this.height-16, 3, 10);
  126.                 ctx.fillStyle = 'red';
  127.                 ctx.beginPath();
  128.                 ctx.moveTo(this.x+10, this.y+6)
  129.                 ctx.lineTo(this.x+10, this.y+this.height-14)
  130.                 ctx.lineTo(this.x+this.width-8, this.y+this.height/2-5)
  131.                 ctx.closePath();
  132.                 ctx.fill();
  133.             }
  134.         }
  135.     }
  136. }
  137.  
  138. function createGrid() {
  139.     firstClick = true;
  140.     blocks = create2DArray(canvas.width/blockWidth, canvas.height/blockWidth)
  141.    
  142.     for(var i = 0; i < blocks.length; i++)
  143.         for(var j = 0; j < blocks[0].length; j++){
  144.             blocks[i][j] = new Block(i*blockWidth, j*blockWidth, i, j);
  145.             console.log('('+i+','+j+')('+(i*blockWidth)+','+(j*blockWidth)+')')
  146.         }
  147. }
  148.  
  149. function create2DArray(rows, cols) {
  150.     var ray = new Array(rows);
  151.     for(var i = 0; i < ray.length; i++)
  152.         ray[i] = new Array(cols)
  153.     return ray;
  154. }
  155.  
  156. function createMines(firstClickX, firstClickY) {
  157.     firstClick = false;
  158.     var numberOfMines = 15;//Math.floor(blocks.length*blocks[0].length*.15);
  159.     console.log(numberOfMines);
  160.     var minesCreated = 0;
  161.     while(minesCreated <= numberOfMines) {
  162.         var row = Math.floor(Math.random()*(blocks.length-1));
  163.         var col = Math.floor(Math.random()*(blocks[0].length-1));
  164.         if(!blocks[row][col].isMine && row != firstClickX && col != firstClickY) {
  165.             blocks[row][col].isMine = true;
  166.             minesCreated++;
  167.         }
  168.     }
  169.  
  170.     for(var i = 0; i < blocks.length; i++)
  171.         for(var j = 0; j < blocks[0].length; j++) {    
  172.             var totalNextTo = 0;
  173.             if(i+1 in blocks && j in blocks[i]) {
  174.                 blocks[i][j].adjacentBlocks.unshift(blocks[i+1][j]);
  175.                 if(blocks[i+1][j].isMine)
  176.                     totalNextTo++;
  177.             }
  178.             if(i-1 in blocks && j in blocks[i]) {
  179.             blocks[i][j].adjacentBlocks.unshift(blocks[i-1][j]);
  180.                 if(blocks[i-1][j].isMine)
  181.                     totalNextTo++;
  182.             }
  183.             if(i in blocks && j+1 in blocks[i]) {
  184.             blocks[i][j].adjacentBlocks.unshift(blocks[i][j+1]);
  185.                 if(blocks[i][j+1].isMine)
  186.                     totalNextTo++;
  187.             }
  188.             if(i in blocks && j-1 in blocks[i]) {
  189.             blocks[i][j].adjacentBlocks.unshift(blocks[i][j-1]);
  190.                 if(blocks[i][j-1].isMine)
  191.                     totalNextTo++;
  192.             }
  193.             if(i+1 in blocks && j-1 in blocks[i]) {
  194.             blocks[i][j].adjacentBlocks.unshift(blocks[i+1][j-1]);
  195.                 if(blocks[i+1][j-1].isMine)
  196.                     totalNextTo++;
  197.             }
  198.             if(i-1 in blocks && j+1 in blocks[i]) {
  199.             blocks[i][j].adjacentBlocks.unshift(blocks[i-1][j+1]);
  200.                 if(blocks[i-1][j+1].isMine)
  201.                     totalNextTo++;
  202.             }
  203.             if(i-1 in blocks && j-1 in blocks[i]) {
  204.             blocks[i][j].adjacentBlocks.unshift(blocks[i-1][j-1]);
  205.                 if(blocks[i-1][j-1].isMine)
  206.                     totalNextTo++;
  207.             }
  208.             if(i+1 in blocks && j+1 in blocks[i]) {
  209.             blocks[i][j].adjacentBlocks.unshift(blocks[i+1][j+1]);
  210.                 if(blocks[i+1][j+1].isMine)
  211.                     totalNextTo++;
  212.             }
  213.             blocks[i][j].minesNextTo = totalNextTo;
  214.         }
  215. }
  216.  
  217. document.oncontextmenu = function() {
  218.     return false;
  219. }
  220.  
  221. function drawBackgroundGrid() {
  222.     ctx.fillStyle = 'rgb(200, 200, 200)';
  223.     ctx.fillRect(0, 0, canvas.width, canvas.height);
  224.     ctx.fillStyle = 'black'
  225.     for(var i = 0; i < canvas.width; i+=blockWidth) {
  226.         ctx.fillRect(i-0.5, 0, 1, canvas.height);
  227.         ctx.fillRect(0, i-0.5, canvas.width, 1);
  228.     }
  229. }
  230.  
  231. </script>
  232.  
  233. </body>
  234. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement