diff --git a/src/lib/lib.ts b/src/lib/lib.ts index fd3428c..d43b1cb 100644 --- a/src/lib/lib.ts +++ b/src/lib/lib.ts @@ -1,6 +1,8 @@ export { Dict } from "./dict.ts"; +export type { Guessing } from "./guesser/guesser.ts"; export { BaseGuesser } from "./guesser/base.ts"; export { ReducingGuesser } from "./guesser/reducing.ts"; export { Simulator } from "./game/simulator.ts"; export { ManualProxy } from "./game/proxy.ts"; -export { Runner } from "./runner.ts"; +export { Runner, TableLogging, VerboseLogging } from "./runner.ts"; +export type { LoggingStrategy } from "./runner.ts"; diff --git a/src/lib/runner.ts b/src/lib/runner.ts index 60c3254..518e40e 100644 --- a/src/lib/runner.ts +++ b/src/lib/runner.ts @@ -20,7 +20,7 @@ export class Runner { } async play_all() { - this.logging.on_start(this.game.length()); + this.logging.on_start(this.game.length(), ...this.guesser.declare_properties()); while (true) { const result = await this.guesser.guess(async (guess, ...properties) => { const result = await this.game.guess(guess); @@ -48,7 +48,7 @@ function format_result(result: GuessResult) { return line; } -interface LoggingStrategy { +export interface LoggingStrategy { on_start(length: number, ...properties: string[]): void; on_guess(guess: string, result: GuessResult, ...properties: unknown[]): void; on_finish(turns: Turn[]): void; @@ -97,7 +97,7 @@ export class TableLogging implements LoggingStrategy { on_start(length: number, ...properties: string[]): void { this.columns = properties.map((p) => [p, p.length] as const); - this.columns.splice(0, 0, ["guess", length], ["result", length]); + 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) + " "; console.log(line); diff --git a/src/simulation.ts b/src/simulation.ts index 485ddec..8b23403 100755 --- a/src/simulation.ts +++ b/src/simulation.ts @@ -2,8 +2,17 @@ import { Command } from "https://deno.land/x/cliffy@v1.0.0-rc.4/command/mod.ts"; -import { BaseGuesser, Dict, Runner, Simulator } from "./lib/lib.ts"; -import { TableLogging } from "./lib/runner.ts"; +import { + BaseGuesser, + Dict, + Guessing, + LoggingStrategy, + ReducingGuesser, + Runner, + Simulator, + TableLogging, + VerboseLogging, +} from "./lib/lib.ts"; import { francais } from "../data/data.ts"; @@ -25,13 +34,23 @@ async function main() { ).option( "-t, --target ", "Target word to search for.", - ).parse(Deno.args); + ).option( + "-g, --guesser ", + `Guesser to use, available are ${[...guessers.keys()]}.`, + { default: "reducing" }, + ) + .option( + "-l, --logger ", + `Logger to use, available are ${[...loggers.keys()]}.`, + { default: "verbose" }, + ) + .parse(Deno.args); const length = args.options.length ?? args.options.target?.length ?? 6; let dict = Dict.from_lines(francais, length); if (args.options.file !== undefined) dict = await Dict.from_text_file(args.options.file, length); - const guesser = new BaseGuesser(dict); + const guesser = guessers.get(args.options.guesser)!(dict); let game = Simulator.from_dict_rand(dict); if (args.options.target !== undefined) game = new Simulator(validate_target(args.options.target, length)); console.log("Target is", game.word); @@ -46,4 +65,14 @@ function validate_target(target: string, length: number) { return target.toLowerCase(); } +const guessers = new Map Guessing>([ + ["explorer", (dict: Dict) => new BaseGuesser(dict)], + ["reducing", (dict: Dict) => new ReducingGuesser(dict)], +]); + +const loggers = new Map([ + ["verbose", new VerboseLogging()], + ["table", new TableLogging()], +]); + if (import.meta.main) await main();