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>
  <style> 
    td { width: 28px; height: 28px; background: white; border: 1px solid black; cursor: pointer;} 
    table { border-collapse: collapse; margin-right: -1px;}
    button { display: inline-block; width: 120px; margin-top: 20px; padding: 5px 0; border: 2px solid #dadada; border-radius: 5px; text-align: center; cursor: pointer;}
    button:hover {background: #f2e41f; }
    .black { background: black; }
    .conversely td { background: black; }
    .conversely .black { background: white; }
  </style> 
</head>
<body>
  <button id="btn-for-table">Поменять цвета</button>
</body>
</html>
 
function Util() {
  
}
Util.trim = function(str) {
  return str.replace(/^\s+|\s+$/g,"");
};
Util.hasClass = function(node, klass) {
  var classes = ' ' + node.className.replace(/\s/, ' ') + ' ';
  
  if ( classes.indexOf(' ' + klass + ' ') >= 0 ) { 
    return true; 
  }
  
  return false;
};
Util.addClass = function(node, klass) {
  if ( !Util.hasClass(node, klass) ) {
    if ( node.className.length !== 0 ) {
      node.className += ' ' + klass;
    } else {
      node.className = klass;
    }
  }
  
  return node;
};
Util.removeClass = function(node, klass) {
  if ( Util.hasClass(node, klass) ) {
    var classes = ' ' + node.className + ' ',
        regexp = new RegExp(" " + klass + "(?= )", 'g');
    
    node.className = Util.trim( classes.replace(regexp, ' ') );
  }
  
  return node;
};
Util.toggleClass = function(node, klass) {
  if ( !Util.hasClass(node, klass) ) {
    return Util.addClass(node, klass);
  } else {
    return Util.removeClass(node, klass);
  }
};
Util.getRandom = function(size) {
  return Math.floor(Math.random()*size);
};
function Field(width, height, mines) {
  this.width = width;
  this.height = height;
  this.mines = mines;
}
Field.MINE = 'mine';
Field.prototype.generateField = function() {
  var body = document.body,
      table = document.createElement('table'),
      tr,
      td;
  
  body.insertBefore(table, body.firstChild);
  
  this.table = table;
  this.cells = [];
  
  for (var x = 0; x < this.width; x++) {
    tr = table.insertRow();
    
    this.cells[x] = [];
    
    for (var y = 0; y < this.height; y++) {
      td = tr.insertCell();
      this.cells[x][y] = new Cell(td);
      this.cells[x][y][Field.MINE] = false;
    }
  }
};
Field.prototype.generateMines = function() {
  var x, y;
  
  for (var i = 0; i < this.mines; i++) {
    x = Util.getRandom(this.width);
    y = Util.getRandom(this.height);
    
    if ( !this.getCellAt(x, y).hasMine() ) {
      this.getCellAt(x, y).miningCell();
    }
  }
};
Field.prototype.getCellAt = function(x, y) {
  return this.cells[x][y];
};
Field.prototype.getCell = function(cell) {
  for (var x = 0; this.width > x; x++) {
    for (var y = 0; this.height > y; y++) {
      if (this.cells[x][y].getTd() == cell) {
        return this.cells[x][y];
      }
    }
  }
};
Field.prototype.calcMinesAround = function(pos1, pos2) {
  
};
function Cell(td) {
  this.td = td;
}
Cell.prototype.hasMine = function() {
  if (this[Field.MINE] === true) {
    return true;
  }
  
  return false;
};
Cell.prototype.miningCell = function() {
  this[Field.MINE] = true;
};
Cell.prototype.openCell = function() {
  var cell = this,
      td = this.getTd();
  
  if (cell[Field.MINE] === true) {
    td.innerHTML = '<span>☢</span>';
  }
};
Cell.prototype.getTd = function() {
  return this.td;
};
/*var button = document.getElementById('btn-for-table');
table.addEventListener('mousedown', function(event) {
  var target = event.target;
  
  if (target.tagName == 'TD') {
    toggleClass(target, 'black');
  }
});
button.addEventListener('click', function(event) {
  toggleClass(table, 'conversely');
});*/
var field1 = new Field(8, 8, 8);
field1.generateField();
field1.generateMines();
field1.table.addEventListener('mousedown', function(event) {
    var target = event.target;
  
    if (target.tagName == 'TD') {
      var cell = field1.getCell(target);
      cell.openCell();
    }
});
Output

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

Dismiss x
public
Bin info
anonymouspro
0viewers