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 { 
  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;
}
 
"// 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) + 1;
};
Util.showWindow = function() {
  var cover = document.createElement('div'),
      window = document.createElement('div'),
      body = document.body;
  
  body.appendChild(cover);
  body.appendChild(window);
  
  Util.addClass(window, 'window');
  Util.addClass(cover, 'cover');
  
  window.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 + 2); x++) {
    tr = table.insertRow();
    
    this.cells[x] = [];
    
    for (var y = 0; y < (this.height + 2); y++) {
      if (x === 0 || x === (this.width + 1) || y === 0 || y == (this.height + 1)) {
        this.cells[x][y] = new Cell(td);
        continue;
      }
      
      td = tr.insertCell();
      this.cells[x][y] = new Cell(td);
    }
  }
};
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++;
    }
  }
  
      console.log('--------------');
};
Field.prototype.getCellAt = function(x, y) {
  return this.cells[x][y];
};
Field.prototype.getCell = function(cell) {
  for (var x = 1; this.width >= x; x++) {
    for (var y = 1; this.height >= y; y++) {
      if (this.getCellAt(x, y).getTd() == cell) {
        return this.cells[x][y];
      }
    }
  }
};
Field.prototype.getCoorByCell = function(cell) {
  for (var x = 1; this.width >= x; x++) {
    for (var y = 1; this.height >= y; y++) {
      if (this.getCellAt(x, y) == cell) {
        return {'x': x, 'y': y};
      }
    }
  }
};
Field.prototype.calcMinesAround = function(pos1, pos2) {
  var mines = 0;
      
  
  for (var x = pos1 - 1; x <= (pos1 + 1); x++) {
    for (var y = pos2 - 1; y <= (pos2 + 1); y++) {
      console.log(x + ' ' + y + ' - ' + this.getCellAt(x, y).hasMine);
      if ( this.getCellAt(x, y).hasMine === true && (x != pos1 || y != pos2) ) {
        mines++;
      }
    }
  }
  
  return mines;
};
Field.prototype.openCell = function(cell) {
  var td = cell.getTd();
  
  if (cell.hasMine === true) {
    td.innerHTML = '<span>☢</span>';
    Util.showWindow();
  }
  
  if (cell.hasMine === false) {
    var x = this.getCoorByCell(cell).x,
        y = this.getCoorByCell(cell).y;
    console.log(x + ' ' + y);
  
    var mines = this.calcMinesAround(x, y);
  
    td.innerHTML = '<span>' + mines +'</span>';
  }
};
function Cell(td) {
  this.td = td;
  this.hasMine = false;
}
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;
};
/*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);
      //field1.calcMinesAround(x, y);
    }
});
Output

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

Dismiss x
public
Bin info
anonymouspro
0viewers