refactor(game): guess only needs a &Word

This commit is contained in:
Christoph J. Scherr 2024-08-02 17:37:36 +02:00
parent b6f639da67
commit 567d02d48b
4 changed files with 13 additions and 5 deletions

View File

@ -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) => {

View File

@ -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());

View File

@ -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<Evaluation>) -> GameResult<GuessResponse> {
pub fn guess(&mut self, guess: &Word, eval: Option<Evaluation>) -> GameResult<GuessResponse> {
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

View File

@ -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<GuessResponse> {
Ok(game.guess(self.guess_for(game)?, None)?)
Ok(game.guess(&self.guess_for(game)?, None)?)
}
/// Play a [Game] and return the last [GuessResponse].
///