daily pastebin goal
41%
SHARE
TWEET

Minesweeper guess counter

qqwref Sep 13th, 2015 7 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2. <head><title>Javascript Minesweeper</title>
  3. <style type="text/css">
  4. .a {
  5.  color: blue;
  6.  text-decoration: underline;
  7.  cursor: pointer }
  8. .y {
  9.  overflow: auto;
  10.  max-width: 800px;
  11.  max-height: 600px;
  12.  text-align: center;
  13.  vertical-align: middle;
  14. }
  15. span#stats {
  16.  font-size: 80%;
  17. }
  18. </style>
  19. <script type="text/javascript">
  20. <!--
  21.  
  22. var h, w, m; // Height, width, mines in board
  23. var squares = []; // Holds board data - 1,2,4 bits = mine,flag,opened
  24.  
  25. var patterns =
  26.  [[[3,2], [0,0], [0,1], [0,2], [3,0], [3,1], [3,2], [1,1,2,1]], // linear
  27.   [[2,3], [0,0], [1,0], [2,0], [0,3], [1,3], [2,3], [1,1,1,2]], // linear
  28.   [[6,2], [0,0], [0,1], [0,2], [6,0], [6,1], [6,2], [1,1,2,1], [2,1,4,1],[4,1,5,1]], // double linear
  29.   [[2,6], [0,0], [1,0], [2,0], [0,6], [1,6], [2,6], [1,1,1,2], [1,2,1,4],[1,4,1,5]], // double linear
  30.   [[3,3], [0,0], [3,0], [0,3], [3,3], [1,1,1,2], [1,1,2,1], [1,2,2,2]]]; // block
  31.  
  32. function initialize() {
  33.  loadBoard(262, 718, 40000);
  34.  var max = 10;
  35.  var z = 0;
  36.  var k = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
  37.  var total = 0;
  38.  document.getElementById('stats').innerHTML = "";
  39.  for (var q=0; q<max; q++) {
  40.   placeMines();
  41.   var holder = [];
  42.   for (var i=0; i<h+2; i++) {
  43.    holder[i] = [];
  44.    for (var j=0; j<w+2; j++) {
  45.     if (j==0 || j==w+1 || i==0 || i==h+1) holder[i][j] = 1;
  46.     else holder[i][j] = (squares[i-1][j-1] & 1);
  47.    }
  48.   }
  49.   if (countGuesses(holder) >= k.length) {
  50.    for (var i=k.length; i<=countGuesses(holder); i++) {
  51.     k[i] = 0;
  52.    }
  53.   }
  54.   k[countGuesses(holder)]++;
  55.   total += countGuesses(holder);
  56.  }
  57.  document.getElementById('stats').innerHTML += k + "<br>" + (total/max);
  58. }
  59.  
  60. function loadBoard(newH, newW, newM) {
  61.  h = newH;
  62.  w = newW;
  63.  m = newM;
  64.  for (var i=0; i<h; i++) {
  65.   squares[i] = [];
  66.   for (var j=0; j<w; j++) {
  67.    squares[i][j] = 0;
  68.   }
  69.  }
  70. }
  71.  
  72. function clearBoard() {
  73.  for (var i=0; i<h; i++) {
  74.   for (var j=0; j<w; j++) {
  75.    squares[i][j] = 0;
  76.   }
  77.  }
  78. }
  79.  
  80. function placeMines() {
  81.  clearBoard();
  82.  for (var i=0; i<m; i++) {
  83.   var done = false;
  84.   while (!done) {
  85.    var newX = Math.floor(Math.random() * w);
  86.    var newY = Math.floor(Math.random() * h);
  87.    if ((squares[newY][newX] & 1) == 0) {
  88.     squares[newY][newX]++;
  89.     done = true;
  90.    }
  91.   }
  92.  }
  93. }
  94.  
  95. function printMines() {
  96.  var s="<pre>";
  97.  for (var i=0; i<h; i++) {
  98.   for (var j=0; j<w; j++) {
  99.    s += (squares[i][j]==0 ? "." : "X")
  100.   }
  101.   s += "<br>";
  102.  }
  103.  document.getElementById('stats').innerHTML += s;
  104. }
  105.  
  106. function countGuesses(arr) {
  107.  var num=0;
  108.   for (var a=0; a<patterns.length; a++) { // a-th pattern
  109.    for (var b=0; b<h+2-patterns[a][0][0]; b++) { // i index
  110.     for (var c=0; c<w+2-patterns[a][0][1]; c++) { // j index
  111.      var good = true;
  112.      for (var d=1; d<patterns[a].length; d++) { // number of the set
  113.       var cnt = 0;
  114.       for (var e=0; 2*e<patterns[a][d].length; e++) { // coordinate pair of the set
  115.        cnt += arr[b+patterns[a][d][2*e]][c+patterns[a][d][2*e+1]];
  116.       }
  117.       if (cnt != 1) good = false;
  118.      }
  119.      //if (good) {num++; document.getElementById('stats').innerHTML += b+"."+c+"<br>";}
  120.          if (good) num++;
  121.     }
  122.    }
  123.   }
  124.  return num;
  125. }
  126.  
  127. function isMine(x, y) {
  128.  if (isValid(x,y) && (squares[y][x] & 1) > 0) return 1;
  129.  else return 0;
  130. }
  131.  
  132. function isValid(x, y) {
  133.  if (x < 0 || y < 0 || x >= w || y >= h) return false;
  134.  return true;
  135. }
  136.  
  137. // -->
  138. </script></head>
  139. <body onload="initialize()" onkeydown="if(event.keyCode==113) resetBoard(false)" bgcolor="#f8f8f8">
  140. <span id="stats">&nbsp;</span>
  141. </body>
  142. </html>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top