Give preciser hints

Fixes #84.
This commit is contained in:
Florine W. Dekker 2020-08-10 20:59:18 +02:00
parent 6adc3de1c4
commit 0ba2f34146
Signed by: FWDekker
GPG Key ID: B1B567AF58D6EE0F
2 changed files with 14 additions and 9 deletions

View File

@ -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",

View File

@ -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;
}