improve naive solver #13

Open
cscherrNT wants to merge 13 commits from feat/naive into devel
4 changed files with 4 additions and 137 deletions
Showing only changes of commit f63c3d0449 - Show all commits

View File

@ -107,16 +107,16 @@ impl<'wl, WL: WordList> Game<'wl, WL> {
if self.finished() || self.step > self.max_steps { if self.finished() || self.step > self.max_steps {
return Err(GameError::TryingToPlayAFinishedGame); return Err(GameError::TryingToPlayAFinishedGame);
} }
if self.wordlist.get_word(&guess).is_none() { if self.wordlist.get_word(guess).is_none() {
return Err(GameError::WordNotInWordlist(guess.to_string())); return Err(GameError::WordNotInWordlist(guess.to_string()));
} }
self.step += 1; self.step += 1;
let response; let response;
if eval.is_some() && self.solution.is_none() { if eval.is_some() && self.solution.is_none() {
response = GuessResponse::new(&guess, eval.unwrap(), self); response = GuessResponse::new(guess, eval.unwrap(), self);
} else if let Some(solution) = self.solution.clone() { } else if let Some(solution) = self.solution.clone() {
response = GuessResponse::new(&guess, Self::evaluate(solution, &guess), self); response = GuessResponse::new(guess, Self::evaluate(solution, guess), self);
} else { } else {
panic!("there is neither an evaluation nor a predefined solution for this guess"); panic!("there is neither an evaluation nor a predefined solution for this guess");
} }

View File

@ -2,7 +2,6 @@ use std::collections::{HashMap, HashSet};
use std::fmt::Debug; use std::fmt::Debug;
use std::ops::{Range, RangeBounds}; use std::ops::{Range, RangeBounds};
use crate::error::WResult;
use crate::wlist::word::Word; use crate::wlist::word::Word;
pub(crate) type CharMap = HashMap<char, CharInfo>; pub(crate) type CharMap = HashMap<char, CharInfo>;

View File

@ -94,7 +94,7 @@ pub trait WordList: Clone + std::fmt::Debug + Default + Sync + Display {
buf buf
} }
fn get_words_matching(&self, pattern: &str) -> WResult<Vec<WordData>> { fn get_words_matching(&self, pattern: &str) -> WResult<Vec<WordData>> {
let pattern = Regex::new(&pattern).map_err(WordlistError::from)?; let pattern = Regex::new(pattern).map_err(WordlistError::from)?;
let hay = self.raw_wordlist(); let hay = self.raw_wordlist();
let keys = pattern.captures_iter(&hay); let keys = pattern.captures_iter(&hay);
let mut buf = Vec::new(); let mut buf = Vec::new();

View File

@ -21,135 +21,3 @@ fn test_build_builtin_solvers() {
let _naive_solver = let _naive_solver =
AnyBuiltinSolver::Naive(NaiveSolver::build(&wl).expect("could not build naive solver")); AnyBuiltinSolver::Naive(NaiveSolver::build(&wl).expect("could not build naive solver"));
} }
#[test]
fn test_naive_play_predetermined_game() -> anyhow::Result<()> {
let wl = wordlist();
let sl =
AnyBuiltinSolver::Naive(NaiveSolver::build(&wl).expect("could not build naive solver"));
let mut game = Game::build(5, false, 6, &wl, false)?;
game.set_solution(Some(("nines".into(), 0.002))); // The accuracy is made up but shouldn't
// matter
sl.make_a_move(&mut game)?;
assert_eq!(
game.responses().last().unwrap().guess(),
&Word::from("which")
);
sl.make_a_move(&mut game)?;
assert_eq!(
game.responses().last().unwrap().guess(),
&Word::from("first")
);
sl.make_a_move(&mut game)?;
assert_eq!(
game.responses().last().unwrap().guess(),
&Word::from("lives")
);
sl.make_a_move(&mut game)?;
assert_eq!(
game.responses().last().unwrap().guess(),
&Word::from("sides")
);
sl.make_a_move(&mut game)?;
assert_eq!(
game.responses().last().unwrap().guess(),
&Word::from("mines")
);
sl.make_a_move(&mut game)?;
assert_eq!(
game.responses().last().unwrap().guess(),
&Word::from("pines")
);
sl.make_a_move(&mut game)?;
assert_eq!(
game.responses().last().unwrap().guess(),
&Word::from("nines")
);
assert!(game.finished());
assert!(game.won());
Ok(())
}
#[test]
fn test_naive_play_predetermined_game_manually() -> anyhow::Result<()> {
let wl = wordlist();
let sl =
AnyBuiltinSolver::Naive(NaiveSolver::build(&wl).expect("could not build naive solver"));
// we don't insert the solution yet,
// pretend that a user inputs guesses manually
let mut game = Game::build(5, false, 6, &wl, false)?;
let _actual_solution: Option<WordData> = Some(("nines".into(), 0.002));
let mut next_guess: Word;
next_guess = sl.guess_for(&game)?;
assert_eq!(next_guess, Word::from("which"));
game.guess(
&next_guess.clone(),
Some(Evaluation::build(&next_guess, "xxfxx")?),
)?;
next_guess = sl.guess_for(&game)?;
assert_eq!(next_guess, Word::from("first"));
game.guess(
&next_guess.clone(),
Some(Evaluation::build(&next_guess, "xcxfx")?),
)?;
next_guess = sl.guess_for(&game)?;
assert_eq!(next_guess, Word::from("lives"));
game.guess(
&next_guess.clone(),
Some(Evaluation::build(&next_guess, "xcxcc")?),
)?;
next_guess = sl.guess_for(&game)?;
assert_eq!(next_guess, Word::from("sides"));
game.guess(
&next_guess.clone(),
Some(Evaluation::build(&next_guess, "xcxcc")?),
)?;
next_guess = sl.guess_for(&game)?;
assert_eq!(next_guess, Word::from("mines"));
game.guess(
&next_guess.clone(),
Some(Evaluation::build(&next_guess, "xcccc")?),
)?;
next_guess = sl.guess_for(&game)?;
assert_eq!(next_guess, Word::from("pines"));
game.guess(
&next_guess.clone(),
Some(Evaluation::build(&next_guess, "xcccc")?),
)?;
next_guess = sl.guess_for(&game)?;
assert_eq!(next_guess, Word::from("nines"));
game.guess(
&next_guess.clone(),
Some(Evaluation::build(&next_guess, "ccccc")?),
)?;
assert!(game.finished());
assert!(game.won());
Ok(())
}
#[test]
fn test_naive_win_games() -> anyhow::Result<()> {
let wl = wordlist();
let sl =
AnyBuiltinSolver::Naive(NaiveSolver::build(&wl).expect("could not build naive solver"));
let builder = Game::builder(&wl);
{ 0..50 }.into_par_iter().for_each(|_round| {
let mut game = builder.build().expect("could not make game");
sl.play(&mut game).expect("could not play game");
assert!(game.finished());
assert!(game.won());
});
Ok(())
}