add interactive solver #4

Merged
cscherrNT merged 29 commits from feat/interactive-solver into devel 2024-07-25 15:19:54 +02:00
2 changed files with 19 additions and 6 deletions
Showing only changes of commit 740f2fbdc1 - Show all commits

View File

@ -54,9 +54,6 @@ struct Cli {
#[derive(Subcommand, Debug, EnumIter, Clone)] #[derive(Subcommand, Debug, EnumIter, Clone)]
enum ReplCommand { 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 /// Let the user input a word and the response for that word
/// ///
/// Evaluation Format: /// Evaluation Format:
@ -86,6 +83,10 @@ enum ReplCommand {
#[command(subcommand)] #[command(subcommand)]
cmd: WlCommand, cmd: WlCommand,
}, },
/// Start a new game
New,
/// Undo the last n operations
Undo { n: usize },
/// Leave the Repl /// Leave the Repl
Exit, Exit,
} }
@ -155,9 +156,13 @@ fn help_guess_interactive(cli: Cli) -> anyhow::Result<()> {
println!("{}", game); println!("{}", game);
} }
ReplCommand::Solve => { 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:?}"); debug!("game state: {game:?}");
println!("best guess: {best_guess}"); println!("best guess: {}", best_guess.unwrap());
} }
ReplCommand::Guess { ReplCommand::Guess {
your_guess, your_guess,
@ -174,7 +179,8 @@ fn help_guess_interactive(cli: Cli) -> anyhow::Result<()> {
println!("{}", guess.unwrap()); println!("{}", guess.unwrap());
debug!("game state: {game:#?}"); debug!("game state: {game:#?}");
} }
_ => todo!(), ReplCommand::New => game = builder.build()?,
ReplCommand::Undo { n } => game.undo(n)?,
} }
} }
Ok(()) Ok(())

View File

@ -133,6 +133,13 @@ impl<'wl, WL: WordList> Game<'wl, WL> {
evaluation.into() 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 { pub fn length(&self) -> usize {
self.length self.length
} }