From f6a41cbe07de95ed1432f71e77d27874ad975808 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Wed, 11 Dec 2019 00:34:13 +0100 Subject: [PATCH] great new heuristic --- script.js | 46 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/script.js b/script.js index 6367a3b..8c799d7 100644 --- a/script.js +++ b/script.js @@ -1140,7 +1140,13 @@ class Game { } } - return {neighbours: nOccupied, freeSequence: maxFreeSequence}; + let { rx, ry } = this.board.gridXyToCoord(x, y); + + return { + neighbours: nOccupied, + freeSequence: maxFreeSequence, + centerWeight: Math.round(Math.sqrt(Math.pow(rx, 2) + Math.pow(ry, 2))), + }; } placeOrbs(template) { @@ -1183,31 +1189,49 @@ class Game { this.board.grid[n] = null; }; - const findAvailableIndexWithNeighbours = (count, except = null) => { + const findBestCandidate = (except = null) => { let candidates = []; for (let n of template) { if (except && except.includes(n)) continue; if (!this.board.grid[n]) { + // is free const neigh = this.getNeighbours(n); - if (neigh.neighbours === count && neigh.freeSequence >= 3) { - candidates.push(n); + if (neigh.freeSequence >= 3) { + candidates.push({ + n, + cw: neigh.centerWeight, + neighs: neigh.neighbours, + }); } } } + candidates.sort((a, b) => a.cw - b.cw); + if (candidates.length) { - return this.arrayChoose(candidates) - } else { - return false; + // return candidates[0].n; + let top = []; + let topw = candidates[0].cw; + for(let cand of candidates) { + if (cand.cw <= topw + 18) { + top.push(cand); + } + } + + //the neighbor count is not used for anything anymore, oops + + // console.log('Got a choice of '+top.length+' tiles'); + + return this.arrayChoose(top).n; } + + return false; }; const findAvailableIndex = (except = null) => { - for (let i = 6; i >= 0; i--) { - const n = findAvailableIndexWithNeighbours(i, except); - if (n !== false) return n; - } + const n = findBestCandidate(except); + if (n !== false) return n; // this corrupts the template, but makes the likelihood of quickly finding a valid solution much higher. if (template.length !== BOARD_SIZE && this.cfg.allowTemplateAugmenting) {