-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
205 lines (167 loc) · 4.62 KB
/
index.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<!doctype html>
<html class="no-js" lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
<title>Simple taquin Game with web component</title>
</head>
<body>
<h2>Taquin 3x3</h2>
<tq-grid data-size="3"
data-value="[1, 2, 3, 0, 4, 5, 7, 8, 6]">
</tq-grid>
<br>
<h2>Taquin 4x4</h2>
<tq-grid data-size="4"
data-value="[1, 2, 3, 0, 4, 5, 7, 8, 6, 9, 10, 11, 12, 13, 14, 15]">
</tq-grid>
<!--
Template associé au web component tq-grid
-->
<template id="tq-grid-tmpl">
<style>
div {
display: flex;
flex-wrap: wrap;
}
</style>
<div></div>
</template>
<!--
Définition de l'élément grille du taquin.
-->
<script>
class TaquinGrid extends HTMLElement {
constructor () {
super();
var shadow = this.attachShadow({
mode: 'open'
}),
// Extraction et parsing du tableau d'élément à afficher
grid = JSON.parse(this.getAttribute('data-value')),
// Extraction de la taille de la grille de jeux
size = +this.getAttribute('data-size'),
// Extraction du template associé au component
tmplElem = document.getElementById('tq-grid-tmpl'),
// Clone du contenu du template
content = document.importNode(tmplElem.content, true);
shadow.appendChild(content);
// Le wrapper est utilisé pour avoir un binding (optimisation)
var wrapperElem = shadow.querySelector('div');
for (let i = 0, length = grid.length; i < length; i++) {
var value = grid[i],
tqItem = document.createElement('tq-item');
tqItem.dataset.x = i % size + 1;
tqItem.dataset.y = (i - i % size) / size + 1;
tqItem.dataset.value = value;
tqItem.style['min-width'] = (10 * Math.floor(10 / size)) + '%';
wrapperElem.appendChild(tqItem);
}
wrapperElem.onclick = this.onClick.bind(this);
}
onClick (event) {
var target = event.target,
x = +target.dataset.x,
y = +target.dataset.y;
if (target instanceof TaquinItem && this.isMovable(x, y)) {
var emptyItem = this.getEmptyItem();
emptyItem.dataset.value = target.dataset.value;
target.dataset.value = 0;
}
if (this.isDone()) {
alert('You win !');
}
}
getEmptyItem () {
return this.shadowRoot.querySelector('tq-item[data-value="0"]');
}
getEmptyPosition () {
var emptyItem = this.getEmptyItem();
return {
x: +emptyItem.dataset.x,
y: +emptyItem.dataset.y
};
}
isMovable (x, y) {
var response = false,
{x: x0, y: y0} = this.getEmptyPosition();
if (x === x0) {
response = this.isAdjacent(y, y0);
} else if (y === y0) {
response = this.isAdjacent(x, x0);
}
return response;
}
isAdjacent (a, b) {
return Math.abs(a - b) === 1;
}
isDone () {
var itemList = this.shadowRoot.querySelectorAll('tq-item'),
response = true;
for (let i = 0, length = itemList.length; i < length - 2; i++) {
if (itemList[i].dataset.value > itemList[i + 1].dataset.value) {
response = false;
break;
}
}
return response;
}
};
customElements.define('tq-grid', TaquinGrid);
</script>
<!--
Template associé au web component tq-item
-->
<template id="tq-item-tmpl">
<style>
:host {
flex: 1;
border-width: 1px;
border-style: solid;
margin-right: 1px;
margin-bottom: 1px;
}
p {
text-align: center;
}
</style>
<p></p>
</template>
<!--
Définition de l'élément item du taquin.
-->
<script>
class TaquinItem extends HTMLElement {
static get observedAttributes () {
return [
'data-value'
];
}
constructor () {
super();
var shadow = this.attachShadow({
mode: 'open'
}),
// Extraction du template associé au component
tmplElem = document.getElementById('tq-item-tmpl'),
// Clone du contenu du template
content = document.importNode(tmplElem.content, true),
// Extraction de la valeur à afficher
value = this.getAttribute('data-value'),
// Déclaration du noeud de texte
pNode;
shadow.appendChild(content);
pNode = this.shadowRoot.querySelector('p');
pNode.innerHTML = value !== '0' ? value : '';
}
attributeChangedCallback(attr, oldValue, newValue) {
if (attr === 'data-value') {
var pNode = this.shadowRoot.querySelector('p');
pNode.innerHTML = newValue !== '0' ? newValue : '';
}
}
};
customElements.define('tq-item', TaquinItem);
</script>
</body>
</html>