<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: #0099FF;
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-lose {
display: inline-block;
position: relative;
width: 60%;
height: 100px;
vertical-align: middle;
text-align: center;
background: #FF3300;
border: 2px solid white;
border-radius: 5px;
z-index: 9999;
}
.window-win {
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;
}
.open-cell {
background: #CCFFFF;
}
#btn {
display: inline-block;
width: 120px;
padding: 5px;
margin: 0 auto;;
text-align: center;
vertical-align: bottom;
border: 2px solid green;
background: white;
border-radius: 3px;
cursor: pointer;
}
"// 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-lose');
Util.addClass(cover, 'cover');
button.setAttribute('id','btn');
window.innerHTML = 'Вы проиграли';
window.appendChild(button);
button.innerHTML = 'Новая игра';
button.addEventListener('mousedown', function() {
location.reload();
});
};
Util.showWinWindow = function() {
var body = document.body,
window = document.createElement('div');
body.appendChild(window);
Util.addClass(window, 'window-win');
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; 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,
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 ) {
cells.push(this.getCellAt(x, y));
}
}
}
return cells;
};
Field.prototype.openNullCells = function(cell) {
var neighbCells = this.getNeighboars(cell);
cell.showOpenCell();
for (var i = 0; i < neighbCells.length; i++) {
var mines = this.calcMinesAround(neighbCells[i]),
hasMine = neighbCells[i].checkHasMine(),
state = neighbCells[i].getState();
if ((mines === 0) && (hasMine === false) && (state === true)) {
this.openNullCells(neighbCells[i]);
} else if ((mines !== 0) && (hasMine === false)) {
var td = neighbCells[i].getTd();
mines = this.calcMinesAround(neighbCells[i]);
neighbCells[i].showOpenCell();
neighbCells[i].showAmountMines(mines, td);
}
}
};
Field.prototype.openCell = function(cell) {
var td = cell.getTd();
if (cell.checkHasMine() === true) {
cell.showMine(td);
Util.showWindow();
}
if (cell.checkHasMine() === false) {
var mines = this.calcMinesAround(cell);
if (mines !== 0) {
cell.showAmountMines(mines, td);
}
if (mines === 0) {
this.openNullCells(cell);
}
}
};
Field.prototype.checkWin = function() {
var amount = 0;
for (var x = 0; this.width > x; x++) {
for (var y = 0; this.height > y; y++) {
if (this.getCellAt(x, y).getState() === true) {
amount++;
}
}
}
if (amount == this.mines) {
Util.showWinWindow();
}
};
function Cell(td, x, y) {
this.td = td;
this.hasMine = false;
this.x = x;
this.y = y;
this.stateClose = true;
}
Cell.prototype.miningCell = function() {
this.hasMine = true;
};
Cell.prototype.checkHasMine = function() {
if (this.hasMine) {
return true;
}
return false;
};
Cell.prototype.getTd = function() {
return this.td;
};
Cell.prototype.getCoorByCell = function() {
return {'x': this.x, 'y': this.y};
};
Cell.prototype.getState = function() {
return this.stateClose;
};
Cell.prototype.changeState = function() {
this.stateClose = false;
};
Cell.prototype.showOpenCell = function() {
this.changeState();
Util.addClass(this.getTd(), 'open-cell');
};
Cell.prototype.showMine = function(td) {
this.showOpenCell();
td.innerHTML = '<span>☢</span>';
};
Cell.prototype.showAmountMines = function(mines, td) {
this.showOpenCell();
td.innerHTML = '<span>' + mines +'</span>';
};
/*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.checkWin();
}
});
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |