Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
<script type="text/template" id="list-template">
<div id="pop-up">
  <div class="pop-up-content">
    <p>{{message}}</p>
    <span class="newGame">New Game</span>
  </div>
</div>
</script>
</body>
</html>
 
html, body {
  margin: 0;
  padding: 0;
}
td {
    border: 2px solid;
  border-color: #eeeeee #999999 #999999 #eeeeee;
    width: 28px;
    height: 28px;
  background-image: linear-gradient(#e4e4e4, #cccccc);
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#e4e4e4', endColorstr='#cccccc',GradientType=0);
  color: #ffffff;
  text-align: center;
  font-weight: bold;
  font-size: 20px;
  cursor: default;
}
.open {
  border-color: #CACACA;
  background: #E4E4E4;
}
#field {
    border-spacing: 1px;
  transition: opacity 1s ease;
  position: relative;
  margin: 0 auto;
}
.one {
  color: #3333cc;
}
.two {
  color: #006600;
}
.more {
  color: #cc0000;
}
.mine {
  color: black;
}
#pop-up {
  width: 100%;
  min-height: 100%;
  background-color: rgba(0,0,0,0.5);
  overflow: hidden;
  position: fixed;
  top: 0px;
  z-index: 99;
}
.pop-up-content {
  margin: 40px auto 0 auto;
  width: 250px;
  height: 140px;
  padding: 20px;
  background-color: #f0f8ff;
  border-radius: 5px;
  box-shadow: 0px 0px 10px #000;
  text-align: center;
}
.pop-up-content p {
  font-size: 30px;
  font-weight: bold;
  color: red;
}
.pop-up-content .newGame {
  font-size: 30px;
  cursor: pointer;
  transition: color 1s ease;
}
.pop-up-content .newGame:hover {
  color: blue;
}
 
