add manual proxied game solving

This commit is contained in:
JOLIMAITRE Matthieu 2024-04-30 17:40:49 +02:00
parent f7ebdbf9b3
commit e058363a52
4 changed files with 83 additions and 0 deletions

43
src/lib/game/proxy.ts Normal file
View file

@ -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<GuessResult> {
console.log("<proxy> 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("<proxy> 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;
}

View file

@ -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";