From 740f2fbdc13873728c8a891af89170e607980d9d Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Thu, 25 Jul 2024 15:17:54 +0200 Subject: [PATCH] feat(solve): new and undo #7 --- src/bin/solve/simple.rs | 18 ++++++++++++------ src/game/mod.rs | 7 +++++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/bin/solve/simple.rs b/src/bin/solve/simple.rs index a5fca3c..19e274a 100644 --- a/src/bin/solve/simple.rs +++ b/src/bin/solve/simple.rs @@ -54,9 +54,6 @@ struct Cli { #[derive(Subcommand, Debug, EnumIter, Clone)] enum ReplCommand { - /// Let the user input the response to the last guess - /// - Response { encoded: String }, /// Let the user input a word and the response for that word /// /// Evaluation Format: @@ -86,6 +83,10 @@ enum ReplCommand { #[command(subcommand)] cmd: WlCommand, }, + /// Start a new game + New, + /// Undo the last n operations + Undo { n: usize }, /// Leave the Repl Exit, } @@ -155,9 +156,13 @@ fn help_guess_interactive(cli: Cli) -> anyhow::Result<()> { println!("{}", game); } ReplCommand::Solve => { - let best_guess = solver.guess_for(&game)?; + let best_guess = solver.guess_for(&game); + if best_guess.is_err() { + eprintln!("{}", style(best_guess.unwrap_err()).red().bold()); + continue; + } debug!("game state: {game:?}"); - println!("best guess: {best_guess}"); + println!("best guess: {}", best_guess.unwrap()); } ReplCommand::Guess { your_guess, @@ -174,7 +179,8 @@ fn help_guess_interactive(cli: Cli) -> anyhow::Result<()> { println!("{}", guess.unwrap()); debug!("game state: {game:#?}"); } - _ => todo!(), + ReplCommand::New => game = builder.build()?, + ReplCommand::Undo { n } => game.undo(n)?, } } Ok(()) diff --git a/src/game/mod.rs b/src/game/mod.rs index d3dbde7..6873311 100644 --- a/src/game/mod.rs +++ b/src/game/mod.rs @@ -133,6 +133,13 @@ impl<'wl, WL: WordList> Game<'wl, WL> { evaluation.into() } + /// discard the last n responses + pub fn undo(&mut self, n: usize) -> WResult<()> { + self.responses + .drain(self.responses.len() - n..self.responses.len()); + Ok(()) + } + pub fn length(&self) -> usize { self.length }