var dom = {};
dom.rand = function(min, max) {
    if(max) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    } else {
        return Math.floor(Math.random() * (min + 1));
    }
};
dom.trim = function(str) {
    return str.replace(/^\s+|\s+$/g, '');
};
dom.hasClass = function(node, klass) {
    var newClasses = (node.className).replace(/\s+/g, ' ');
    return (' ' + newClasses + ' ').indexOf(' ' + klass + ' ') > -1;
};
dom.addClass = function(node, klass) {
    if (!node.className) {
        node.className = klass;
    } else if (!this.hasClass(node, klass)) {
        node.className += ' ' + klass;
    }
};
dom.removeClass = function(node, klass) {
    if (node.className && dom.hasClass(node, klass)) {
        var newClasses = node.className.replace(new RegExp('(\\s+|^)' + klass + '(\\s+|$)', 'g'), ' ');
        node.className = this.trim(newClasses);
    }
};
dom.toggleClass = function(node, klass) {
    if (dom.hasClass(node, klass)) {
        dom.removeClass(node, klass);
    } else {
        dom.addClass(node, klass);
    }
};
dom.addEvent = function(elem, type, handler) {
    if (document.addEventListener) {
        elem.addEventListener(type, handler, false);
    } else {
        elem.attachEvent("on" + type, handler);
    }
};
dom.preventBrowserEvent = function(e) {
    if (e.preventDefault) {
        e.preventDefault();
    } else {
        e.returnValue = false;
  }
};
dom.isRegionButton = function(e, btn) {
  if (btn == 'right') {
    if (e.which) {
      return e.which == 3;
    } else {
      return e.button == 2;
    }
  } else if (btn == 'left') {
    if (e.which) {
      return e.which == 1;
    } else {
      return e.button == 1;
    }
  }
};
function Cell(x, y, td) {
  this.td = td;
    this.isOpened = false;
    this.isMined = false;
    this.isMarked = false;
    this.x = x;
    this.y = y;
}
Cell.prototype.toggleFlag = function() {
  if (this.isMarked) {
    this.td.innerHTML = '&#9873;';
    this.isMarked = true;
  } else {     
    this.td.innerHTML = '';
    this.isMarked = false;
  }
};
Cell.prototype.drawMine = function() {
  this.td.innerHTML = '&#9762;';
  dom.addClass(this.td, 'open');
  dom.addClass(this.td, 'mine');
};
Cell.prototype.drawEmptyCell = function() {
  this.isOpened = true;
  dom.addClass(this.td, 'open');
};
Cell.prototype.drawNumber = function(number) {
  this.isOpened = true;
  dom.addClass(this.td, 'open');
  switch (number) {
    case 1:
      dom.addClass(this.td, 'one');
      break;
    case 2:
      dom.addClass(this.td, 'two');
      break;
    default:
      dom.addClass(this.td, 'more');
  }
  this.td.innerHTML = number;
};
Cell.prototype.setMine = function() {
  this.isMined = true;
};
Cell.prototype.hasMine = function() {
    return this.isMined;
};
Cell.prototype.getX = function() {
  return this.x;
};
Cell.prototype.getY = function() {
  return this.y;
};
function Field(width, height, mines) {
  
  this.table = document.createElement('table');
    this.width = width;
    this.height = height;
    this.mines = mines;
    this.cells = [];
  this.openCells = 0;
  
    this.createField();
  this.rightClickListener();
  this.clickListener();
  this.generateMines();
}
Field.prototype.createField = function() {
  this.table.id = 'field';
  document.body.appendChild(this.table);
    var row, y, x;
    
    for (y = 0; y < this.height; y++) {
        row = this.table.insertRow(y);
    this.cells[y] = [];
        for (x = 0; x < this.width; x++) {
            this.cells[y].push(new Cell(x, y, row.insertCell(x)));
        }
    }
};
Field.prototype.rightClickListener = function() {
  var self = this;
  dom.addEvent(this.table, 'mousedown', function(e) {
    
        var target = e && e.target || event.srcElement,
        cell = self.cells[target.parentNode.rowIndex][target.cellIndex];
    
        if (target.tagName == 'TD' && dom.isRegionButton(e, 'right')) {
            cell.toggleFlag();
        }
    dom.preventBrowserEvent(e);
  });
};
Field.prototype.clickListener = function() {
  var self = this;
  dom.addEvent(this.table, 'click', function(e) {
        
        var target = e && e.target || event.srcElement;
    
        if (target.tagName != 'TD') {
            return false;
        }
    
    self.openCell(target.cellIndex, target.parentNode.rowIndex);
    
        dom.preventBrowserEvent(e);
  });
};
Field.prototype.generateMines = function(table) {
    var randRow, randCell, i, mineCount = 0;
  
  while(mineCount < this.mines) {
    randRow = dom.rand(0, this.height - 1);
    randCell = dom.rand(0, this.width - 1);
    
    if (this.cells[randRow][randCell].hasMine()) {
      continue;
    } else {
      mineCount++;
      this.cells[randRow][randCell].setMine();
    }
  }
};
Field.prototype.openCell = function(x, y) {
  var cell = this.cells[y][x];
  var numberAroundMines = this.getNumberAroundMines(cell);
  var lostCells = this.width * this.height - this.mines;
  
  if (cell.hasMine()) {
    this.gameOver();
    cell.drawMine(x, y);
  } else if (numberAroundMines !== 0 && !cell.isOpened){
    cell.drawNumber(numberAroundMines);
    this.openCells++;
    if (this.openCells == lostCells) {
      this.winGame();
    }
    
  } else if (numberAroundMines === 0 && !cell.isOpened) {
    cell.drawEmptyCell();
    this.openCells++;
    if (this.openCells == lostCells) {
      this.winGame();
    }
    var aroundCells = this.getAroundCells(x, y);
      for (var l = 0; l < aroundCells.length; l++) {
        this.openCell(aroundCells[l].getX(), aroundCells[l].getY());
      }
  }
};
Field.prototype.winGame = function() {
   var popUp = document.getElementById('list-template').innerHTML;
  popUp = popUp.replace(/{{\w+}}/, 'YOU WIN!');
  document.body.innerHTML = popUp;
    
  this.newGame();
};
Field.prototype.getNumberAroundMines = function(cell) {
  var cellIndex = cell.getX();
  var rowIndex = cell.getY();
  var aroundMines = [];
  
  for (var j = rowIndex - 1; j <= rowIndex + 1; j++) {
    for (var k = cellIndex - 1; k <= cellIndex + 1; k++) {
      if (!this.cellExist(k, j)) {
        continue;
      }
      if (this.cells[j][k].isMined) {
        aroundMines.push(this.cells[j][k]);
      }
    }
  }
  
  return aroundMines.length;
};
Field.prototype.cellExist = function(x, y) {
  
  if (x > this.width - 1 || x < 0 || y < 0 || y > this.height - 1) {
    return false;
  } else {
    return true;
  }
};
Field.prototype.getAroundCells = function(x, y) {
  var cellIndex = x;
  var rowIndex = y;
  var aroundCells = [];
  
  for (var j = rowIndex - 1; j <= rowIndex + 1; j++) {
    if (rowIndex === undefined) {
      continue;
    }
    for (var k = cellIndex - 1; k <= cellIndex + 1; k++) {
      if (!this.cellExist(k, j)) {
        continue;
      }
      
      if (k == x && j == y) {
        continue;
      }
      
      aroundCells.push(this.cells[j][k]);
    }
  }
  
  return aroundCells;
};
Field.prototype.gameOver = function() {    
  var popUp = document.getElementById('list-template').innerHTML;
  popUp = popUp.replace(/{{\w+}}/, 'YOU LOSE!');
  document.body.insertBefore = (popUp, this.table);
  
  this.showMines();   
  this.newGame();
};
Field.prototype.showMines = function() {
  for (var y = 0; y < this.height; y++) {
    for (var x = 0; x < this.width; x++) {
      if (this.cells[y][x].hasMine()) {
        this.cells[y][x].drawMine();
      }
    }
  }
};
Field.prototype.newGame = function() {
  dom.addEvent(document.getElementById('pop-up').children[0].children[1], 'click', function() {
    location.reload();
  });
};
function Game(width, height, mines) {
    this.field = new Field(width, height, mines);
}
var game = new Game(9, 9, 10);
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
fxslokerpro
0viewers