diff --git a/src/lib/guesser/reducing.ts b/src/lib/guesser/reducing.ts index a29177c..aa7f090 100644 --- a/src/lib/guesser/reducing.ts +++ b/src/lib/guesser/reducing.ts @@ -6,11 +6,7 @@ import { GuessResult, Info } from "../game/game.ts"; import { info_of_guess } from "../game/simulator.ts"; import { Awaitable, dbg, enumerate, first, range, zip } from "../utils.ts"; import { Guessing, pick_random_common_word } from "./guesser.ts"; -import { - section, - tip, - top, -} from "https://git.barnulf.net/mb/profiterole/raw/commit/02d19fce2a0878abd176801cf8f2a663f6db6c16/mod.ts"; +import { tip, top } from "https://git.barnulf.net/mb/profiterole/raw/branch/master/mod.ts"; export class ReducingGuesser implements Guessing { length; @@ -27,22 +23,27 @@ export class ReducingGuesser implements Guessing { return ["candidates", "best score"]; } - public async guess(try_: (guess: string, candidates: number, score: number) => Awaitable) { - return await section("guess", async () => { - if (this.candidates.size === 1) return await try_(first(this.candidates)!, this.candidates.size, 1); - const [guess, score] = get_word_with_smallest_cuts(this.candidates, this.words); - if (score >= this.candidates.size) { - const pick = pick_random_common_word([...this.candidates.values()]); - assertExists(pick); - const result = await try_(pick, this.candidates.size, score); - if (result.kind === "success") return result; - else return null; - } - const result = await try_(guess, this.candidates.size, score); + public async guess_impl(try_: (guess: string, candidates: number, score: number) => Awaitable) { + if (this.candidates.size === 1) return await try_(first(this.candidates)!, this.candidates.size, 1); + const [guess, score] = get_word_with_smallest_cuts(this.candidates, this.words); + if (score >= this.candidates.size) { + const pick = pick_random_common_word([...this.candidates.values()]); + assertExists(pick); + const result = await try_(pick, this.candidates.size, score); if (result.kind === "success") return result; - this.learn(guess, result.informations); - return null; - }); + else return null; + } + const result = await try_(guess, this.candidates.size, score); + if (result.kind === "success") return result; + this.learn(guess, result.informations); + return null; + } + + public async guess(try_: (guess: string, candidates: number, score: number) => Awaitable) { + tip("guess"); + const res = await this.guess_impl(try_); + top("guess"); + return res; } learn(word: string, infos: Info[]) { @@ -115,18 +116,18 @@ function word_cuts(word: string, dict: Set) { } function get_word_with_smallest_cuts(candidates: Set, dict: Set) { - return section("get_word_with_smallest_cuts", () => { - let best = null as null | [string, number]; - for (const candidate of dict.values()) { - const cuts = word_cuts(candidate, candidates); - let max_part_size = 0; - for (const cut of cuts) for (const part of cut) if (part.size > max_part_size) max_part_size = part.size; - if (best === null) best = [candidate, max_part_size]; - else if (max_part_size < best[1]) best = [candidate, max_part_size]; - } - assertExists(best); - return best; - }); + tip("get_word_with_smallest_cuts"); + let best = null as null | [string, number]; + for (const candidate of dict.values()) { + const cuts = word_cuts(candidate, candidates); + let max_part_size = 0; + for (const cut of cuts) for (const part of cut) if (part.size > max_part_size) max_part_size = part.size; + if (best === null) best = [candidate, max_part_size]; + else if (max_part_size < best[1]) best = [candidate, max_part_size]; + } + assertExists(best); + top("get_word_with_smallest_cuts"); + return best; } function matches_constraints(candidate: string, constraints: [string, Info][]) { diff --git a/src/lib/prompt.ts b/src/lib/prompt.ts index e377a74..80f866f 100644 --- a/src/lib/prompt.ts +++ b/src/lib/prompt.ts @@ -1,6 +1,8 @@ import { assertExists } from "https://deno.land/std@0.224.0/assert/assert_exists.ts"; -import { async_next, enumerate, LineReader, next } from "./utils.ts"; +import { enumerate, LineReader } from "./utils.ts"; +import { writeAll } from "https://deno.land/std@0.224.0/io/write_all.ts"; +import { Writer } from "https://deno.land/std@0.224.0/io/types.ts"; export async function initialize_prompt(lines: LineReader) { console.log("Please input initial state of the game. Format is :"); @@ -20,13 +22,13 @@ export async function initialize_prompt(lines: LineReader) { } async function parse_initialization_until_correct(lines: LineReader) { - console.log("initial : "); + print(Deno.stdout, "initial : "); while (true) { const input = await lines.read(); assertExists(input); const parsed = parse_initialization(input); if (parsed !== null) return parsed; - console.log("Invalid, please try again : "); + print(Deno.stdout, "Invalid, please try again : "); } } @@ -45,3 +47,8 @@ function parse_initialization(input: string) { } return { length, constraints }; } + +async function print(writer: Writer, ...rest: unknown[]) { + const text = rest.map((o) => `${o}`).join(" "); + await writeAll(writer, new TextEncoder().encode(text)); +} diff --git a/src/lib/runner.ts b/src/lib/runner.ts index bd4bcf0..5fa9229 100644 --- a/src/lib/runner.ts +++ b/src/lib/runner.ts @@ -3,10 +3,7 @@ import { assertExists } from "https://deno.land/std@0.224.0/assert/assert_exists import { Guessing } from "./guesser/guesser.ts"; import { Gaming, GuessResult } from "./game/game.ts"; import { last, wait, zip } from "./utils.ts"; -import { - tip, - top, -} from "https://git.barnulf.net/mb/profiterole/raw/commit/02d19fce2a0878abd176801cf8f2a663f6db6c16/mod.ts"; +import { tip, top } from "https://git.barnulf.net/mb/profiterole/raw/branch/master/mod.ts"; export class Runner { game; @@ -42,7 +39,7 @@ export class Runner { while (true) { const result = await this.play_once(); if (result !== null) break; - if (this.max !== undefined) { if (this.turns.length >= this.max) return this.turns; } + if (this.max !== undefined) if (this.turns.length >= this.max) return this.turns; await wait(this.delay_ms); } this.logging.on_finish(this.turns); diff --git a/src/manual_proxy.ts b/src/manual_proxy.ts index 93a99ea..6dce195 100755 --- a/src/manual_proxy.ts +++ b/src/manual_proxy.ts @@ -1,4 +1,4 @@ -#!/usr/bin/env -S deno run --allow-read +#!/usr/bin/env -S deno run --allow-read --allow-env import { Command } from "https://deno.land/x/cliffy@v1.0.0-rc.4/command/mod.ts"; diff --git a/src/simulation.ts b/src/simulation.ts index 30ebf50..0b965ee 100755 --- a/src/simulation.ts +++ b/src/simulation.ts @@ -1,13 +1,7 @@ #!/usr/bin/env -S deno run -A --unstable-temporal import { Command } from "https://deno.land/x/cliffy@v1.0.0-rc.4/command/mod.ts"; -import { - report, - section, - startup, - tip, - top, -} from "https://git.barnulf.net/mb/profiterole/raw/commit/02d19fce2a0878abd176801cf8f2a663f6db6c16/mod.ts"; +import { report, tip, top } from "https://git.barnulf.net/mb/profiterole/raw/branch/master/mod.ts"; import { Dict,