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>
</body>
</html>
 
td {
    border: 1px solid green;
    width: 28px;
    height: 28px;
}
#field {
    border-spacing: 0;
    border-collapse: collapse;
}
 
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;
  }
};
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 = false;
  } else {     
    this.td.innerHTML = '';
    this.isMarked = true;
  }
};
Cell.prototype.mineCell = function() {
  this.td.innerHTML = '&#9762;';
};
Cell.prototype.setNumber = function(number) {
  this.isOpened = true;
  this.td.innerHTML = number;
};
Cell.prototype.setMine = function() {
  this.isMine = true;
};
Cell.prototype.hasMine = function() {
  if (this.isMined) {
    return true;
  } else {
    return false;
  }
};
Cell.prototype.getX = function() {
  return this.x;
};
Cell.prototype.getY = function() {
  return this.y;
};
function Field(width, height, mines) {
  
    this.width = width;
    this.height = height;
    this.mines = mines;
    this.cells = [];
  
    this.createField();
  this.rightClickListener();
  this.generateMines();
  this.clickListener();
}
Field.prototype.createField = function() {
  var table = document.createElement('table');
  table.id = 'field';
  document.body.appendChild(table);
    var row, y, x;
    
    for (y = 0; y < this.height; y++) {
        row = 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.openCell = function(x, y) {
  var cell = this.cells[y][x];
  
  if (cell.hasMine()) {
    this.gameOver();
    this.cell.mineCell();
  } else if (this.getNumberAroundMines(cell) !== 0){
    cell.setNumber(this.getNumberAroundMines(cell));
  } else if (this.getNumberAroundMines(cell) === 0) {
    if (!l) {
      var l = 0;
    }
    var aroundCells = this.getAroundCells(x, y);
    for (; l < aroundCells.length; l++) {
      this.openCell(aroundCells[l].getX(), aroundCells[l].getY(), l);
      break;
    }
  }
};
Field.prototype.getTable = function() {
  return document.getElementById('field');
};
Field.prototype.clickListener = function() {
  var self = this;
  dom.addEvent(this.getTable(), '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.rightClickListener = function() {
  var self = this;
  dom.addEvent(this.getTable(), 'mousedown', function(e) {
        
        var target = e && e.target || event.srcElement,
        cell = self.cells[target.parentNode.rowIndex][target.cellIndex];
    
        if (target.tagName == 'TD' && target.innerHTML === '' && e.button == 2) {
            cell.toggleFlag();
        }
    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.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 < this.width - 1 || y < this.height - 1 || y > this.height - 1) {
    return false;
  } else {
    return true;
  }
};
Field.prototype.getAroundCells = function(x, y) {
  var cellIndex = y;
  var rowIndex = x;
  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;
      }
      
      aroundCells.push(this.cells[rowIndex][cellIndex]);
    }
  }
  
  return aroundCells;
};
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