add control flags to simulation

This commit is contained in:
JOLIMAITRE Matthieu 2024-05-02 16:27:59 +02:00
parent aab2e60dab
commit 9d36edecc9
3 changed files with 39 additions and 8 deletions

View file

@ -1,6 +1,8 @@
export { Dict } from "./dict.ts"; export { Dict } from "./dict.ts";
export type { Guessing } from "./guesser/guesser.ts";
export { BaseGuesser } from "./guesser/base.ts"; export { BaseGuesser } from "./guesser/base.ts";
export { ReducingGuesser } from "./guesser/reducing.ts"; export { ReducingGuesser } from "./guesser/reducing.ts";
export { Simulator } from "./game/simulator.ts"; export { Simulator } from "./game/simulator.ts";
export { ManualProxy } from "./game/proxy.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";

View file

@ -20,7 +20,7 @@ export class Runner<Ga extends Gaming, Gu extends Guessing> {
} }
async play_all() { async play_all() {
this.logging.on_start(this.game.length()); this.logging.on_start(this.game.length(), ...this.guesser.declare_properties());
while (true) { while (true) {
const result = await this.guesser.guess(async (guess, ...properties) => { const result = await this.guesser.guess(async (guess, ...properties) => {
const result = await this.game.guess(guess); const result = await this.game.guess(guess);
@ -48,7 +48,7 @@ function format_result(result: GuessResult) {
return line; return line;
} }
interface LoggingStrategy { export interface LoggingStrategy {
on_start(length: number, ...properties: string[]): void; on_start(length: number, ...properties: string[]): void;
on_guess(guess: string, result: GuessResult, ...properties: unknown[]): void; on_guess(guess: string, result: GuessResult, ...properties: unknown[]): void;
on_finish(turns: Turn[]): void; on_finish(turns: Turn[]): void;
@ -97,7 +97,7 @@ export class TableLogging implements LoggingStrategy {
on_start(length: number, ...properties: string[]): void { on_start(length: number, ...properties: string[]): void {
this.columns = properties.map((p) => [p, p.length] as const); 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 = ""; 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(line);

View file

@ -2,8 +2,17 @@
import { Command } from "https://deno.land/x/cliffy@v1.0.0-rc.4/command/mod.ts"; 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 {
import { TableLogging } from "./lib/runner.ts"; BaseGuesser,
Dict,
Guessing,
LoggingStrategy,
ReducingGuesser,
Runner,
Simulator,
TableLogging,
VerboseLogging,
} from "./lib/lib.ts";
import { francais } from "../data/data.ts"; import { francais } from "../data/data.ts";
@ -25,13 +34,23 @@ async function main() {
).option( ).option(
"-t, --target <target:string>", "-t, --target <target:string>",
"Target word to search for.", "Target word to search for.",
).parse(Deno.args); ).option(
"-g, --guesser <guesser:string>",
`Guesser to use, available are ${[...guessers.keys()]}.`,
{ default: "reducing" },
)
.option(
"-l, --logger <logger:string>",
`Logger to use, available are ${[...loggers.keys()]}.`,
{ default: "verbose" },
)
.parse(Deno.args);
const length = args.options.length ?? args.options.target?.length ?? 6; const length = args.options.length ?? args.options.target?.length ?? 6;
let dict = Dict.from_lines(francais, length); let dict = Dict.from_lines(francais, length);
if (args.options.file !== undefined) dict = await Dict.from_text_file(args.options.file, 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); let game = Simulator.from_dict_rand(dict);
if (args.options.target !== undefined) game = new Simulator(validate_target(args.options.target, length)); if (args.options.target !== undefined) game = new Simulator(validate_target(args.options.target, length));
console.log("Target is", game.word); console.log("Target is", game.word);
@ -46,4 +65,14 @@ function validate_target(target: string, length: number) {
return target.toLowerCase(); return target.toLowerCase();
} }
const guessers = new Map<string, (dict: Dict) => Guessing>([
["explorer", (dict: Dict) => new BaseGuesser(dict)],
["reducing", (dict: Dict) => new ReducingGuesser(dict)],
]);
const loggers = new Map<string, LoggingStrategy>([
["verbose", new VerboseLogging()],
["table", new TableLogging()],
]);
if (import.meta.main) await main(); if (import.meta.main) await main();