From e058363a52ca03c392311031c0d9466f10d66563 Mon Sep 17 00:00:00 2001 From: JOLIMAITRE Matthieu Date: Tue, 30 Apr 2024 17:40:49 +0200 Subject: [PATCH] add manual proxied game solving --- build.sh | 1 + src/lib/game/proxy.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ src/lib/lib.ts | 1 + src/manual_proxy.ts | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+) create mode 100644 src/lib/game/proxy.ts create mode 100644 src/manual_proxy.ts diff --git a/build.sh b/build.sh index 57e17f1..52377e9 100755 --- a/build.sh +++ b/build.sh @@ -4,4 +4,5 @@ cd "$(dirname "$(realpath "$0")")" mkdir -p bin +deno compile -o bin/manual_proxy src/manual_proxy.ts deno compile -o bin/simulation src/simulation.ts diff --git a/src/lib/game/proxy.ts b/src/lib/game/proxy.ts new file mode 100644 index 0000000..1e55085 --- /dev/null +++ b/src/lib/game/proxy.ts @@ -0,0 +1,43 @@ +import { assertExists } from "https://deno.land/std@0.224.0/assert/assert_exists.ts"; + +import { Awaitable } from "../utils.ts"; +import { Gaming, GuessResult, Info } from "./game.ts"; + +export class ManualProxy implements Gaming { + word_length; + + constructor(length: number) { + this.word_length = length; + } + + guess(guess: string, _known: string): Awaitable { + console.log(" Guessing:", guess); + return read_until_correct(this.word_length); + } + + length(): number { + return this.word_length; + } +} + +function read_until_correct(length: number): GuessResult { + while (true) { + const input = prompt(" Result:"); + assertExists(input); + const informations = parse_input(input, length); + if (informations === null) continue; + if (informations.every((i) => i.kind === "there")) return { kind: "success" }; + return { kind: "failure", informations }; + } +} + +function parse_input(input: string, length: number) { + const parsed = [] as Info[]; + for (const character of input.trim()) { + if (character === "n") parsed.push({ kind: "abscent" }); + if (character === "i") parsed.push({ kind: "somewhere" }); + if (character === "y") parsed.push({ kind: "there" }); + } + if (parsed.length !== length) return null; + else return parsed; +} diff --git a/src/lib/lib.ts b/src/lib/lib.ts index 3ad2e8e..bd84cb1 100644 --- a/src/lib/lib.ts +++ b/src/lib/lib.ts @@ -1,4 +1,5 @@ export { Dict } from "./dict.ts"; export { Guesser } from "./guesser.ts"; export { Simulator } from "./game/simulator.ts"; +export { ManualProxy } from "./game/proxy.ts"; export { Runner } from "./runner.ts"; diff --git a/src/manual_proxy.ts b/src/manual_proxy.ts new file mode 100644 index 0000000..c0a80b7 --- /dev/null +++ b/src/manual_proxy.ts @@ -0,0 +1,38 @@ +#!/usr/bin/env -S deno run --allow-read + +import { Command } from "https://deno.land/x/cliffy@v1.0.0-rc.4/command/mod.ts"; + +import { Dict, Guesser, ManualProxy, Runner } from "./lib/lib.ts"; +import { VerboseLogging } from "./lib/runner.ts"; + +import { francais } from "../data/data.ts"; + +async function main() { + const args = await new Command().name("manual_proxy") + .description( + "Program to solve manually proxied TUSMO games with guesser controller.", + ) + .option( + "-f, --file ", + "Sets dictionnary to use words from (defaults to internal french dict).", + ).option( + "-n, --length ", + "Length of the word to use from the dictionnary.", + { default: 6 }, + ).option( + "-w, --wait ", + "Time to wait between guesses, in ms.", + { default: 0 }, + ).parse(Deno.args); + + let dict = Dict.from_lines(francais, args.options.length); + if (args.options.file !== undefined) dict = await Dict.from_text_file(args.options.file, args.options.length); + + const guesser = new Guesser(dict); + const game = new ManualProxy(args.options.length); + + const runner = new Runner(game, guesser, new VerboseLogging(), args.options.wait); + await runner.play_all(); +} + +if (import.meta.main) await main();