add manual proxy program

This commit is contained in:
JOLIMAITRE Matthieu 2024-04-30 18:17:23 +02:00
parent e058363a52
commit 626e639f59
5 changed files with 76 additions and 21 deletions

View file

@ -12,7 +12,9 @@ export class ManualProxy implements Gaming {
guess(guess: string, _known: string): Awaitable<GuessResult> {
console.log("<proxy> Guessing:", guess);
return read_until_correct(this.word_length);
const result = read_until_correct(this.word_length);
console.log();
return result;
}
length(): number {
@ -25,7 +27,10 @@ function read_until_correct(length: number): GuessResult {
const input = prompt("<proxy> Result:");
assertExists(input);
const informations = parse_input(input, length);
if (informations === null) continue;
if (informations === null) {
console.log("<proxy> incorrect input, try again");
continue;
}
if (informations.every((i) => i.kind === "there")) return { kind: "success" };
return { kind: "failure", informations };
}
@ -34,9 +39,9 @@ function read_until_correct(length: number): GuessResult {
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 (character === ".") parsed.push({ kind: "abscent" });
if (character === "-") parsed.push({ kind: "somewhere" });
if (character === "+") parsed.push({ kind: "there" });
}
if (parsed.length !== length) return null;
else return parsed;