回答編集履歴
2
追記
test
CHANGED
@@ -223,3 +223,195 @@
|
|
223
223
|
|
224
224
|
|
225
225
|
```
|
226
|
+
|
227
|
+
**再追記**
|
228
|
+
|
229
|
+
バカなことに神経衰弱を理解していませんでした。
|
230
|
+
|
231
|
+
以下のコードで出来るかと思います。
|
232
|
+
|
233
|
+
```Javascript
|
234
|
+
|
235
|
+
(function () {
|
236
|
+
|
237
|
+
"use strict";
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
var pairs = 2;
|
242
|
+
|
243
|
+
var cards = [];
|
244
|
+
|
245
|
+
var flipCount = 0;
|
246
|
+
|
247
|
+
var firstCard = null;
|
248
|
+
|
249
|
+
var secondCard = null;
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
//組み合わせたいもの書いてください
|
254
|
+
|
255
|
+
var cocktails = [
|
256
|
+
|
257
|
+
["トマトジュース", "ウォッカ"], // ["ウォッカ", "トマトジュース"]の順番でもOKです
|
258
|
+
|
259
|
+
["ルジェカシス", "オレンジジュース"], // ["オレンジジュース", "ルジェカシス"]の順番でもOKです
|
260
|
+
|
261
|
+
["ピーチツリー", "ソーダ"],
|
262
|
+
|
263
|
+
];
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
function init() {
|
268
|
+
|
269
|
+
// var i;
|
270
|
+
|
271
|
+
var card;
|
272
|
+
|
273
|
+
// for (i = 1; i <= pairs; i++) {
|
274
|
+
|
275
|
+
cocktails.forEach(function (drinks) {
|
276
|
+
|
277
|
+
drinks.forEach(function (drink) {
|
278
|
+
|
279
|
+
cards.push(createCard(drink));
|
280
|
+
|
281
|
+
});
|
282
|
+
|
283
|
+
});
|
284
|
+
|
285
|
+
//cards.push(createCard("トマトジュース"));
|
286
|
+
|
287
|
+
//cards.push(createCard("ウォッカ"));
|
288
|
+
|
289
|
+
//cards.push(createCard("ルジェカシス"));
|
290
|
+
|
291
|
+
//cards.push(createCard("オレンジジュース"));
|
292
|
+
|
293
|
+
// document.getElementById('stage').appendChild(createCard('トマトジュース'));
|
294
|
+
|
295
|
+
// document.getElementById('stage').appendChild(createCard('ウォッカ'));
|
296
|
+
|
297
|
+
// document.getElementById('stage').appendChild(createCard('ルジェカシス'));
|
298
|
+
|
299
|
+
// document.getElementById('stage').appendChild(createCard('オレンジジュース'));
|
300
|
+
|
301
|
+
// document.getElementById('stage').appendChild(createCard('ピーチツリー'));
|
302
|
+
|
303
|
+
// document.getElementById('stage').appendChild(createCard('ソーダ'));
|
304
|
+
|
305
|
+
// }
|
306
|
+
|
307
|
+
while (cards.length) {
|
308
|
+
|
309
|
+
card = cards.splice(Math.floor(Math.random() * cards.length), 1)[0];
|
310
|
+
|
311
|
+
document.getElementById("stage").appendChild(card);
|
312
|
+
|
313
|
+
}
|
314
|
+
|
315
|
+
}
|
316
|
+
|
317
|
+
|
318
|
+
|
319
|
+
function createCard(num) {
|
320
|
+
|
321
|
+
var container;
|
322
|
+
|
323
|
+
var card;
|
324
|
+
|
325
|
+
var inner;
|
326
|
+
|
327
|
+
inner = '<div class="card-front">' + num + '</div><div class="card-back">?</div>';
|
328
|
+
|
329
|
+
card = document.createElement("div");
|
330
|
+
|
331
|
+
card.innerHTML = inner;
|
332
|
+
|
333
|
+
card.className = "card";
|
334
|
+
|
335
|
+
card.addEventListener("click", function () {
|
336
|
+
|
337
|
+
flipCard(this);
|
338
|
+
|
339
|
+
});
|
340
|
+
|
341
|
+
container = document.createElement("div");
|
342
|
+
|
343
|
+
container.className = "card-container";
|
344
|
+
|
345
|
+
container.appendChild(card);
|
346
|
+
|
347
|
+
return container;
|
348
|
+
|
349
|
+
}
|
350
|
+
|
351
|
+
|
352
|
+
|
353
|
+
function flipCard(card) {
|
354
|
+
|
355
|
+
if (firstCard !== null && secondCard !== null) {
|
356
|
+
|
357
|
+
return;
|
358
|
+
|
359
|
+
}
|
360
|
+
|
361
|
+
card.className = "card open";
|
362
|
+
|
363
|
+
flipCount++;
|
364
|
+
|
365
|
+
if (flipCount % 2 === 1) {
|
366
|
+
|
367
|
+
firstCard = card;
|
368
|
+
|
369
|
+
} else {
|
370
|
+
|
371
|
+
secondCard = card;
|
372
|
+
|
373
|
+
secondCard.addEventListener("transitionend", check);
|
374
|
+
|
375
|
+
}
|
376
|
+
|
377
|
+
}
|
378
|
+
|
379
|
+
|
380
|
+
|
381
|
+
function check() {
|
382
|
+
|
383
|
+
cocktails.some(function (drinks) {
|
384
|
+
|
385
|
+
if (drinks.indexOf(firstCard.children[0].textContent.trim()) >= 0 && drinks.indexOf(secondCard.children[0].textContent.trim()) >= 0) {
|
386
|
+
|
387
|
+
firstCard.className = "card open";
|
388
|
+
|
389
|
+
secondCard.className = "card open";
|
390
|
+
|
391
|
+
return true;
|
392
|
+
|
393
|
+
} else {
|
394
|
+
|
395
|
+
firstCard.className = "card";
|
396
|
+
|
397
|
+
secondCard.className = "card";
|
398
|
+
|
399
|
+
}
|
400
|
+
|
401
|
+
});
|
402
|
+
|
403
|
+
secondCard.removeEventListener("transitionend", check);
|
404
|
+
|
405
|
+
firstCard = null;
|
406
|
+
|
407
|
+
secondCard = null;
|
408
|
+
|
409
|
+
}
|
410
|
+
|
411
|
+
|
412
|
+
|
413
|
+
init();
|
414
|
+
|
415
|
+
})();
|
416
|
+
|
417
|
+
```
|
1
追記
test
CHANGED
@@ -37,3 +37,189 @@
|
|
37
37
|
}
|
38
38
|
|
39
39
|
```
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
**追記**
|
44
|
+
|
45
|
+
Javascriptを以下のようにすればもっとスマート(管理がしやすく)に出来ると思われます。
|
46
|
+
|
47
|
+
```Javascript
|
48
|
+
|
49
|
+
(function () {
|
50
|
+
|
51
|
+
"use strict";
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
var pairs = 2;
|
56
|
+
|
57
|
+
var cards = [];
|
58
|
+
|
59
|
+
var flipCount = 0;
|
60
|
+
|
61
|
+
var firstCard = null;
|
62
|
+
|
63
|
+
var secondCard = null;
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
//組み合わせたいもの書いてください
|
68
|
+
|
69
|
+
var cocktails = [
|
70
|
+
|
71
|
+
["トマトジュース", "ウォッカ"], // ["ウォッカ", "トマトジュース"]の順番でもOKです
|
72
|
+
|
73
|
+
["ルジェカシス", "オレンジジュース"], // ["オレンジジュース", "ルジェカシス"]の順番でもOKです
|
74
|
+
|
75
|
+
["ピーチツリー", "ソーダ"],
|
76
|
+
|
77
|
+
];
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
function init() {
|
82
|
+
|
83
|
+
// var i;
|
84
|
+
|
85
|
+
var card;
|
86
|
+
|
87
|
+
// for (i = 1; i <= pairs; i++) {
|
88
|
+
|
89
|
+
cocktails.forEach(function (drinks) {
|
90
|
+
|
91
|
+
drinks.forEach(function (drink) {
|
92
|
+
|
93
|
+
cards.push(createCard(drink));
|
94
|
+
|
95
|
+
});
|
96
|
+
|
97
|
+
});
|
98
|
+
|
99
|
+
//cards.push(createCard("トマトジュース"));
|
100
|
+
|
101
|
+
//cards.push(createCard("ウォッカ"));
|
102
|
+
|
103
|
+
//cards.push(createCard("ルジェカシス"));
|
104
|
+
|
105
|
+
//cards.push(createCard("オレンジジュース"));
|
106
|
+
|
107
|
+
// document.getElementById('stage').appendChild(createCard('トマトジュース'));
|
108
|
+
|
109
|
+
// document.getElementById('stage').appendChild(createCard('ウォッカ'));
|
110
|
+
|
111
|
+
// document.getElementById('stage').appendChild(createCard('ルジェカシス'));
|
112
|
+
|
113
|
+
// document.getElementById('stage').appendChild(createCard('オレンジジュース'));
|
114
|
+
|
115
|
+
// document.getElementById('stage').appendChild(createCard('ピーチツリー'));
|
116
|
+
|
117
|
+
// document.getElementById('stage').appendChild(createCard('ソーダ'));
|
118
|
+
|
119
|
+
// }
|
120
|
+
|
121
|
+
while (cards.length) {
|
122
|
+
|
123
|
+
card = cards.splice(Math.floor(Math.random() * cards.length), 1)[0];
|
124
|
+
|
125
|
+
document.getElementById("stage").appendChild(card);
|
126
|
+
|
127
|
+
}
|
128
|
+
|
129
|
+
}
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
function createCard(num) {
|
134
|
+
|
135
|
+
var container;
|
136
|
+
|
137
|
+
var card;
|
138
|
+
|
139
|
+
var inner;
|
140
|
+
|
141
|
+
inner = '<div class="card-front">' + num + '</div><div class="card-back">?</div>';
|
142
|
+
|
143
|
+
card = document.createElement("div");
|
144
|
+
|
145
|
+
card.innerHTML = inner;
|
146
|
+
|
147
|
+
card.className = "card";
|
148
|
+
|
149
|
+
card.addEventListener("click", function () {
|
150
|
+
|
151
|
+
flipCard(this);
|
152
|
+
|
153
|
+
});
|
154
|
+
|
155
|
+
container = document.createElement("div");
|
156
|
+
|
157
|
+
container.className = "card-container";
|
158
|
+
|
159
|
+
container.appendChild(card);
|
160
|
+
|
161
|
+
return container;
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
function flipCard(card) {
|
168
|
+
|
169
|
+
if (firstCard !== null && secondCard !== null) {
|
170
|
+
|
171
|
+
return;
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
card.className = "card open";
|
176
|
+
|
177
|
+
flipCount++;
|
178
|
+
|
179
|
+
if (flipCount % 2 === 1) {
|
180
|
+
|
181
|
+
firstCard = card;
|
182
|
+
|
183
|
+
} else {
|
184
|
+
|
185
|
+
secondCard = card;
|
186
|
+
|
187
|
+
secondCard.addEventListener("transitionend", check);
|
188
|
+
|
189
|
+
}
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
function check() {
|
196
|
+
|
197
|
+
cocktails.forEach(function (drinks) {
|
198
|
+
|
199
|
+
if (drinks.indexOf(firstCard.children[0].textContent.trim()) >= 0 && drinks.indexOf(secondCard.children[0].textContent.trim()) >= 0) {
|
200
|
+
|
201
|
+
firstCard.className = "card";
|
202
|
+
|
203
|
+
secondCard.className = "card";
|
204
|
+
|
205
|
+
}
|
206
|
+
|
207
|
+
});
|
208
|
+
|
209
|
+
secondCard.removeEventListener("transitionend", check);
|
210
|
+
|
211
|
+
firstCard = null;
|
212
|
+
|
213
|
+
secondCard = null;
|
214
|
+
|
215
|
+
}
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
init();
|
220
|
+
|
221
|
+
})();
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
```
|