<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.klet, .cl{
width: 50px;
height: 50px;
background-color: #ddd;
font-size: 42px;
border: 5px solid #ccc;
color: grey;
text-align: center;
}
.cl{
background-color: white;
border: 0;
color: white;
}
.green{
background-color: #B6FF00FF;
}
</style>
</head>
<body>
<div id="game"></div>
<script>
var game = document.querySelector('#game'),
table = document.createElement('table'), str = '';
arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,'cl'].sort((a, b)=>Math.random() - 0.5);
for(var i = 0; i<4; i++){
str += '<tr>';
for(var j = 0; j<4; j++) str += '<td class="klet '+(arr[arr.length-1] == 'cl'&&'cl')+'">'+arr.pop();
}
table.innerHTML = str;
game.append(table);
var td = document.querySelectorAll('td');
function fn2(t){
var cl = document.querySelector('.cl'),
text = t.textContent;
t.textContent = 'cl';
cl.textContent = text;
cl.classList.remove('cl');
t.classList.add('cl');
fn1();
}
(fn1=()=>{
var x = 0;
for(var i = 0; i<15; i++){
if(i+1 == td[i].textContent*1 && i==x) {
td[i].classList.add('green');
if(++x == 15) alert('Победа');
}
else td[i].classList.remove('green');
}
})();
td.forEach((el,i) => el.onclick = function(){
if ((i!=0 && i!=4 && i!=8 && i!=12 && td[i-1].matches('.cl')) || (i!=15 && i!=11 && i!=7 && i!=3 && td[i+1].matches('.cl')) || (i<12 && td[i+4].matches('.cl')) ||(i>3 && td[i-4].matches('.cl')) ) fn2(this);
});
</script>
</body>
</html>
|