JSFiddle - React, Tailwind, and code Playground
HTML
<script type="text/x-template" id="template-popup">
<div class="modal"></div>
<div class="popup-body">
{{text}}
<button type="button" class="new-game">Новая игра</button>
</div>
</script>
CSS
table{
border-collapse: collapse;
}
td{
width:28px;
height: 28px;
background-color: white;
border: 1px solid;
cursor: pointer;
text-align: center;
}
.opened{
background-color: gray;
}
.bomb:before{
content: "⚈";
}
.exploded{
background-color: red;
}
.modal{
position:fixed;
top:0px;
left:0px;
opacity: 0.3;
background-color:gray;
width:100%;
height:100%;
}
.popup-body{
position: fixed;
border: 1px solid;
background-color: white;
padding: 10px 40px;
left:40%;
top:40%;
z-index:9999;
}
.popup-body button{
display: block;
}
.flagged:before{
content: "⚑";
color: red;
}
JavaScript
function MineField(options){
this._rows = options.rows;
this._cells = options.cells;
this._table = this.renderTable();
this._bombs = [];
this._openedCells = [];
this._flaggedCells = [];
this._bombsCount = options.bombsCount;
this._template = options.template.innerHTML;
this._modal = document.createElement('div');
this._generated = false;
this._table.addEventListener('mousedown', this.clickCell.bind(this));
this._table.oncontextmenu = function(){
return false;
};
}
MineField.prototype.renderTable = function()
{
var table = document.createElement('table');
var tbody = document.createElement('tbody');
table.appendChild(tbody);
for(var i = 0; i<this._rows; i++){
var row = document.createElement('tr');
for(var n = 0; n<this._cells; n++){
var cell = document.createElement('td');
row.appendChild(cell);
}
tbody.appendChild(row);
}
document.body.appendChild(table);
return table;
};
MineField.prototype.generate = function(firstClicked)
{
if(this._generated) return;
for(var i = 0; i<this._bombsCount; i++){
var x = Math.floor(Math.random()*9);
var y = Math.floor(Math.random()*9);
while(this.checkForBomb(x,y) || (x == firstClicked[0] && y == firstClicked[1])){
x = Math.floor(Math.random()*9);
y = Math.floor(Math.random()*9);
}
this._bombs.push([x, y]);
}
this._generated = true;
};
MineField.prototype.checkForBomb = function(x,y)
{
for(var i = 0; i< this._bombs.length; i++){
if(this._bombs[i][0] == x && this._bombs[i][1] == y){
return true;
}
}
return false;
};
MineField.prototype.clickCell = function(event)
{
event.preventDefault();
if(event.button==0){
this.toggleCell(event.target);
...