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",
"version": "0.82.12",
"version": "0.82.13",
"description": "Just Minesweeper!",
"author": "F.W. Dekker",
"browser": "dist/bundle.js",

View File

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

View File

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

View File

@ -13,7 +13,7 @@ export class Solver {
*
* @param field the field to solve
*/
solve(field: Field): void {
static solve(field: Field): void {
if (field.hasWon || field.hasLost) 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 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();
if (initialSquare !== undefined) copy.runUndoably(() => copy.uncover(initialSquare));
this.solve(copy);
@ -65,7 +65,7 @@ export class Solver {
* @param field the field to suggest a move for
* @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;
const knowns = Solver.getKnowns(field);
@ -102,7 +102,7 @@ export class Solver {
* @returns `true` if a step could be solved
* @private
*/
private step(field: Field): boolean {
private static step(field: Field): boolean {
let flagCount = field.flagCount;
let coveredCount = field.coveredNonMineCount;
@ -134,7 +134,7 @@ export class Solver {
* @param field the field to solve
* @private
*/
private stepSingleSquares(field: Field): void {
private static stepSingleSquares(field: Field): void {
Solver.getKnowns(field)
.forEach(square => {
field.chord(square);
@ -155,7 +155,7 @@ export class Solver {
* @param field the field to solve
* @private
*/
private stepNeighboringSquares(field: Field): void {
private static stepNeighboringSquares(field: Field): void {
const knowns = Solver.getKnowns(field);
knowns.forEach(known => {
Solver.applySolution(
@ -174,7 +174,7 @@ export class Solver {
* @param field the field to solve
* @private
*/
private stepAllSquares(field: Field): void {
private static stepAllSquares(field: Field): void {
if (!field.hasStarted || field.hasWon || field.hasLost) return;
const knowns = Solver.getKnowns(field);
@ -194,7 +194,7 @@ export class Solver {
* @returns the solution that has been found
* @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 [];
let unknowns: Square[];