-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
69 lines (60 loc) · 1.61 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const table = document.querySelector("table")
const div = document.querySelector("div")
let arr = ['']
let hole = []
for (let i = 1; i <= 15; i++) {
arr.push(i)
}
arr.sort((a, b) => 0.5 - Math.random());
cedvel()
function swipe(i, j) {
let cell0 = document.getElementById(hole.join(''))
let cell1 = document.getElementById([i,j].join(''))
if (i == hole[0] && Math.abs(j - hole[1]) == 1 ||
j == hole[1] && Math.abs(i - hole[0]) == 1 ) {
cell0.innerHTML = cell1.innerHTML
cell1.innerHTML = ''
hole = [i, j]
}
if (isGameFinished()) gameOver();
//* For check gameOver() method :)
// else gameOver();
}
function cedvel() {
const n = 4;
let kod = '';
let x = 0;
for (let i = 1; i <= n; i++) {
kod += '<tr>'
for (let j = 1; j <= n; j++) {
if(arr[x] == '') hole = [i, j]
kod += `<td id="${i}${j}" onclick="swipe(${i}, ${j})">${arr[x]}</td>`;
x++;
}
kod += '</tr>'
}
table.innerHTML = kod
}
function isGameFinished() {
const cells = table.querySelectorAll("td");
for (let i = 0; i < cells.length; i++) {
const cellValue = +cells[i].innerHTML;
if (cellValue !== i + 1) {
return false;
}
}
return true;
}
function gameOver() {
const str="GameOver";
kod='';
let c=0;
for (let i = 0; i < 4; i++) {
kod+= `<tr class="end">`;
for (let j = 0; j <4; j++) {
kod+= `<td> ${i > 0 && i < 3 ? str[c++] : ''}</td>`
}
kod+=`</tr>`
}
table.innerHTML = kod
}