add target arg to sim to search specific words

This commit is contained in:
JOLIMAITRE Matthieu 2024-04-30 19:43:40 +02:00
parent 2e9c90dbca
commit d0de8f867f

View file

@ -18,18 +18,22 @@ async function main() {
).option( ).option(
"-n, --length <length:number>", "-n, --length <length:number>",
"Length of the word to use from the dictionnary.", "Length of the word to use from the dictionnary.",
{ default: 6 },
).option( ).option(
"-w, --wait <wait:number>", "-w, --wait <wait:number>",
"Time to wait between guesses, in ms.", "Time to wait between guesses, in ms.",
{ default: 0 }, { default: 0 },
).option(
"-t, --target <target:string>",
"Target word to search for.",
).parse(Deno.args); ).parse(Deno.args);
const length = args.options.length ?? args.options.target?.length ?? 6;
let dict = Dict.from_lines(francais, args.options.length); let dict = Dict.from_lines(francais, length);
if (args.options.file !== undefined) dict = await Dict.from_text_file(args.options.file, args.options.length); if (args.options.file !== undefined) dict = await Dict.from_text_file(args.options.file, length);
const guesser = new BaseGuesser(dict); const guesser = new BaseGuesser(dict);
const 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));
console.log("Target is", game.word); console.log("Target is", game.word);
console.log(); console.log();
@ -37,4 +41,9 @@ async function main() {
await runner.play_all(); await runner.play_all();
} }
function validate_target(target: string, length: number) {
if (target.length !== length) throw new Error("Invalid target length");
return target.toLowerCase();
}
if (import.meta.main) await main(); if (import.meta.main) await main();