|
| 1 | +(function() { |
| 2 | + let state = 1; |
| 3 | + let game = document.querySelector("#game"); |
| 4 | + // let noOfMoves = document.querySelector("#counter").innerText; |
| 5 | + // noOfMoves = isNaN(noOfMoves) ? 0 : noOfMoves; |
| 6 | + |
| 7 | + // Creates game then shuffle it.. |
| 8 | + createAndSolve(), shuffle(); |
| 9 | + |
| 10 | + // Listens for click on game cells |
| 11 | + game.addEventListener("click", function(e) { |
| 12 | + if (state == 1) { |
| 13 | + // Enables sliding animation |
| 14 | + game.className = "animate"; |
| 15 | + moveNumberCellToEmpty(e.target, 1); |
| 16 | + } |
| 17 | + }); |
| 18 | + |
| 19 | + // Listens for click on control buttons |
| 20 | + document.getElementById("solve").addEventListener("click", createAndSolve); |
| 21 | + document.getElementById("shuffle").addEventListener("click", shuffle); |
| 22 | + |
| 23 | + // Create Solved Puzzle.. |
| 24 | + function createAndSolve() { |
| 25 | + if (state == 0) { |
| 26 | + return; |
| 27 | + } |
| 28 | + game.innerHTML = ""; // Clear the game area |
| 29 | + let n = 1; |
| 30 | + for (let i = 0; i < 4; i++) { |
| 31 | + for (let j = 0; j < 4; j++) { |
| 32 | + let cell = document.createElement("span"); // create one cell |
| 33 | + cell.id = `cell-${i}-${j}`; // 'cell-' + i + '-' + j; give id to the cell |
| 34 | + cell.style.left = j * 80 + 1 * j + 1 + "px"; // position of the cell from left |
| 35 | + cell.style.top = i * 80 + 1 * i + 1 + "px"; // position of the cell from the top |
| 36 | + if (n <= 15) { |
| 37 | + // numberCell |
| 38 | + cell.classList.add("number"); |
| 39 | + cell.classList.add( |
| 40 | + (i % 2 === 0 && j % 2 > 0) || (i % 2 > 0 && j % 2 === 0) |
| 41 | + ? "dark" |
| 42 | + : "light" |
| 43 | + ); // alternate between the cells (dark-light) |
| 44 | + cell.innerHTML = (n++).toString(); |
| 45 | + } else { |
| 46 | + // emptyCell |
| 47 | + cell.className = "empty"; |
| 48 | + } |
| 49 | + game.appendChild(cell); |
| 50 | + } |
| 51 | + } |
| 52 | + // resetNoOfMoves(); |
| 53 | + } |
| 54 | + |
| 55 | + // shuffle the game |
| 56 | + function shuffle() { |
| 57 | + if (state == 0) { |
| 58 | + return; |
| 59 | + } |
| 60 | + game.removeAttribute("class"); // remove animation.. |
| 61 | + state = 0; |
| 62 | + let previousCell; |
| 63 | + let i = 1; |
| 64 | + let interval = setInterval(function() { |
| 65 | + if (i <= 150) { |
| 66 | + let adjacent = getAdjacentCells(getEmptyCell()); |
| 67 | + if (previousCell) { |
| 68 | + for (let j = adjacent.length - 1; j >= 0; j--) { |
| 69 | + if (adjacent[j].innerHTML == previousCell.innerHTML) { |
| 70 | + adjacent.splice(j, 1); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + // Gets random adjacent cell and memorizes it for the next iteration |
| 75 | + previousCell = adjacent[getRandomNumberBetween(0, adjacent.length - 1)]; |
| 76 | + moveNumberCellToEmpty(previousCell, 0); |
| 77 | + i++; |
| 78 | + } else { |
| 79 | + clearInterval(interval); |
| 80 | + state = 1; |
| 81 | + } |
| 82 | + }, 5); |
| 83 | + // resetNoOfMoves(); |
| 84 | + } |
| 85 | + |
| 86 | + function resetNoOfMoves() { |
| 87 | + noOfMoves = 0; |
| 88 | + document.getElementById("counter").innerText = noOfMoves; |
| 89 | + } |
| 90 | + |
| 91 | + // Shifts number cell to the empty cell |
| 92 | + function moveNumberCellToEmpty(cell, playingOrShuffling) { |
| 93 | + // Checks if selected cell has number |
| 94 | + if (cell.className != "empty") { |
| 95 | + // Tries to get empty adjacent cell |
| 96 | + let emptyCell = getEmptyAdjacentCellIfExists(cell); |
| 97 | + |
| 98 | + if (emptyCell) { |
| 99 | + if (playingOrShuffling === 1) { |
| 100 | + // noOfMoves++; |
| 101 | + // document.getElementById("counter").innerText = noOfMoves; |
| 102 | + // new Audio("../sound/fire_bow_sound-mike-koenig.mp3").play(); |
| 103 | + } |
| 104 | + // There is empty adjacent cell.. |
| 105 | + // styling and id of the number cell |
| 106 | + let tempCell = { style: cell.style.cssText, id: cell.id }; |
| 107 | + |
| 108 | + // Exchanges id and style values |
| 109 | + cell.style.cssText = emptyCell.style.cssText; |
| 110 | + cell.id = emptyCell.id; |
| 111 | + emptyCell.style.cssText = tempCell.style; |
| 112 | + emptyCell.id = tempCell.id; |
| 113 | + |
| 114 | + if (state == 1) { |
| 115 | + // Checks the order of numbers |
| 116 | + checkSolvedState(); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + // Gets specific cell by row and column. |
| 123 | + function getCell(row, col) { |
| 124 | + return document.getElementById(`cell-${row}-${col}`); |
| 125 | + } |
| 126 | + |
| 127 | + // get the empty cell. |
| 128 | + function getEmptyCell() { |
| 129 | + return game.querySelector(".empty"); |
| 130 | + } |
| 131 | + |
| 132 | + // Gets empty adjacent cell if it exists. |
| 133 | + function getEmptyAdjacentCellIfExists(cell) { |
| 134 | + // Gets all adjacent cells |
| 135 | + let adjacent = getAdjacentCells(cell); |
| 136 | + |
| 137 | + // Searches for empty cell |
| 138 | + for (let i = 0; i < adjacent.length; i++) { |
| 139 | + if (adjacent[i].className == "empty") { |
| 140 | + return adjacent[i]; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + // Empty adjacent cell was not found.. |
| 145 | + return false; |
| 146 | + } |
| 147 | + |
| 148 | + // Gets all adjacent cells |
| 149 | + function getAdjacentCells(cell) { |
| 150 | + let id = cell.id.split("-"); |
| 151 | + |
| 152 | + // Gets cell position indexes |
| 153 | + console.log(`id[0] = ${id[0]}`); |
| 154 | + let row = parseInt(id[1]); |
| 155 | + let col = parseInt(id[2]); |
| 156 | + |
| 157 | + let adjacent = []; |
| 158 | + |
| 159 | + // Gets all possible adjacent cells |
| 160 | + if (row < 3) { |
| 161 | + adjacent.push(getCell(row + 1, col)); // right |
| 162 | + } |
| 163 | + if (row > 0) { |
| 164 | + adjacent.push(getCell(row - 1, col)); // left |
| 165 | + } |
| 166 | + if (col < 3) { |
| 167 | + adjacent.push(getCell(row, col + 1)); // top |
| 168 | + } |
| 169 | + if (col > 0) { |
| 170 | + adjacent.push(getCell(row, col - 1)); // bottom |
| 171 | + } |
| 172 | + return adjacent; |
| 173 | + } |
| 174 | + |
| 175 | + // Checks if the order of numbers is correct and we get solved-state.. |
| 176 | + function checkSolvedState() { |
| 177 | + // Checks if the empty cell is in correct position |
| 178 | + if (getCell(3, 3).className != "empty") { |
| 179 | + return; |
| 180 | + } |
| 181 | + |
| 182 | + let n = 1; |
| 183 | + // Goes through all cells and checks numbers |
| 184 | + for (let i = 0; i < 4; i++) { |
| 185 | + for (let j = 0; j < 4; j++) { |
| 186 | + if (n <= 15 && getCell(i, j).innerHTML != n.toString()) { |
| 187 | + // Order is not correct |
| 188 | + return; |
| 189 | + } |
| 190 | + n++; |
| 191 | + } |
| 192 | + } |
| 193 | + // Puzzle is solved, offers to shuffle it |
| 194 | + startCongratsOverLay(), shuffle(); |
| 195 | + } |
| 196 | + |
| 197 | + // Generates random number |
| 198 | + function getRandomNumberBetween(from, to) { |
| 199 | + return Math.floor(Math.random() * (to - from + 1)) + from; |
| 200 | + } |
| 201 | +})(); |
| 202 | + |
| 203 | +// overLay effect.. |
| 204 | +function startReadyOverLay() { |
| 205 | + document.getElementById("overlay-1").style.display = "block"; |
| 206 | +} |
| 207 | +function endReadyOverLay() { |
| 208 | + document.getElementById("overlay-1").style.display = "none"; |
| 209 | +} |
| 210 | +function startCongratsOverLay() { |
| 211 | + document.getElementById("overlay-2").style.display = "block"; |
| 212 | +} |
| 213 | +function endCongratsOverLay() { |
| 214 | + document.getElementById("overlay-2").style.display = "none"; |
| 215 | +} |
| 216 | +// Play sound library.. |
| 217 | +function Sound(source, volume, loop) { |
| 218 | + this.source = source; |
| 219 | + this.volume = volume; |
| 220 | + this.loop = loop; |
| 221 | + var son; |
| 222 | + this.son = son; |
| 223 | + this.finish = false; |
| 224 | + this.stop = function() { |
| 225 | + document.body.removeChild(this.son); |
| 226 | + }; |
| 227 | + this.start = function() { |
| 228 | + if (this.finish) return false; |
| 229 | + this.son = document.createElement("embed"); |
| 230 | + this.son.setAttribute("src", this.source); |
| 231 | + this.son.setAttribute("hidden", "true"); |
| 232 | + this.son.setAttribute("volume", this.volume); |
| 233 | + this.son.setAttribute("autostart", "true"); |
| 234 | + this.son.setAttribute("loop", this.loop); |
| 235 | + document.body.appendChild(this.son); |
| 236 | + }; |
| 237 | + this.remove = function() { |
| 238 | + document.body.removeChild(this.son); |
| 239 | + this.finish = true; |
| 240 | + }; |
| 241 | + this.init = function(volume, loop) { |
| 242 | + this.finish = false; |
| 243 | + this.volume = volume; |
| 244 | + this.loop = loop; |
| 245 | + }; |
| 246 | +} |
0 commit comments