-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
288 lines (272 loc) · 9.39 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<!DOCTYPE html>
<!--[if IE 8]> <html lang="en" class="ie8"> <![endif]-->
<!--[if !IE]><!-->
<html lang="en">
<!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=1" />
<title>Memory</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/tether/1.4.0/js/tether.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>
<head>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
</head>
<style>
#nav-bar img {
height: 40px;
width: 40px;
}
#nav-modal img {
height: 100px;
width: 100px;
}
header, #play, #count, #source, nav {
max-width: 400px;
width:100%;
margin: 0 auto;
font-family: 'Helvetica Neue',sans-serif !important;
font-size: 14px !important;
text-align: center;
z-index:1;
background-color: rgb(255,255,255,0.5);
left: 0;
right: 0;
}
header form {
margin: 0px;
margin-bottom: 5px;
padding: 5px;
background-color: white;
}
header {
position: fixed;
top: 0;
}
table tr td:hover {
cursor: pointer;
}
table tr td {
border: solid 10px white;
box-shadow: 0 0 1px #fff inset;
background-position: center;
background-size: cover;
background-color: lightgray;
}
#count {
font-size: 20px!important;
font-weight: bold;
}
.icon-ok{
font-size: 30px;
color: lightgreen;
}
.modal-title {
width: 100%;
text-transform: uppercase;
}
</style>
</head>
<body>
<div class="d-flex flex-column flex-md-row align-items-center p-3 px-md-4 mb-3 bg-white border-bottom box-shadow">
<h5 class="my-0 mr-md-auto font-weight-normal"><a class="p-2 text-dark" href="/" title="Home"><i class="large material-icons">replay</i></a></h5>
<nav id="nav-bar">
</nav>
</div>
<div id="count" data-count=0>COUPS: 0</div>
<div id="play"></div>
<div id="source"></div>
<div class="modal fade bd-example-modal-lg" id="popup-choice" tabindex="-1" role="dialog" aria-labelledby="popup-choice-label" aria-hidden="true" width="100%">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<i class="icon-ok material-icons" id="popup-choice-icon">view_comfy</i>
<h5 class="modal-title text-center" id="popup-choice-label">Bienvenue sur le jeu Memory</h5>
</div>
<div class="modal-body">
<nav id="nav-modal">
</nav>
</div>
<div class="modal-footer valign-wrapper">
<i class="material-icons" style="font-size: 20px;padding-top: 5px; color:gray;">child_care</i>
<span style="color:gray;"><small>Site non commercial et <a href="https://github.com/SergeBredin/jeux">open source</a></small></span>
</div>
</div>
</div>
</div>
<noscript>
<div id="page-container">
<div class="text-center p-t-30 p-b-20 bg-white">
<h4 class="m-t-20 text-default"><em>Cette page nécessite l'activation du javascript dans votre navigateur.</em></h4>
</div>
</div>
</noscript>
</body>
<script type="text/javascript">
var data = undefined;
var game = "ninjago";//"marvel";//
var url_ref = window.location.protocol + '//' + window.location.host + window.location.pathname;
$.getJSON("https://sergebredin.github.io/jeux/memory.json", function(json) {
data = json;
// console.log(data)
// Init Nav bar
document.getElementById("nav-bar").innerHTML = "";
document.getElementById("nav-modal").innerHTML = "";
var strNav = "<div width='100%'>";
Object.keys(data)
.forEach(function(game){
strNav += "<a class=\"p-2 text-dark\" href=\""+new URL("?game="+game,url_ref)+"\">";
strNav += "<img src=\""+data[game]["images"][0]+"\"/>"
strNav += "</a>"
});
strNav += "</div>";
document.getElementById("nav-bar").innerHTML = strNav;
document.getElementById("nav-modal").innerHTML = strNav;
// Detect game in url
const urlParams = new URLSearchParams(window.location.search);
if( urlParams.get("game") &&
Object.keys(data).includes(urlParams.get("game"))){
game = urlParams.get("game");
}
else {
$("#popup-choice").modal();
}
var dimensions = data[game]["dimensions"];
// Init game play
function initMemory(rows,cols,size){
document.getElementById("play").innerHTML = "";
var taille = size.toString();
var nbImages = data[game]["images"].length;
var limit = rows*cols/2;
var indexes = [];
var positions = []
for (var pos = 0; pos<limit*2; ++pos){
indexes.push(0);
positions.push(pos);
}
for (var img = 0; img<limit; ++img){
for(var doublon=0; doublon<2; ++doublon){
pos = Math.floor(Math.random() * positions.length);
indexes[positions[pos]]=img;
positions.splice(pos,1);
}
}
var strTable = "<table>";
for (var i = 0; i < rows; i++){
strTable += "<tr>";
for (var j = 0; j < cols; j++){
var num = (i*cols+j);
var indexImage = indexes[num];
url = data[game]["images"][indexImage];
strTable += "<td class='carte'";
strTable += " id='carte-"+num.toString()+"'";
strTable += " style='height:"+taille+"px; width:"+taille+"px;'";
strTable += " data-value="+indexImage;
strTable += " data-num="+num.toString();
strTable += " data-status=0";
strTable += " onclick=clickCarte("+num+")>";
strTable += "</td>";
}
strTable += "</tr>";
}
strTable += "</table>";
document.getElementById("play").innerHTML = strTable;
var strSource = "<span><small>source";
strSource += "<a class=\"p-2 text-dark\" href=\""+data[game]["source"]["url"]+"\">";
strSource += data[game]["source"]["nom"];
strSource += "</a>"
strSource += "</small></span>";
document.getElementById("source").innerHTML = strSource;
}
initMemory(dimensions["lignes"],dimensions["colonnes"],dimensions["taille"]);
});
function clickCarte(numero){
var carte = document.getElementById("carte-"+numero.toString());
var val = carte.dataset.value;
var statut = carte.dataset.status;
// On gère avec le statut
// 0 = Carte cachée
// 1 = 1ère carte retournée
// 2 = Carte trouvée
if(statut == 0){
// 2 cas: il existe une premère carte retournée ou non
var prems = undefined;
Array.from(document.getElementsByClassName("carte"))
.forEach(function(td){
if(td.dataset.status == 1){
prems = td;
}
});
// On retourne
var url = data[game]["images"][val];
carte.style["background-image"] = "url(\""+url+"\")";
carte.style["background-color"]= "white";
// 1) Il n'existe pas de premère carte retournée
if(!prems){
carte.dataset.status = 1;
}
// 2) Il existe une premère carte retournée
else {
// 2.1) Si ces cartes sont les mêmes
// On les affiche et met leur status à "trouvé"
if(prems.dataset.value == val){
prems.innerHTML = "<i class='icon-ok large material-icons'>done</i>";
prems.dataset.status = 2;
carte.innerHTML = "<i class='icon-ok large material-icons'>done</i>";
carte.dataset.status = 2;
setTimeout(() => {
carte.textContent = '';
prems.textContent = '';
rafraichirCompteur();
}, 1000);
}
// 2.2) Si ces cartes ne sont pas les mêmes
// On les re-retourne et met leur status à "caché"
else {
setTimeout(() => {
carte.style["background-image"] = null;
carte.style["background-color"]= "lightgray";
carte.dataset.status = 0;
carte.textContent = '';
prems.style["background-image"] = null;
prems.style["background-color"]= "lightgray";
prems.dataset.status = 0;
prems.textContent = '';
rafraichirCompteur();
}, 1000);
}
function rafraichirCompteur(){
// On augmente le compteur
var nodeCount = document.getElementById("count");
var count = parseInt(nodeCount.dataset.count) + 1;
nodeCount.dataset.count = count;
nodeCount.textContent = "COUPS: " + count.toString();
// On teste si c'est la fin du jeu
var nonTrouvees = Array.from(document.getElementsByClassName("carte"))
.reduce(function(sum,td){
return (td.dataset.status == 0)? sum+1:sum;
},0);
if(nonTrouvees == 0){
document.getElementById("popup-choice-label").textContent = "Bravo! Gagné en "+count.toString()+ " coups.";
document.getElementById("popup-choice-icon").textContent = "thumb_up";
$("#popup-choice").modal();
}
}
}
}
}
</script>
<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-161952364-1', 'auto');
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->
</html>