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;
  background: #0000FF;
  color: #ffffff;
}
#field {
    border-spacing: 0;
    border-collapse: collapse;
}
.empty {
  background: #808080;
}
.end {
  opacity: 0.2;
}
 
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) {
  if (e.which) {
    return e.which == 2;
  } else {
    return e.button == 2;
  }
};
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.drawMine = function() {
  this.td.innerHTML = '&#9762;';
};
Cell.prototype.drawEmptyCell = function() {
  this.isOpened = true;
  dom.addClass(this.td, 'empty');
};
Cell.prototype.drawNumber = function(number) {
  this.isOpened = true;
  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.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' && cell.isMarked && dom.isRegionButton(e)) {
            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);
  
  
  if (cell.hasMine()) {
    this.gameOver();
    cell.drawMine(x, y);
  } else if (numberAroundMines !== 0){
    cell.drawNumber(numberAroundMines);
    return;
  } else if (numberAroundMines === 0) {
    cell.drawEmptyCell();
    var aroundCells = this.getAroundCells(x, y);
    if (!cell.isOpened) {
      for (var l = 0; l < aroundCells.length; l++) {
        this.openCell(aroundCells[l].getX(), aroundCells[l].getY());
      }
    }
  }
};
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() {
  dom.addClass(this.table, 'end');
};
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