From b6b817e10c5f118831e50c2cbbceda0834ed1bd4 Mon Sep 17 00:00:00 2001 From: "Felix W. Dekker" Date: Fri, 7 Aug 2020 12:24:56 +0200 Subject: [PATCH] Add timer for finding solvable field --- package.json | 2 +- src/main/js/Display.ts | 2 +- src/main/js/Field.ts | 18 +++++++++++++----- src/main/js/Timer.ts | 41 +++++++++++++++++++++++++++++++++++++---- 4 files changed, 52 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 1b98d61..0161010 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "minesweeper", - "version": "0.78.1", + "version": "0.78.2", "description": "Just Minesweeper!", "author": "Felix W. Dekker", "browser": "dist/bundle.js", diff --git a/src/main/js/Display.ts b/src/main/js/Display.ts index 8565d4e..4763ab8 100644 --- a/src/main/js/Display.ts +++ b/src/main/js/Display.ts @@ -372,7 +372,7 @@ export class Display { ); // Time - const time = Math.floor(this.field.time / 1000); + const time = Math.floor(this.field.elapsedTime / 1000); const minutes = Math.floor(time / 60); const seconds = ("" + (time % 60)).padStart(2, "0"); ctx.drawImage( diff --git a/src/main/js/Field.ts b/src/main/js/Field.ts index 64debae..0d5c96f 100644 --- a/src/main/js/Field.ts +++ b/src/main/js/Field.ts @@ -220,9 +220,17 @@ export class Field { this.runUndoably(() => { if (!this.hasStarted) { - if (this.isSolvable) - while (!(new Solver()).canSolve(this, coords)) - this.shuffle(this.rng.uint32()); + if (this.isSolvable) { + let i = 1; + const time = Timer.time(() => { + const solver = new Solver(); + while (!solver.canSolve(this, coords)) { + this.shuffle(this.rng.uint32()); + i++; + } + }); + console.log(`Found solvable field in ${time}ms in ${i} attempts.`) + } this.clearMines(square); this.hasStarted = true; @@ -339,8 +347,8 @@ export class Field { /** * The time the player has played on this field so far. */ - get time(): number { - return this.timer.time; + get elapsedTime(): number { + return this.timer.elapsedTime; } diff --git a/src/main/js/Timer.ts b/src/main/js/Timer.ts index 608aebd..b12fba4 100644 --- a/src/main/js/Timer.ts +++ b/src/main/js/Timer.ts @@ -6,6 +6,15 @@ export class Timer { private readonly endTimes: number[] = []; + /** + * Constructs a new timer. + * + * @param start whether the timer should start running right away + */ + constructor(start: boolean = false) { + if (start) this.start(); + } + /** * Creates an exact copy of this timer. * @@ -22,14 +31,14 @@ export class Timer { /** * Returns true if the timer is currently running. */ - get isRunning() { + get isRunning(): boolean { return this.startTimes.length > this.endTimes.length; } /** * Returns the amount of milliseconds that the timer has been running. */ - get time() { + get elapsedTime(): number { return this.endTimes .map((_, i) => this.endTimes[i] - this.startTimes[i]) .reduce((sum, diff) => sum + diff, 0) @@ -40,7 +49,7 @@ export class Timer { /** * Starts or resumes the timer. */ - start() { + start(): void { if (this.isRunning) return; this.startTimes.push(Date.now()); @@ -49,9 +58,33 @@ export class Timer { /** * Stops or pauses the timer. */ - stop() { + stop(): void { if (!this.isRunning) return; this.endTimes.push(Date.now()); } + + /** + * Runs the given callback and adds its execution time to this timer. + * + * @param callback the callback to time the execution of + */ + time(callback: () => void): void { + this.start(); + callback(); + this.stop(); + } + + + /** + * Runs the given callback and returns the number of milliseconds it took to execute. + * + * @param callback the function to time the execution of + * @returns the number of milliseconds the callback took to execute + */ + static time(callback: () => void): number { + const timer = new Timer(); + timer.time(callback); + return timer.elapsedTime; + } }