teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

追記

2020/06/07 06:22

投稿

fake_shibe
fake_shibe

スコア806

answer CHANGED
@@ -110,4 +110,100 @@
110
110
  init();
111
111
  })();
112
112
 
113
+ ```
114
+ **再追記**
115
+ バカなことに神経衰弱を理解していませんでした。
116
+ 以下のコードで出来るかと思います。
117
+ ```Javascript
118
+ (function () {
119
+ "use strict";
120
+
121
+ var pairs = 2;
122
+ var cards = [];
123
+ var flipCount = 0;
124
+ var firstCard = null;
125
+ var secondCard = null;
126
+
127
+ //組み合わせたいもの書いてください
128
+ var cocktails = [
129
+ ["トマトジュース", "ウォッカ"], // ["ウォッカ", "トマトジュース"]の順番でもOKです
130
+ ["ルジェカシス", "オレンジジュース"], // ["オレンジジュース", "ルジェカシス"]の順番でもOKです
131
+ ["ピーチツリー", "ソーダ"],
132
+ ];
133
+
134
+ function init() {
135
+ // var i;
136
+ var card;
137
+ // for (i = 1; i <= pairs; i++) {
138
+ cocktails.forEach(function (drinks) {
139
+ drinks.forEach(function (drink) {
140
+ cards.push(createCard(drink));
141
+ });
142
+ });
143
+ //cards.push(createCard("トマトジュース"));
144
+ //cards.push(createCard("ウォッカ"));
145
+ //cards.push(createCard("ルジェカシス"));
146
+ //cards.push(createCard("オレンジジュース"));
147
+ // document.getElementById('stage').appendChild(createCard('トマトジュース'));
148
+ // document.getElementById('stage').appendChild(createCard('ウォッカ'));
149
+ // document.getElementById('stage').appendChild(createCard('ルジェカシス'));
150
+ // document.getElementById('stage').appendChild(createCard('オレンジジュース'));
151
+ // document.getElementById('stage').appendChild(createCard('ピーチツリー'));
152
+ // document.getElementById('stage').appendChild(createCard('ソーダ'));
153
+ // }
154
+ while (cards.length) {
155
+ card = cards.splice(Math.floor(Math.random() * cards.length), 1)[0];
156
+ document.getElementById("stage").appendChild(card);
157
+ }
158
+ }
159
+
160
+ function createCard(num) {
161
+ var container;
162
+ var card;
163
+ var inner;
164
+ inner = '<div class="card-front">' + num + '</div><div class="card-back">?</div>';
165
+ card = document.createElement("div");
166
+ card.innerHTML = inner;
167
+ card.className = "card";
168
+ card.addEventListener("click", function () {
169
+ flipCard(this);
170
+ });
171
+ container = document.createElement("div");
172
+ container.className = "card-container";
173
+ container.appendChild(card);
174
+ return container;
175
+ }
176
+
177
+ function flipCard(card) {
178
+ if (firstCard !== null && secondCard !== null) {
179
+ return;
180
+ }
181
+ card.className = "card open";
182
+ flipCount++;
183
+ if (flipCount % 2 === 1) {
184
+ firstCard = card;
185
+ } else {
186
+ secondCard = card;
187
+ secondCard.addEventListener("transitionend", check);
188
+ }
189
+ }
190
+
191
+ function check() {
192
+ cocktails.some(function (drinks) {
193
+ if (drinks.indexOf(firstCard.children[0].textContent.trim()) >= 0 && drinks.indexOf(secondCard.children[0].textContent.trim()) >= 0) {
194
+ firstCard.className = "card open";
195
+ secondCard.className = "card open";
196
+ return true;
197
+ } else {
198
+ firstCard.className = "card";
199
+ secondCard.className = "card";
200
+ }
201
+ });
202
+ secondCard.removeEventListener("transitionend", check);
203
+ firstCard = null;
204
+ secondCard = null;
205
+ }
206
+
207
+ init();
208
+ })();
113
209
  ```

1

追記

2020/06/07 06:22

投稿

fake_shibe
fake_shibe

スコア806

answer CHANGED
@@ -17,4 +17,97 @@
17
17
  firstCard = null;
18
18
  secondCard = null;
19
19
  }
20
+ ```
21
+
22
+ **追記**
23
+ Javascriptを以下のようにすればもっとスマート(管理がしやすく)に出来ると思われます。
24
+ ```Javascript
25
+ (function () {
26
+ "use strict";
27
+
28
+ var pairs = 2;
29
+ var cards = [];
30
+ var flipCount = 0;
31
+ var firstCard = null;
32
+ var secondCard = null;
33
+
34
+ //組み合わせたいもの書いてください
35
+ var cocktails = [
36
+ ["トマトジュース", "ウォッカ"], // ["ウォッカ", "トマトジュース"]の順番でもOKです
37
+ ["ルジェカシス", "オレンジジュース"], // ["オレンジジュース", "ルジェカシス"]の順番でもOKです
38
+ ["ピーチツリー", "ソーダ"],
39
+ ];
40
+
41
+ function init() {
42
+ // var i;
43
+ var card;
44
+ // for (i = 1; i <= pairs; i++) {
45
+ cocktails.forEach(function (drinks) {
46
+ drinks.forEach(function (drink) {
47
+ cards.push(createCard(drink));
48
+ });
49
+ });
50
+ //cards.push(createCard("トマトジュース"));
51
+ //cards.push(createCard("ウォッカ"));
52
+ //cards.push(createCard("ルジェカシス"));
53
+ //cards.push(createCard("オレンジジュース"));
54
+ // document.getElementById('stage').appendChild(createCard('トマトジュース'));
55
+ // document.getElementById('stage').appendChild(createCard('ウォッカ'));
56
+ // document.getElementById('stage').appendChild(createCard('ルジェカシス'));
57
+ // document.getElementById('stage').appendChild(createCard('オレンジジュース'));
58
+ // document.getElementById('stage').appendChild(createCard('ピーチツリー'));
59
+ // document.getElementById('stage').appendChild(createCard('ソーダ'));
60
+ // }
61
+ while (cards.length) {
62
+ card = cards.splice(Math.floor(Math.random() * cards.length), 1)[0];
63
+ document.getElementById("stage").appendChild(card);
64
+ }
65
+ }
66
+
67
+ function createCard(num) {
68
+ var container;
69
+ var card;
70
+ var inner;
71
+ inner = '<div class="card-front">' + num + '</div><div class="card-back">?</div>';
72
+ card = document.createElement("div");
73
+ card.innerHTML = inner;
74
+ card.className = "card";
75
+ card.addEventListener("click", function () {
76
+ flipCard(this);
77
+ });
78
+ container = document.createElement("div");
79
+ container.className = "card-container";
80
+ container.appendChild(card);
81
+ return container;
82
+ }
83
+
84
+ function flipCard(card) {
85
+ if (firstCard !== null && secondCard !== null) {
86
+ return;
87
+ }
88
+ card.className = "card open";
89
+ flipCount++;
90
+ if (flipCount % 2 === 1) {
91
+ firstCard = card;
92
+ } else {
93
+ secondCard = card;
94
+ secondCard.addEventListener("transitionend", check);
95
+ }
96
+ }
97
+
98
+ function check() {
99
+ cocktails.forEach(function (drinks) {
100
+ if (drinks.indexOf(firstCard.children[0].textContent.trim()) >= 0 && drinks.indexOf(secondCard.children[0].textContent.trim()) >= 0) {
101
+ firstCard.className = "card";
102
+ secondCard.className = "card";
103
+ }
104
+ });
105
+ secondCard.removeEventListener("transitionend", check);
106
+ firstCard = null;
107
+ secondCard = null;
108
+ }
109
+
110
+ init();
111
+ })();
112
+
20
113
  ```