-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathChessboard.html
130 lines (111 loc) · 4.01 KB
/
Chessboard.html
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Chessboard</title>
<style>
.container {
display: grid;
grid-template-columns: repeat(8, 40px);
border: 2px solid black;
border-radius: 0.3rem;
width: max-content;
padding: 2px;
}
.item {
height: 40px;
cursor: pointer;
}
.bg-white {
color: black;
background-color: white;
}
.bg-black {
color: white;
background-color: black;
}
.bg-red {
background-color: red !important;
}
</style>
</head>
<body>
<h1>Chessboard</h1>
<div id="container" class="container"></div>
<script>
function Chessbord(el, n) {
let container = document.querySelector(el);
let boardFragmentContainer = document.createDocumentFragment();
let lastIJObj = null;
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
let item = document.createElement("div");
item.classList.add("item");
if (isWhiteTile(i, j)) item.classList.add("bg-white");
else item.classList.add("bg-black");
item.dataset.i = i;
item.dataset.j = j;
// item.innerText = ${i} x ${j};
boardFragmentContainer.append(item);
}
}
container.appendChild(boardFragmentContainer);
function highlightTile(i, j) {
let index = getIndex(i, j);
container.children[index].classList.add("bg-red");
}
function resetTile(i, j) {
let index = getIndex(i, j);
if (isWhiteTile(i, j))
container.children[index].classList.add("bg-white");
else container.children[index].classList.add("bg-black");
container.children[index].classList.remove("bg-red");
}
function onContainerClick(e) {
let {
i,
j
} = e.target.dataset;
if (i !== undefined && j !== undefined) {
if (lastIJObj)
highlightDiagonalTiles(lastIJObj.i, lastIJObj.j, resetTile);
highlightDiagonalTiles(Number(i), Number(j), highlightTile);
lastIJObj = {
i: Number(i),
j: Number(j)
};
}
}
function isWhiteTile(i, j) {
return (i % 2 === 0 && j % 2 === 0) || (i % 2 !== 0 && j % 2 !== 0);
}
function getIndex(i, j) {
return i * n + j;
}
function highlightDiagonalTiles(i, j, cb = () => {}) {
let startI = i - j < 0 ? 0 : i - j;
let startJ = j - i < 0 ? 0 : j - i;
let index;
for (let l = startI, k = startJ; l < n; l++, k++) {
index = getIndex(l, k);
if (container.children[index] && l < 8 && k < 8) {
cb(l, k);
} else break;
}
startI = i + j < 8 ? 0 : i + j - 7;
startJ = i + j > 8 ? 7 : i + j;
for (let l = startI, k = startJ; l < n; l++, k--) {
index = getIndex(l, k);
if (container.children[index] && l < 8 && k >= 0) {
cb(l, k);
} else break;
}
}
container.addEventListener("click", onContainerClick);
}
new Chessbord("#container", 8);
</script>
</body>
</html>