-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.js
325 lines (319 loc) · 9.81 KB
/
memory.js
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
class MatchingGame {
//picturematching game
constructor(app) {
//set up all the dom refs
this.app = document.getElementById(app);
this.scoreBox = document.getElementById('score-box');
this.timerBox = document.getElementById('timer-box');
this.playBtn = document.getElementById('playBtn');
this.playBtn.addEventListener('click', this.setupGame.bind(this));
this.subBtn = document.getElementById('highScore');
this.subBtn.addEventListener('click', this.submitHighScore.bind(this));
this.msgBox = document.getElementById('msgBox');
//Tile template for later cloning
this.tileTemplate = document.createElement('div');
this.tileTemplate.className = 'tile';
this.tileTemplate.appendChild(document.createElement('img'));
this.tileTemplate.firstChild.src = 'images/blank.png';
this.tileTemplate.firstChild.className = 'tileImg';
//set up variables to be used in the game
this.numTiles;
this.startTime;
this.gameScore = 1000;
this.gameArr;
this.lastItem;
this.matches = 0;
this.attempts = 0;
this.numArr = [
0,
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
];
this.realPicsArr = getWebPics(30);
}
setupGame() {
this.playBtn.disabled = true;
if (this.tickInterval) {
clearInterval(this.tickInterval);
}
this.numTiles = document.getElementById('diffSelect').value;
this.subBtn.style.display = 'none';
this.app.innerHTML = '';
this.matches = 0;
this.attempts = 0;
this.lastItem = undefined;
this.gameScore = 1000;
this.gameArr = this.numArr.slice(0, this.numTiles);
//this.gameArr = this.picsArr.slice(0,this.numTiles);
this.gameArr = [...this.gameArr, ...this.gameArr];
this.gameArr.sort(() => 0.5 - Math.random());
//create instances of Match Tiles
//this.gameArr = this.gameArr.map(tileName=>new MatchTile(tileName, this.tileTemplate, this));
this.gameArr = this.gameArr.map(
tileName =>
new MatchTile(
tileName,
this.tileTemplate,
this,
this.realPicsArr[tileName].src
)
);
this.gameArr.map(tile => {
this.app.appendChild(tile.imgDiv);
});
this.startCountdown(5);
}
startCountdown(secs) {
this.msgBox.innerHTML = secs ? `Starting Game in ${secs} seconds...` : '';
secs == 0
? this.startGame()
: setTimeout(() => this.startCountdown(secs - 1), 1000);
}
startGame() {
this.playBtn.disabled = false;
//flip every tile on the game board
this.gameArr.map(tile => tile.tileListen());
this.gameArr.map(tile => tile.flip());
this.startTime = Date.now();
this.tickInterval = setInterval(this.tick.bind(this), 1000);
}
itemSelected(item) {
this.lastItem
? this.checkMatch(this.lastItem, item)
: (this.lastItem = item);
}
checkMatch(item1, item2) {
this.attempts++;
this.lastItem = undefined;
if (item1.name == item2.name) {
item1.isMatched = true;
item2.isMatched = true;
this.gameScore += 5000;
this.matches++;
if (this.matches == this.numTiles) {
this.tick();
this.endGame();
}
} else {
this.gameScore -= 1000;
setTimeout(() => {
item1.isSelected = false;
item2.isSelected = false;
item1.flip();
item2.flip();
return;
}, 2000);
}
}
tick() {
let elapsed = Date.now() - this.startTime;
this.timerBox.innerHTML = `${
Math.floor(elapsed / 1000 / 60) < 10
? '0' + Math.floor(elapsed / 1000 / 60)
: Math.floor(elapsed / 1000 / 60)
}:${
Math.floor((elapsed / 1000) % 60) < 10
? '0' + Math.floor((elapsed / 1000) % 60)
: Math.floor((elapsed / 1000) % 60)
}`;
this.gameScore -= 100;
this.scoreBox.innerHTML = `Attempts: ${
this.attempts
} Matches: ${this.matches} Average: ${
Math.round((this.matches / this.attempts) * 100)
? Math.round((this.matches / this.attempts) * 100)
: 0
}% Score: ${this.gameScore} `;
}
endGame() {
this.msgBox.innerHTML = 'All Tiles Matched -- Way to Go!';
clearInterval(this.tickInterval);
document.getElementById('highScore').style.display = 'block';
}
getUsername() {
return new Promise((res, rej) => {
let initials = prompt('Please enter your initials for score', 'ABC');
if (String(initials).length < 3) {initials = String(initials).padEnd(3, 'A');}
if (initials.length > 0) res(initials);
else rej('please give input');
});
}
submitHighScore() {
let jsonHigh;
this.subBtn.style.display = 'none';
const xhr = new XMLHttpRequest();
xhr.onload = function() {
jsonHigh = JSON.parse(xhr.responseText);
let highBox = document.createElement('div');
highBox.style =
'display: block; position: absolute; z-index: 2; background-color: white; border: 1px solid black; border-radius: 10px; left: 45%; top:15%; width=35%; height: auto;';
let scoreTable = document.createElement('table');
scoreTable.style.margin = '20px auto';
let headerRow = document.createElement('tr');
let headings = ['Initials', 'Score'];
headings.map(heading => {
let tr = document.createElement('tr');
let th = document.createElement('th');
th.innerHTML = heading;
headerRow.appendChild(th);
return th;
});
scoreTable.appendChild(headerRow);
jsonHigh.map(score => {
let row = document.createElement('tr');
if (score.user == true) {
row.style.backgroundColor = 'red';
row.style.color = 'white';
}
let name = document.createElement('td');
name.innerText = score.INITIALS;
row.appendChild(name);
let scoreValue = document.createElement('td');
scoreValue.innerText = score.SCORE;
row.appendChild(scoreValue);
scoreTable.appendChild(row);
});
highBox.appendChild(scoreTable);
let closeBtn = document.createElement('button');
let app = document.getElementById('app');
closeBtn.addEventListener('click', () => app.removeChild(highBox));
closeBtn.innerText = 'Close';
highBox.appendChild(closeBtn);
app.appendChild(highBox);
};
this.getUsername()
.then(initials => {
let url = `highscore.php?INITIALS=${initials.slice(0,3)}&SCORE=${Number(this.gameScore)}`;
url = encodeURI(url);
xhr.open('GET', url, true);
xhr.send();
})
.catch(err => console.log('issue with input'));
}
}
class MatchTile {
constructor(name, template, parent, imgSrc) {
this.parent = parent;
this.name = name;
this.imgSrc = imgSrc;
this.flipped = false;
this.isMatched = false;
this.isSelected = false;
this.imgDiv = template.cloneNode(true);
this.imgDiv.firstChild.src = this.imgSrc;
}
tileListen() {
this.imgDiv.addEventListener('click', this.wasClicked.bind(this));
}
wasClicked() {
if (!this.isMatched && !this.isSelected) {
this.flip();
this.isSelected = true;
this.parent.itemSelected(this);
}
}
flip() {
this.flipped = !this.flipped;
//Do swap image flipping based on flip logic.
this.imgDiv.classList.add('flip-vertical-edge');
setTimeout(
() =>
this.flipped
? (this.imgDiv.firstChild.src = 'images/blank.png')
: (this.imgDiv.firstChild.src = this.imgSrc),
100
);
setTimeout(() => this.imgDiv.classList.remove('flip-vertical-edge'), 200);
}
}
const getWebPics = length => {
let picArr = [];
let offlinePicsArr = [
'anchor',
'apple',
'barn',
'baseball',
'basketball-player',
'boxer',
'car',
'cat',
'cowboy-boot',
'duck',
'eagle',
'golden-anchor',
'grapes',
'horse',
'house',
'jumping-horse',
'jumping',
'key',
'keys',
'lion',
'monster-truck',
'motorcycle',
'pocket-watch',
'scissors',
'seahorse',
'shoe',
'stopwatch',
'wagon-wheel',
'wheel',
'slamdunk'
];
for (let i = 0; i < length; i++) {
let url = 'http://picsum.photos/65/65/?random';
let picReq = new XMLHttpRequest();
let image = document.createElement('img');
picReq.open('GET', url);
picReq.responseType = 'blob';
picReq.onload = () => {
image.src = window.URL.createObjectURL(picReq.response);
picArr.push(image);
if (i == length - 1) {
let playBtn = document.getElementById('playBtn');
playBtn.style.opacity = '1.0';
playBtn.disabled = false;
}
};
picReq.onerror = () => {
//Add failback option to use offline images if picsum is not responding
image.src = `images/${offlinePicsArr[i]}.jpg`;
picArr.push(image);
if (i == length - 1) {
let playBtn = document.getElementById('playBtn');
playBtn.style.opacity = '1.0';
playBtn.disabled = false;
}
};
picReq.send();
}
return picArr;
};
const memGame = new MatchingGame('app');