diff --git a/package.json b/package.json index 98ec783..c6e4ea5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "minesweeper", - "version": "0.79.2", + "version": "0.79.3", "description": "Just Minesweeper!", "author": "Felix W. Dekker", "browser": "dist/bundle.js", diff --git a/src/main/js/Field.ts b/src/main/js/Field.ts index fd9b939..26c9920 100644 --- a/src/main/js/Field.ts +++ b/src/main/js/Field.ts @@ -38,11 +38,13 @@ export class Field { return this._hasStarted; } + private _hasWonBefore: boolean = false; private _hasWon: boolean = false; get hasWon(): boolean { return this._hasWon; } + private _hasLostBefore: boolean = false; private _hasLost: boolean = false; get hasLost(): boolean { return this._hasLost; @@ -53,6 +55,8 @@ export class Field { return this._deathCount; } + wasAutoSolved: boolean = false; + /** * Constructs a new playing field for a game of Minesweeper. @@ -290,7 +294,11 @@ export class Field { this.timer.stop(); this._hasLost = true; this._deathCount++; - this.statistics.gamesLost++; + + if (!this._hasLostBefore && !this.wasAutoSolved) { + this._hasLostBefore = true; + this.statistics.gamesLost++; + } } else { this._coveredNonMineCount--; if (this.coveredNonMineCount === 0) { @@ -299,7 +307,13 @@ export class Field { remainingFlags.forEach(it => it.hasFlag = true); this._flagCount = this.mineCount; this._hasWon = true; - this.statistics.gamesWon++; + + if (!this._hasWonBefore && !this.wasAutoSolved) { + this._hasWonBefore = true; + this.statistics.gamesWon++; + if (this.deathCount === 0) + this.statistics.gamesWonWithoutLosing++; + } } } }, diff --git a/src/main/js/Solver.ts b/src/main/js/Solver.ts index 733fded..8469e8e 100644 --- a/src/main/js/Solver.ts +++ b/src/main/js/Solver.ts @@ -15,6 +15,7 @@ export class Solver { */ solve(field: Field): void { if (field.hasWon || field.hasLost) return; + field.wasAutoSolved = true; if (!field.hasStarted) { field.runUndoably(() => { diff --git a/src/main/js/Statistics.ts b/src/main/js/Statistics.ts index a5c9012..7a2e4a9 100644 --- a/src/main/js/Statistics.ts +++ b/src/main/js/Statistics.ts @@ -12,6 +12,7 @@ export interface Statistics { gamesStarted: number; gamesLost: number; gamesWon: number; + gamesWonWithoutLosing: number; squaresChorded: number; squaresChordedLeadingToLoss: number; @@ -118,6 +119,16 @@ export class LocalStatistics implements Statistics { this.write(statistics); } + get gamesWonWithoutLosing(): number { + return +(this.read()["gamesWonWithoutLosing"] ?? 0); + } + + set gamesWonWithoutLosing(value: number) { + const statistics = this.read(); + statistics["gamesWonWithoutLosing"] = "" + value; + this.write(statistics); + } + get squaresChorded(): number { return +(this.read()["squaresChorded"] ?? 0); } @@ -208,6 +219,10 @@ export class LocalStatistics implements Statistics { Games won ${this.gamesWon} + + Games won without losing + ${this.gamesWonWithoutLosing} +

Steps taken

@@ -242,6 +257,7 @@ export class MemoryStatistics implements Statistics { gamesLost: number = 0; gamesStarted: number = 0; gamesWon: number = 0; + gamesWonWithoutLosing: number = 0; squaresChorded: number = 0; squaresChordedLeadingToLoss: number = 0; squaresFlagged: number = 0; @@ -256,6 +272,7 @@ export class MemoryStatistics implements Statistics { this.gamesLost = 0; this.gamesStarted = 0; this.gamesWon = 0; + this.gamesWonWithoutLosing = 0; this.squaresChorded = 0; this.squaresChordedLeadingToLoss = 0; this.squaresFlagged = 0; diff --git a/src/main/js/UI.ts b/src/main/js/UI.ts index 54a1cd3..c5c792d 100644 --- a/src/main/js/UI.ts +++ b/src/main/js/UI.ts @@ -29,10 +29,10 @@ export class Overlay { this.hide(); } ); - overlay.addEventListener( + document.addEventListener( "keydown", event => { - if (event.key === "Escape") + if (event.key === "Escape" && this.isVisible()) this.hide(); } ); @@ -73,4 +73,11 @@ export class Overlay { hide(): void { this.overlay.style.visibility = "hidden"; } + + /** + * Returns `true` if and only if this overlay is currently visible. + */ + isVisible(): boolean { + return this.overlay.style.visibility !== "hidden"; + } }