Make Solver class and methods static

This commit is contained in:
Florine W. Dekker 2021-11-14 15:54:52 +01:00
parent 7f0f93378e
commit 74b2cb9b27
Signed by: FWDekker
GPG Key ID: D3DCFAA8A4560BE0
4 changed files with 12 additions and 13 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "minesweeper", "name": "minesweeper",
"version": "0.82.12", "version": "0.82.13",
"description": "Just Minesweeper!", "description": "Just Minesweeper!",
"author": "F.W. Dekker", "author": "F.W. Dekker",
"browser": "dist/bundle.js", "browser": "dist/bundle.js",

View File

@ -249,8 +249,7 @@ export class Field {
if (this.isSolvable) { if (this.isSolvable) {
let i = 1; let i = 1;
const time = Timer.time(() => { const time = Timer.time(() => {
const solver = new Solver(); while (!Solver.canSolve(this, coords)) {
while (!solver.canSolve(this, coords)) {
this.shuffle(this.rng.uint32()); this.shuffle(this.rng.uint32());
i++; i++;
} }

View File

@ -223,7 +223,7 @@ export class Game {
if (this.field !== null) { if (this.field !== null) {
this.statistics.hintsRequested++; this.statistics.hintsRequested++;
this.display.hintSquare = (new Solver()).getHint(this.field); this.display.hintSquare = Solver.getHint(this.field);
} }
blurActiveElement(); blurActiveElement();
} }
@ -238,7 +238,7 @@ export class Game {
if (this.field !== null) { if (this.field !== null) {
this.statistics.solverUsages++; this.statistics.solverUsages++;
(new Solver()).solve(this.field); Solver.solve(this.field);
} }
blurActiveElement(); blurActiveElement();
} }

View File

@ -13,7 +13,7 @@ export class Solver {
* *
* @param field the field to solve * @param field the field to solve
*/ */
solve(field: Field): void { static solve(field: Field): void {
if (field.hasWon || field.hasLost) return; if (field.hasWon || field.hasLost) return;
if (field.hasStarted && !this.step(field.copy())) return; if (field.hasStarted && !this.step(field.copy())) return;
@ -52,7 +52,7 @@ export class Solver {
* @param field the field to check for solvability * @param field the field to check for solvability
* @param initialSquare the initial coordinates to click at * @param initialSquare the initial coordinates to click at
*/ */
canSolve(field: Field, initialSquare: { x: number, y: number } | undefined = undefined): boolean { static canSolve(field: Field, initialSquare: { x: number, y: number } | undefined = undefined): boolean {
const copy = field.copy(); const copy = field.copy();
if (initialSquare !== undefined) copy.runUndoably(() => copy.uncover(initialSquare)); if (initialSquare !== undefined) copy.runUndoably(() => copy.uncover(initialSquare));
this.solve(copy); this.solve(copy);
@ -65,7 +65,7 @@ export class Solver {
* @param field the field to suggest a move for * @param field the field to suggest a move for
* @returns a suggestion for a next move based on the current state of the field * @returns a suggestion for a next move based on the current state of the field
*/ */
getHint(field: Field): Square | null { static getHint(field: Field): Square | null {
if (!field.hasStarted || field.hasWon || field.hasLost) return null; if (!field.hasStarted || field.hasWon || field.hasLost) return null;
const knowns = Solver.getKnowns(field); const knowns = Solver.getKnowns(field);
@ -102,7 +102,7 @@ export class Solver {
* @returns `true` if a step could be solved * @returns `true` if a step could be solved
* @private * @private
*/ */
private step(field: Field): boolean { private static step(field: Field): boolean {
let flagCount = field.flagCount; let flagCount = field.flagCount;
let coveredCount = field.coveredNonMineCount; let coveredCount = field.coveredNonMineCount;
@ -134,7 +134,7 @@ export class Solver {
* @param field the field to solve * @param field the field to solve
* @private * @private
*/ */
private stepSingleSquares(field: Field): void { private static stepSingleSquares(field: Field): void {
Solver.getKnowns(field) Solver.getKnowns(field)
.forEach(square => { .forEach(square => {
field.chord(square); field.chord(square);
@ -155,7 +155,7 @@ export class Solver {
* @param field the field to solve * @param field the field to solve
* @private * @private
*/ */
private stepNeighboringSquares(field: Field): void { private static stepNeighboringSquares(field: Field): void {
const knowns = Solver.getKnowns(field); const knowns = Solver.getKnowns(field);
knowns.forEach(known => { knowns.forEach(known => {
Solver.applySolution( Solver.applySolution(
@ -174,7 +174,7 @@ export class Solver {
* @param field the field to solve * @param field the field to solve
* @private * @private
*/ */
private stepAllSquares(field: Field): void { private static stepAllSquares(field: Field): void {
if (!field.hasStarted || field.hasWon || field.hasLost) return; if (!field.hasStarted || field.hasWon || field.hasLost) return;
const knowns = Solver.getKnowns(field); const knowns = Solver.getKnowns(field);
@ -194,7 +194,7 @@ export class Solver {
* @returns the solution that has been found * @returns the solution that has been found
* @private * @private
*/ */
private matrixSolve(field: Field, knowns: Square[], adjacentSquaresOnly: boolean): Solution { private static matrixSolve(field: Field, knowns: Square[], adjacentSquaresOnly: boolean): Solution {
if (knowns.length === 0) return []; if (knowns.length === 0) return [];
let unknowns: Square[]; let unknowns: Square[];