-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
114 lines (96 loc) · 2.93 KB
/
Copy pathscript.js
File metadata and controls
114 lines (96 loc) · 2.93 KB
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
const board = document.querySelector('.board');
const clone = document.querySelector('.clone');
const overlay = document.querySelector('.overlay');
const reset = document.querySelector('.reset');
const tileOptions = ['erupt', 'ptero', 'tri', 'ahahah', 'egg', 'dino'];
const state = {
selections: [],
boardLocked: false,
matches: 0
};
reset.addEventListener('click', () => {
if (state.boardLocked) return;
resetGame();
});
function resetGame() {
state.boardLocked = true;
state.selections = [];
state.matches = 0;
document.querySelectorAll('.cube').forEach(tile => {
tile.removeEventListener('click', () => selectTile(tile));
tile.remove();
});
overlay.classList.add('hidden');
createBoard();
}
function createBoard() {
const tiles = shuffleArray([...tileOptions, ...tileOptions]);
const length = tiles.length;
for (let i = 0; i < length; i++) {
window.setTimeout(() => {
board.appendChild(buildTile(tiles.pop(), i));
}, i * 100);
}
window.setTimeout(() => {
document.querySelectorAll('.cube').forEach(tile => {
tile.addEventListener('click', () => selectTile(tile));
});
state.boardLocked = false;
}, tiles.length * 100);
}
function buildTile(option, id) {
const tile = clone.cloneNode(true);
tile.classList.remove('clone');
tile.classList.add('cube');
tile.setAttribute('data-tile', option);
tile.setAttribute('data-id', id);
return tile;
}
function selectTile(selectedTile) {
if (state.boardLocked || selectedTile.classList.contains('flipped')) return;
state.boardLocked = true;
if (state.selections.length <= 1) {
selectedTile.classList.add('flipped');
state.selections.push({
id: selectedTile.dataset.id,
tile: selectedTile.dataset.tile,
el: selectedTile
});
}
if (state.selections.length === 2) {
if (state.selections[0].tile === state.selections[1].tile) {
window.setTimeout(() => {
state.selections[0].el.classList.add('matched');
state.selections[1].el.classList.add('matched');
state.boardLocked = false;
state.matches = state.matches + 1;
if (state.matches === tileOptions.length) {
window.setTimeout(() => {
overlay.classList.remove('hidden');
document.querySelector('.audio-win').play();
}, 600);
}
state.selections = [];
document.querySelector(`.audio-${selectedTile.dataset.tile}`).play();
}, 600);
} else {
setTimeout(() => {
document.querySelectorAll('.cube').forEach(tile => {
tile.classList.remove('flipped');
});
state.boardLocked = false;
}, 800);
state.selections = [];
}
} else {
state.boardLocked = false;
}
}
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
createBoard();