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>
 
body {
  text-align: center;
  border: 2px solid red;
}
td { 
  width: 28px; 
  height: 28px; 
  background: gray; 
  border: 1px solid black; 
  cursor: pointer; 
  text-align: center; 
} 
table { 
  position: absolute;
  border-collapse: collapse; 
  margin: 0 auto;
}
.cover {
  position: fixed;
  width: 100%;
  height: 100%;
  background: gray;
  opacity: 0.3;
  z-index: 9000;
}
.window { 
  display: inline-block;
  position: fixed;
  width: 60%;
  height: 100px;
  vertical-align: middle;
  text-align: center;
  background: grey;
  border: 2px soid white;
  border-radius: 5px;
  z-index: 9999;
}
.white-cell {
  background: white;
}
.btn {
  display: inline-block;
  width: 100px;
  padding: 10px;
  text-align: center;
  vertical-align: middle;
  border: 2px solid red;
  background: white;
  border-radius: 3px;
}
 
"// noprotect";
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);
};
Util.showWindow = function() {
  var cover = document.createElement('div'),
      window = document.createElement('div'),
      button = document.createElement('button'),
      body = document.body;
  
  body.appendChild(cover);
  body.appendChild(window);
  window.appendChild(button);
  
  Util.addClass(window, 'window');
  Util.addClass(cover, 'cover');
  Util.addClass(button, 'btn');
  
  window.innerHTML = 'Вы проиграли';
  button.innerHTML = 'Новая игра';
};
function Field(width, height, mines) {
  this.width = width;
  this.height = height;
  this.mines = mines;
}
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, x, y);
    }
  }
};
Field.prototype.generateMines = function() {
  var x, y;
  var i = 0;
  
  while (i < this.mines) {
    x = Util.getRandom(this.width);
    y = Util.getRandom(this.height);
    
    if ( !this.getCellAt(x, y).checkHasMine() ) {
      this.getCellAt(x, y).miningCell();
      console.log(x + ' ' + y);
      i++;
    }
  }
};
Field.prototype.getCellAt = function(x, y) {
  return this.cells[x][y];
};
Field.prototype.getCell = function(td) {
  for (var x = 0; this.width > x; x++) {
    for (var y = 0; this.height > y; y++) {
      if (this.getCellAt(x, y).getTd() == td) {
        return this.cells[x][y];
      }
    }
  }
};
Field.prototype.calcMinesAround = function(cell) {
  var mines = 0;
  var cells = this.getNeighboars(cell);
      
  for (var i = 0; i < cells.length; i++) {
    if (cells[i].checkHasMine() === true) {
      mines++;
    }
  }
  
  return mines;
};
Field.prototype.getNeighboars = function(cell) {
  var cells = [];
  
  var posX = cell.getCoorByCell().x,
      posY = cell.getCoorByCell().y;
  
  for (var x = Math.max(0, posX - 1); x <= Math.min(7, posX + 1); x++) {
    for (var y = Math.max(0, posY - 1); y <= Math.min(7, posY + 1); y++) {
      if ( (x != posX || y != posY) ) {
        cell = this.getCellAt(x, y);
        
        cells.push(cell);
      }
    }
  }
  
  return cells;
};
Field.prototype.openNullCells = function(cell) {
  var neighbCells = this.getNeighboars(cell);
  
  Util.addClass(cell.getTd(), 'white-cell');
  
  for (var i = 0; i < neighbCells.length; i++) {
    if (this.calcMinesAround(neighbCells[i]) === 0) {
      return this.openNullCells(neighbCells[i]);
    }
  }
};
Field.prototype.openCell = function(cell) {
  var td = cell.getTd();
  
  if (cell.hasMine === true) {
    td.innerHTML = '<span>☢</span>';
    Util.showWindow();
  }
  
  if (cell.hasMine === false) {
    var mines = this.calcMinesAround(cell);
    
    if (mines !== 0) {
      td.innerHTML = '<span>' + mines +'</span>';
    }
    
    if (mines === 0) {
      this.openNullCells(cell);
    }
  }
  
  Util.addClass(cell.getTd(), 'white-cell');
};
function Cell(td, x, y) {
  this.td = td;
  this.hasMine = false;
  this.x = x;
  this.y = y;
}
Cell.prototype.checkHasMine = function() {
  if (this.hasMine) {
    return true;
  }
  
  return false;
};
Cell.prototype.miningCell = function() {
  this.hasMine = true;
};
Cell.prototype.getTd = function() {
  return this.td;
};
Cell.prototype.getCoorByCell = function() {
  return {'x': this.x, 'y': this.y};
};
/*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);
      
      
      
      field1.openCell(cell);
    }
});
Output

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

Dismiss x
public
Bin info
anonymouspro
0viewers