From 0ba2f341469b1c221317009c9583354412db45e8 Mon Sep 17 00:00:00 2001 From: "Felix W. Dekker" Date: Mon, 10 Aug 2020 20:59:18 +0200 Subject: [PATCH] Give preciser hints Fixes #84. --- package.json | 2 +- src/main/js/Solver.ts | 21 +++++++++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 4b687e9..8685b2e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "minesweeper", - "version": "0.80.1", + "version": "0.80.2", "description": "Just Minesweeper!", "author": "Felix W. Dekker", "browser": "dist/bundle.js", diff --git a/src/main/js/Solver.ts b/src/main/js/Solver.ts index 78e4e54..d24a4f7 100644 --- a/src/main/js/Solver.ts +++ b/src/main/js/Solver.ts @@ -95,11 +95,9 @@ export class Solver { */ getHint(field: Field): Square | null { if (!field.hasStarted || field.hasWon || field.hasLost) return null; - - let candidate: Square | undefined; const knowns = this.getKnowns(field); - candidate = knowns.find(square => + const candidate = knowns.find(square => // Can chord square.getNeighborCount(it => it.hasFlag) === square.getNeighborCount(it => it.hasMine) || // Can flag @@ -108,11 +106,18 @@ export class Solver { ); if (candidate !== undefined) return candidate; - candidate = knowns.find(square => - this.matrixSolve(field, square.neighbors.filter(it => !it.isCovered).concat(square), true) - .some(it => it !== undefined) - ); - if (candidate !== undefined) return candidate; + for (let i = 0; i < knowns.length; i++) { + const square = knowns[i]; + const solution = this.matrixSolve(field, square.neighbors.filter(it => !it.isCovered).concat(square), true); + const candidate = solution.find(it => it !== undefined); + if (candidate !== undefined) + return candidate[1]; + } + + const solution = this.matrixSolve(field, knowns, false); + const candidate2 = solution.find(it => it !== undefined); + if (candidate2 !== undefined) + return candidate2[1]; return null; }