great new heuristic

master
Ondřej Hruška 5 years ago
parent 288d321eff
commit f6a41cbe07
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 46
      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) {

Loading…
Cancel
Save