Add timer for finding solvable field

This commit is contained in:
Florine W. Dekker 2020-08-07 12:24:56 +02:00
parent 3ec38607ca
commit b6b817e10c
Signed by: FWDekker
GPG Key ID: B1B567AF58D6EE0F
4 changed files with 52 additions and 11 deletions

View File

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

View File

@ -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(

View File

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

View File

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