fixes and cleanup
This commit is contained in:
parent
374c42802e
commit
bc211621e5
6 changed files with 199 additions and 83 deletions
|
@ -10,13 +10,15 @@ export class Runner<Ga extends Gaming, Gu extends Guessing> {
|
|||
delay_ms;
|
||||
turns;
|
||||
logging;
|
||||
max;
|
||||
|
||||
constructor(game: Ga, guesser: Gu, logging: LoggingStrategy, delay_ms = 0) {
|
||||
constructor(game: Ga, guesser: Gu, logging: LoggingStrategy, delay_ms = 0, max: number | undefined = undefined) {
|
||||
this.game = game;
|
||||
this.guesser = guesser;
|
||||
this.delay_ms = delay_ms;
|
||||
this.turns = [] as Turn[];
|
||||
this.logging = logging;
|
||||
this.max = max;
|
||||
}
|
||||
|
||||
async play_all() {
|
||||
|
@ -29,9 +31,11 @@ export class Runner<Ga extends Gaming, Gu extends Guessing> {
|
|||
return result;
|
||||
});
|
||||
if (result !== null) break;
|
||||
if (this.max !== undefined) if (this.turns.length >= this.max) return this.turns;
|
||||
await wait(this.delay_ms);
|
||||
}
|
||||
this.logging.on_finish(this.turns);
|
||||
return this.turns;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -99,7 +103,7 @@ export class TableLogging implements LoggingStrategy {
|
|||
this.columns = properties.map((p) => [p, p.length] as const);
|
||||
this.columns.splice(0, 0, ["guess", length], ["result", Math.max(7, length)]);
|
||||
let line = "";
|
||||
for (const [name, width] of this.columns) line += name.padStart(width) + " ";
|
||||
for (const [name, width] of this.columns) line += name.padStart(width) + " ";
|
||||
console.log(line);
|
||||
console.log();
|
||||
}
|
||||
|
@ -107,7 +111,7 @@ export class TableLogging implements LoggingStrategy {
|
|||
on_guess(guess: string, result: GuessResult, ...properties: unknown[]): void {
|
||||
const properties_ = [guess, format_result(result), ...properties];
|
||||
let line = "";
|
||||
for (const [[_, width], value] of zip(this.columns, properties_)) line += `${value}`.padStart(width) + " ";
|
||||
for (const [[_, width], value] of zip(this.columns, properties_)) line += `${value}`.padStart(width) + " ";
|
||||
console.log(line);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue