From 567d02d48bd93f1aab17bbf0c01db96661edd781 Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Fri, 2 Aug 2024 17:37:36 +0200 Subject: [PATCH] refactor(game): guess only needs a &Word --- src/bin/game/cli.rs | 2 +- src/bin/solve/simple.rs | 2 +- src/game/mod.rs | 12 ++++++++++-- src/solve/mod.rs | 2 +- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/bin/game/cli.rs b/src/bin/game/cli.rs index 29ccdec..bf42a30 100644 --- a/src/bin/game/cli.rs +++ b/src/bin/game/cli.rs @@ -61,7 +61,7 @@ fn main() -> anyhow::Result<()> { let mut guess: Word; loop { guess = get_word(&cli, game.step())?; - response = match game.guess(guess, None) { + response = match game.guess(&guess, None) { Ok(r) => r, Err(err) => match err { GameError::GuessHasWrongLength(len) => { diff --git a/src/bin/solve/simple.rs b/src/bin/solve/simple.rs index 2f4c060..8ee5d8e 100644 --- a/src/bin/solve/simple.rs +++ b/src/bin/solve/simple.rs @@ -181,7 +181,7 @@ fn help_guess_interactive(cli: Cli) -> anyhow::Result<()> { } => { let evaluation_converted: Evaluation = Evaluation::build(&your_guess, &evalutation)?; - let guess = game.guess(your_guess, Some(evaluation_converted)); + let guess = game.guess(&your_guess, Some(evaluation_converted)); debug!("your guess: {guess:?}"); if guess.is_err() { eprintln!("{}", style(guess.unwrap_err()).red().bold()); diff --git a/src/game/mod.rs b/src/game/mod.rs index 9430c9b..9b34f65 100644 --- a/src/game/mod.rs +++ b/src/game/mod.rs @@ -101,7 +101,7 @@ impl<'wl, WL: WordList> Game<'wl, WL> { /// /// This function will return an error if the length of the [Word] is wrong It will also error /// if the game is finished. - pub fn guess(&mut self, guess: Word, eval: Option) -> GameResult { + pub fn guess(&mut self, guess: &Word, eval: Option) -> GameResult { if guess.len() != self.length { return Err(GameError::GuessHasWrongLength(guess.len())); } @@ -109,7 +109,7 @@ impl<'wl, WL: WordList> Game<'wl, WL> { return Err(GameError::TryingToPlayAFinishedGame); } if self.wordlist.get_word(&guess).is_none() { - return Err(GameError::WordNotInWordlist(guess)); + return Err(GameError::WordNotInWordlist(guess.to_string())); } self.step += 1; @@ -324,6 +324,14 @@ impl<'wl, WL: WordList> GameBuilder<'wl, WL> { self } + /// Enable or disable Generation of a solution for this builder + /// + /// Default is true + pub fn generate_solution(mut self, generate: bool) -> Self { + self.generate_solution = generate; + self + } + /// Set the solution for the games built by the builder /// /// If this is [Some], then the solution generated by diff --git a/src/solve/mod.rs b/src/solve/mod.rs index 1f00281..dc62e07 100644 --- a/src/solve/mod.rs +++ b/src/solve/mod.rs @@ -51,7 +51,7 @@ pub trait Solver<'wl, WL: WordList>: Clone + std::fmt::Debug + Sized + Sync { /// /// This function will return an error if [guess_for](Solver::guess_for) fails. fn make_a_move(&self, game: &mut Game<'wl, WL>) -> WResult { - Ok(game.guess(self.guess_for(game)?, None)?) + Ok(game.guess(&self.guess_for(game)?, None)?) } /// Play a [Game] and return the last [GuessResponse]. ///