|
| 1 | + |
| 2 | +const knightBoard = document.querySelector("#knight-board"); |
| 3 | +const solvedBoards = document.querySelector("#knight-solved-boards"); |
| 4 | +const submitNum = document.querySelector("#submit-knight-range"); |
| 5 | +const noOfKnights = document.querySelector("#no-of-knights"); |
| 6 | +const stopExecution = document.querySelector("#stop-knight-execution"); |
| 7 | +const result = document.querySelector("#knight-result h3"); |
| 8 | +const solveSpeed = document.querySelector("#knight-speed"); |
| 9 | + |
| 10 | + |
| 11 | +let stopEx = false; |
| 12 | +let combinations = 0; |
| 13 | +let count = 0; |
| 14 | + |
| 15 | +let board; |
| 16 | + |
| 17 | +let isSolving = false; |
| 18 | + |
| 19 | +submitNum.addEventListener("click", async function() { |
| 20 | + result.textContent = "Total: "; |
| 21 | + combinations = 0; |
| 22 | + isSolving = true; |
| 23 | + solvedBoards.textContent = ""; |
| 24 | + submitNum.disabled = true; |
| 25 | + |
| 26 | + await nKnights(); |
| 27 | + |
| 28 | + submitNum.disabled = false; |
| 29 | + stopEx = false; |
| 30 | + isSolving = false; |
| 31 | +}); |
| 32 | + |
| 33 | +stopExecution.addEventListener("click", function() { |
| 34 | + |
| 35 | + stopEx = true; |
| 36 | + |
| 37 | +}); |
| 38 | + |
| 39 | + |
| 40 | +if (!isSolving) { |
| 41 | + createDefaultBoard(); |
| 42 | + createBoard(); |
| 43 | +} |
| 44 | + |
| 45 | +let value1 = 200; |
| 46 | +let value2 = 500; |
| 47 | + |
| 48 | +solveSpeed.addEventListener("change", function(event) { |
| 49 | + |
| 50 | + switch(event.target.value) { |
| 51 | + case "Slow": |
| 52 | + value1 = 500; |
| 53 | + value2 = 1000; |
| 54 | + break; |
| 55 | + case "Medium": |
| 56 | + value1 = 100; |
| 57 | + value2 = 200; |
| 58 | + break; |
| 59 | + case "Fast": |
| 60 | + value1 = 0; |
| 61 | + value2 = 0; |
| 62 | + break; |
| 63 | + } |
| 64 | + |
| 65 | +}) |
| 66 | + |
| 67 | + |
| 68 | +export default async function nKnights() { |
| 69 | + |
| 70 | + if (board.length > 0) { |
| 71 | + |
| 72 | + if (count == 0) { |
| 73 | + |
| 74 | + count = 1; |
| 75 | + |
| 76 | + await solveForNKnights(board, 0, 0, board.length); |
| 77 | + |
| 78 | + count = 0; |
| 79 | + } |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | +} |
| 84 | + |
| 85 | +function createDefaultBoard() { |
| 86 | + const newBoard = []; |
| 87 | + |
| 88 | + for (let i = 0; i < 4; i++) { |
| 89 | + |
| 90 | + newBoard[i] = []; |
| 91 | + |
| 92 | + for (let j = 0; j < 4; j++) { |
| 93 | + |
| 94 | + const div = document.createElement("div"); |
| 95 | + newBoard[i][j] = div; |
| 96 | + knightBoard.append(div); |
| 97 | + |
| 98 | + } |
| 99 | + } |
| 100 | + knightBoard.style = `display: grid; grid-template: repeat(4, 1fr) / repeat(4, 1fr)`; |
| 101 | + board = newBoard; |
| 102 | +} |
| 103 | + |
| 104 | +async function createKnightBoard(value) { |
| 105 | + |
| 106 | + return new Promise((resolve) => { |
| 107 | + |
| 108 | + const newBoard = []; |
| 109 | + knightBoard.textContent = ""; |
| 110 | + |
| 111 | + for (let i = 0; i < value; i++) { |
| 112 | + |
| 113 | + newBoard[i] = []; |
| 114 | + |
| 115 | + for (let j = 0; j < value; j++) { |
| 116 | + |
| 117 | + const div = document.createElement("div"); |
| 118 | + newBoard[i][j] = div; |
| 119 | + knightBoard.append(div); |
| 120 | + |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + knightBoard.style = `display: grid; grid-template: repeat(${value}, 1fr) / repeat(${value}, 1fr)`; |
| 125 | + |
| 126 | + board = newBoard; // Update the global board variable |
| 127 | + |
| 128 | + resolve(board); |
| 129 | + |
| 130 | + }); |
| 131 | + |
| 132 | +} |
| 133 | + |
| 134 | +function createBoard() { |
| 135 | + |
| 136 | + const pickerValue = document.querySelector("#knight-number-value"); |
| 137 | + |
| 138 | + noOfKnights.addEventListener("change", async function (event) { |
| 139 | + |
| 140 | + |
| 141 | + if (!isSolving) { |
| 142 | + |
| 143 | + const value = event.target.value; |
| 144 | + pickerValue.textContent = "Value: " + value; |
| 145 | + if (board) { |
| 146 | + // Remove the previous board before creating a new one |
| 147 | + knightBoard.textContent = ""; |
| 148 | + } |
| 149 | + |
| 150 | + board = await createKnightBoard(value); |
| 151 | + |
| 152 | + } |
| 153 | + |
| 154 | + }); |
| 155 | + |
| 156 | +} |
| 157 | + |
| 158 | +function sleep(ms) { |
| 159 | + return new Promise(resolve => setTimeout(resolve, ms)); |
| 160 | +} |
| 161 | + |
| 162 | +async function solveForNKnights(board, row, col, target) { |
| 163 | + |
| 164 | + if (!stopEx) { |
| 165 | + if (target == 0) { |
| 166 | + appendBoard(board); |
| 167 | + combinations += 1; |
| 168 | + |
| 169 | + result.textContent = "Total: " + combinations; |
| 170 | + |
| 171 | + return; |
| 172 | + } |
| 173 | + |
| 174 | + if (row == board.length-1 && col == board.length) { |
| 175 | + return; |
| 176 | + } |
| 177 | + |
| 178 | + if (row == board.length) { |
| 179 | + return; |
| 180 | + } |
| 181 | + |
| 182 | + if (col == board[row].length) { |
| 183 | + await solveForNKnights(board, row + 1, 0, target); |
| 184 | + return; |
| 185 | + } |
| 186 | + |
| 187 | + await sleep(value1); |
| 188 | + |
| 189 | + board[row][col].textContent = "\u265E"; |
| 190 | + board[row][col].style.color = "red"; |
| 191 | + |
| 192 | + await sleep(value2); |
| 193 | + board[row][col].textContent = ""; |
| 194 | + |
| 195 | + if (isSafe(board, row, col)) { |
| 196 | + |
| 197 | + board[row][col].style.color = "green"; |
| 198 | + board[row][col].textContent = "\u265E"; |
| 199 | + |
| 200 | + await sleep(value1); |
| 201 | + |
| 202 | + await solveForNKnights(board, row, col + 1, target - 1); |
| 203 | + |
| 204 | + await sleep(value2); |
| 205 | + |
| 206 | + board[row][col].textContent = ""; |
| 207 | + } |
| 208 | + |
| 209 | + |
| 210 | + |
| 211 | + await solveForNKnights(board, row, col + 1, target); |
| 212 | + |
| 213 | + |
| 214 | + } |
| 215 | + |
| 216 | + |
| 217 | + |
| 218 | +} |
| 219 | + |
| 220 | +function appendBoard(board) { |
| 221 | + |
| 222 | + const solvedBoards = document.querySelector("#knight-solved-boards"); |
| 223 | + const boardDiv = document.createElement("div"); |
| 224 | + boardDiv.id = "solved-board-div"; |
| 225 | + |
| 226 | + for (let i = 0; i < board.length; i++) { |
| 227 | + |
| 228 | + |
| 229 | + for (let j = 0; j < board[i].length; j++) { |
| 230 | + |
| 231 | + const div = document.createElement("div"); |
| 232 | + |
| 233 | + |
| 234 | + if (board[i][j].textContent == "\u265E") { |
| 235 | + div.textContent = "\u265E"; |
| 236 | + } else { |
| 237 | + div.textContent = ""; |
| 238 | + } |
| 239 | + boardDiv.append(div); |
| 240 | + } |
| 241 | + } |
| 242 | + boardDiv.style = `display: grid; grid-template: repeat(${board.length}, 1fr) / repeat(${board[0].length}, 1fr); margin-top: 1em`; |
| 243 | + |
| 244 | + solvedBoards.append(boardDiv); |
| 245 | + |
| 246 | + |
| 247 | +} |
| 248 | + |
| 249 | +function isValid(board, row, col) { |
| 250 | + |
| 251 | + if (row >= 0 && row <= board.length-1 && col >= 0 && col <= board[row].length-1) { |
| 252 | + return true; |
| 253 | + } |
| 254 | + return false; |
| 255 | + |
| 256 | +} |
| 257 | + |
| 258 | +function isSafe(board, row, col) { |
| 259 | + |
| 260 | + if (isValid(board, row - 2, col - 1)) { |
| 261 | + if (board[row - 2][col - 1].textContent == "\u265E") { |
| 262 | + return false; |
| 263 | + } |
| 264 | + } |
| 265 | + if (isValid(board, row - 1, col - 2)) { |
| 266 | + if (board[row - 1][col - 2].textContent == "\u265E") { |
| 267 | + return false; |
| 268 | + } |
| 269 | + } |
| 270 | + if (isValid(board, row - 2, col + 1)) { |
| 271 | + if (board[row - 2][col + 1].textContent == "\u265E") { |
| 272 | + return false; |
| 273 | + } |
| 274 | + } |
| 275 | + if (isValid(board, row - 1, col + 2)) { |
| 276 | + if (board[row - 1][col + 2].textContent == "\u265E") { |
| 277 | + return false; |
| 278 | + } |
| 279 | + } |
| 280 | + if (isValid(board, row + 2, col - 1)) { |
| 281 | + if (board[row + 2][col - 1].textContent == "\u265E") { |
| 282 | + return false; |
| 283 | + } |
| 284 | + } |
| 285 | + if (isValid(board, row + 1, col - 2)) { |
| 286 | + if (board[row + 1][col - 2].textContent == "\u265E") { |
| 287 | + return false; |
| 288 | + } |
| 289 | + } |
| 290 | + if (isValid(board, row + 2, col + 1)) { |
| 291 | + if (board[row + 2][col + 1].textContent == "\u265E") { |
| 292 | + return false; |
| 293 | + } |
| 294 | + } |
| 295 | + if (isValid(board, row + 1, col + 2)) { |
| 296 | + if (board[row + 1][col + 2].textContent == "\u265E") { |
| 297 | + return false; |
| 298 | + } |
| 299 | + } |
| 300 | + |
| 301 | + |
| 302 | + |
| 303 | + return true; |
| 304 | + |
| 305 | + |
| 306 | +} |
0 commit comments