diff --git a/index.html b/index.html index e9400aa..ae9f447 100644 --- a/index.html +++ b/index.html @@ -24,7 +24,7 @@ @@ -38,7 +38,7 @@
Numbers - +
@@ -46,8 +46,8 @@ - -- plates tried   - (--/sec) + 0 boards tried   + (0.00/sec) diff --git a/main.js b/main.js index 1dd7e11..598957c 100644 --- a/main.js +++ b/main.js @@ -31,11 +31,17 @@ function resetWorkers() { } function runWorkers() { + const numbers = [].map.call(document.getElementsByClassName("number-input"), input => input.valueAsNumber) + .filter(value => !isNaN(value)); + const winType = document.querySelector("input[name=win-type]:checked").value; + resetWorkers(); const amount = document.getElementById("workers").valueAsNumber; log(`Creating ${amount} workers`); + log(`* Win type: ${winType}`); + log(`* Numbers: ${numbers.join(", ")}`); for (let i = 0; i < amount; i++) { const worker = new Worker("worker.js"); @@ -60,7 +66,7 @@ function runWorkers() { } }); - worker.postMessage({ type: "init", id: i + 1 }); + worker.postMessage({ type: "init", id: i + 1, numbers, winType }); workers.push(worker); } } @@ -70,6 +76,7 @@ document.getElementById("numbers-container").addEventListener("keydown", event = if (event.key === "Enter") { const input = document.createElement("input"); input.type = "number"; + input.className = "number-input"; input.min = 1; input.max = 90; diff --git a/worker.js b/worker.js index 1f4f0c7..d1eee94 100644 --- a/worker.js +++ b/worker.js @@ -1,9 +1,10 @@ importScripts("seedrandom.js"); importScripts("generator.js"); -console.log("worker"); - let workerId; +let winType; +let numbers; + let currentInput = ""; let attempts = 0; let startedAt; @@ -11,7 +12,7 @@ let startedAt; let statsInterval; let guessInterval; -function getNumbers(name) { +function getBoard(name) { Math.seedrandom(name); let numbers = []; @@ -21,27 +22,15 @@ function getNumbers(name) { } const activatedCols = generate_rows_check(); - const activatedNumbers = []; + const board = [[], [], []]; for (let row = 0; row < 3; row++) { for (const col of activatedCols[row]) { - activatedNumbers.push(numbers[(col - 1) * 3 + row]); + board[row].push(numbers[(col - 1) * 3 + row]); } } - return activatedNumbers; -} - -function sendStats() { - if (!startedAt) return; - - self.postMessage({ type: "stats", attempts, time: Date.now() - startedAt }); - resetStats(); -} - -function resetStats() { - attempts = 0; - startedAt = Date.now(); + return board; } function generateNextInput(input) { @@ -61,11 +50,49 @@ function generateNextInput(input) { return alphabet[0] + input; } +function hasWonRow(row, numbers) { + return numbers.every(number => row.includes(number)); +} + +function getRowsWon(board, numbers) { + return board.filter(row => hasWonRow(row, numbers)).length; +} + +function hasWon(wonRows, winType) { + switch (winType) { + case "row": + return wonRows >= 1; + case "two-rows": + return wonRows >= 2; + case "full-board": + return wonRows >= 3; + } +} + function guess() { currentInput = generateNextInput(currentInput); attempts++; - // TODO + const board = getBoard(currentInput); + + const wonRows = getRowsWon(board, numbers); + + if (hasWon(wonRows, winType)) { + self.postMessage({ type: "winner", name: currentInput }); + self.postMessage({ type: "log", text: `Found winner: ${currentInput}`, id: workerId }); + } +} + +function sendStats() { + if (!startedAt) return; + + self.postMessage({ type: "stats", attempts, time: Date.now() - startedAt }); + resetStats(); +} + +function resetStats() { + attempts = 0; + startedAt = Date.now(); } function run() { @@ -90,6 +117,8 @@ self.onmessage = message => { switch (message.data.type) { case "init": workerId = message.data.id; + winType = message.data.winType; + numbers = message.data.numbers; self.postMessage({ type: "ready" }); break; case "run":