diff --git a/src/lib/guesser.ts b/src/lib/guesser.ts index 6ba23d9..c0afca6 100644 --- a/src/lib/guesser.ts +++ b/src/lib/guesser.ts @@ -1,9 +1,13 @@ import { assertExists } from "https://deno.land/std@0.224.0/assert/assert_exists.ts"; -import { enumerate, range } from "./utils.ts"; +import { Awaitable, enumerate, range } from "./utils.ts"; import { Dict } from "./dict.ts"; import { GuessResult } from "./game/game.ts"; +export interface Guessing { + guess(try_: (guess: string, known: string) => Awaitable): Promise; +} + type Knowledge = { letter: string; at: Set; @@ -11,7 +15,7 @@ type Knowledge = { not_at: Set; }; -export class Gueser { +export class Guesser implements Guessing { length; dict; informations; @@ -25,9 +29,9 @@ export class Gueser { } } - async next_guess(operation: (guess: string, known: string) => GuessResult | Promise) { + async guess(try_: (guess: string, known: string) => Awaitable) { const res = this.expected_result(); - if (res.completed) return operation(res.result, res.result); + if (res.completed) return await try_(res.result, res.result); const words = [...this.dict.words.values()]; const scored = words.map((word) => [this.info_score_for(word), word] as const); @@ -35,8 +39,8 @@ export class Gueser { const bests = scored.filter(([s]) => s === best_score).map(([_, word]) => word); const guess = pick_random_common_word(bests); - const result = await operation(guess, res.result); - if (result.kind === "success") return guess; + const result = await try_(guess, res.result); + if (result.kind === "success") return result; for (const [index, info] of enumerate(result.informations)) { const letter = guess[index]; if (info.kind === "there") this.learn_letter_at(letter, index); @@ -46,6 +50,7 @@ export class Gueser { this.learn_letter_not_at(letter, index); } } + return null; } learn_does_not_exist(letter: string) {