This repository was archived by the owner on Nov 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 269
/
Copy pathgrid.js
321 lines (287 loc) · 11.1 KB
/
grid.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
(function () {
'use strict';
const pieces = [...document.querySelectorAll('.puzzle-game > button')];
const clickScore = document.getElementById('clicks');
const soundButton = document.getElementById('toggle-audio');
const congratulations = document.getElementsByClassName('congratulations')[0];
let shuffledPieces = pieces;
const hiddenPiece = pieces[6];
let clickCount = 0;
let isDone = true;
let audio = false;
const success = new Audio('./sounds/success.wav');
const fail = new Audio('./sounds/failure.wav');
/// =============================================================================================
/// see https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array
/// =============================================================================================
const shuffle = function(array) {
let currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (currentIndex !== 0) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
};
/// =============================================================================================
/// update the grid area of every piece of the puzzle
/// =============================================================================================
const updatePositions = function() {
shuffledPieces.forEach((piece) => {
piece.parentNode.appendChild(piece);
});
};
/// =============================================================================================
/// highlight the pieces that are moveable
/// =============================================================================================
const highlightMoveablePieces = function() {
const indexOfHiddenPiece = shuffledPieces.indexOf(hiddenPiece);
const rowOfHiddenPiece = Math.floor(indexOfHiddenPiece / 3);
const colOfHiddenPiece = indexOfHiddenPiece % 3;
for (const piece of pieces) {
const indexOfCurrentPiece = shuffledPieces.indexOf(piece);
const rowOfCurrentPiece = Math.floor(indexOfCurrentPiece / 3);
const colOfCurrentPiece = indexOfCurrentPiece % 3;
if ((colOfCurrentPiece === colOfHiddenPiece && Math.abs(rowOfHiddenPiece - rowOfCurrentPiece) === 1) ||
(rowOfCurrentPiece === rowOfHiddenPiece && Math.abs(colOfHiddenPiece - colOfCurrentPiece) === 1)
) {
piece.classList.add('piece-is-moveable');
} else {
piece.classList.remove('piece-is-moveable');
}
}
};
/// =============================================================================================
/// Reset the game to start over
/// =============================================================================================
const reset = function() {
isDone = false;
hiddenPiece.style.opacity = '0';
clickCount = 0;
shuffledPieces = shuffle([...pieces]);
congratulations.style.display = 'none';
updatePositions();
highlightMoveablePieces();
};
/// =============================================================================================
/// checks whether all pieces are in the correct order
/// =============================================================================================
const checkIfHasWon = function() {
const hasWon = shuffledPieces.every((piece, index) => {
return (pieces[index] === piece);
});
if (hasWon) {
if (clickCount === 0) {
reset();
} else {
isDone = true;
hiddenPiece.style.opacity = '1';
congratulations.style.display = 'block';
congratulations.focus();
congratulations.textContent = 'Congrats, you solved the puzzle!';
}
}
};
/// =============================================================================================
/// Update the text of the audio button after clicking
/// =============================================================================================
const updateToggleButtonText = function(audioText) {
const span = soundButton.querySelector('span');
span.innerText = audioText;
};
/// =============================================================================================
/// Turn on and off audio
/// =============================================================================================
const toggleAudio = function() {
let audioText = '';
switch (audio) {
case true: {
audio = false;
audioText = 'on';
break;
}
default: {
audio = true;
audioText = 'off';
break;
}
}
updateToggleButtonText(audioText);
};
/// =============================================================================================
/// A helper function to say things
/// =============================================================================================
const say = function(statement) {
if (window.speechSynthesis.speak) {
window.speechSynthesis.speak(new SpeechSynthesisUtterance(statement));
}
};
/// =============================================================================================
/// Determine where the hidden piece is in the 3x3 grid
/// =============================================================================================
const whereIsHiddenPiece = function(requested) {
const indexOfHiddenPiece = shuffledPieces.indexOf(hiddenPiece);
const colOfHiddenPiece = (indexOfHiddenPiece % 3) + 1;
const rowOfHiddenPiece = (Math.floor(indexOfHiddenPiece / 3)) + 1;
if (requested) {
say(`The hidden piece is in row ${rowOfHiddenPiece} and in column ${colOfHiddenPiece}`);
}
};
/// =============================================================================================
/// Audibly speak the current state of the game
/// =============================================================================================
const currentState = function(requested) {
let row1 = 'Row1: ';
let row2 = 'Row2: ';
let row3 = 'Row3: ';
shuffledPieces.forEach((piece, index) => {
switch (true) {
case (index <= 2): {
row1 += `${piece.getAttribute('aria-label')} `;
break;
}
case (index > 2 && index <= 5): {
row2 += `${piece.getAttribute('aria-label')} `;
break;
}
default: {
row3 += `${piece.getAttribute('aria-label')} `;
break;
}
}
});
if (requested) {
say(row1);
say(row2);
say(row3);
}
};
/// =============================================================================================
/// Audibly state where the pieces that can move are
/// =============================================================================================
const whereAreTheMoveablePieces = function() {
let speech = 'Tiles that can move: ';
const tiles = document.querySelectorAll('.piece-is-moveable');
tiles.forEach((tile) => {
speech += `Tile ${tile.getAttribute('aria-label')} `;
});
say(speech);
};
/// =============================================================================================
/// attempts to swap the clicked piece with the hidden piece
/// =============================================================================================
const trySwapWithHiddenPiece = function() {
const clickedPiece = this || null; // eslint-disable-line no-invalid-this
clickedPiece.focus();
const indexOfHiddenPiece = shuffledPieces.indexOf(hiddenPiece);
const indexOfClickedPiece = shuffledPieces.indexOf(clickedPiece);
const rowOfHiddenPiece = Math.floor(indexOfHiddenPiece / 3);
const colOfHiddenPiece = indexOfHiddenPiece % 3;
const rowOfClickedPiece = Math.floor(indexOfClickedPiece / 3);
const colOfClickedPiece = indexOfClickedPiece % 3;
const isMoveValid = (
false ||
(colOfClickedPiece === colOfHiddenPiece && Math.abs(rowOfHiddenPiece - rowOfClickedPiece) === 1) ||
(rowOfClickedPiece === rowOfHiddenPiece && Math.abs(colOfHiddenPiece - colOfClickedPiece) === 1)
);
if (isMoveValid && !isDone) {
clickCount++;
clickScore.textContent = clickCount;
shuffledPieces[indexOfHiddenPiece] = clickedPiece;
shuffledPieces[indexOfClickedPiece] = hiddenPiece;
if (audio) {
success.play();
}
updatePositions();
highlightMoveablePieces();
checkIfHasWon();
} else if (audio) {
fail.play();
}
};
/// =============================================================================================
/// Handles any key presses for game purposes
/// =============================================================================================
const handleKeyDown = function(event) {
const indexOfHiddenPiece = shuffledPieces.indexOf(hiddenPiece);
const rowOfHiddenPiece = Math.floor(indexOfHiddenPiece / 3);
const colOfHiddenPiece = indexOfHiddenPiece % 3;
let indexOfClickedPiece;
switch (event.key) {
case 's': {
const rowOfClickedPiece = rowOfHiddenPiece - 1;
const colOfClickedPiece = colOfHiddenPiece;
indexOfClickedPiece = (rowOfClickedPiece * 3) + colOfClickedPiece;
break;
}
case 'w': {
const rowOfClickedPiece = rowOfHiddenPiece + 1;
const colOfClickedPiece = colOfHiddenPiece;
indexOfClickedPiece = (rowOfClickedPiece * 3) + colOfClickedPiece;
break;
}
case 'd': {
const rowOfClickedPiece = rowOfHiddenPiece;
const colOfClickedPiece = colOfHiddenPiece - 1;
indexOfClickedPiece = (rowOfClickedPiece * 3) + colOfClickedPiece;
break;
}
case 'a': {
const rowOfClickedPiece = rowOfHiddenPiece;
const colOfClickedPiece = colOfHiddenPiece + 1;
indexOfClickedPiece = (rowOfClickedPiece * 3) + colOfClickedPiece;
break;
}
case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': {
const clickedPiece = document.getElementById(`piece-${parseInt(event.key)}`);
indexOfClickedPiece = shuffledPieces.indexOf(clickedPiece);
break;
}
case 'c': {
currentState(true);
break;
}
case 'h': {
whereIsHiddenPiece(true);
break;
}
case 'm': {
whereAreTheMoveablePieces(true);
break;
}
default: {
break;
}
}
const clickedPiece = shuffledPieces[indexOfClickedPiece];
if (clickedPiece) {
trySwapWithHiddenPiece.call(clickedPiece, event);
}
};
/// =============================================================================================
/// Make it so that audio can fire quickly
/// =============================================================================================
const playAudio = function(audioVar) {
if (window.currentlyPlaying) {
window.currentlyPlaying.pause();
}
window.currentlyPlaying = audioVar.target;
};
// =============================================================================================
/// Init
/// =============================================================================================
addEventListener('keydown', handleKeyDown);
addEventListener('play', playAudio, true);
soundButton.addEventListener('click', toggleAudio);
for (const piece of pieces) {
piece.addEventListener('click', trySwapWithHiddenPiece);
piece.addEventListener('keypress', trySwapWithHiddenPiece);
}
reset();
updatePositions();
}());