From 82bac3ecd7e10f59b194e15a5a958303ad7d637a Mon Sep 17 00:00:00 2001 From: Alex Cross Date: Wed, 30 Nov 2022 20:20:51 +0000 Subject: [PATCH] Tidy the solver - inject the dictionary --- src/dictionary.rs | 5 +++++ src/factory.rs | 18 ++++++++++++++---- src/solver.rs | 16 +++++++++++----- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/dictionary.rs b/src/dictionary.rs index ece6b79..a3c5db5 100644 --- a/src/dictionary.rs +++ b/src/dictionary.rs @@ -7,6 +7,11 @@ use crate::word::Word; const ALL_WORDS: &str = "./dictionaries/dictionary-full-official.json"; const SOLUTIONS: &str = "./dictionaries/dictionary-answers-official.json"; +pub struct Dictionary { + pub all_words: Vec, + pub potential_solns: Vec, +} + pub fn get_all_words() -> Vec { let all_words = get_words(ALL_WORDS); let solutions = get_words(SOLUTIONS); diff --git a/src/factory.rs b/src/factory.rs index 361548f..07c3a7c 100644 --- a/src/factory.rs +++ b/src/factory.rs @@ -1,9 +1,12 @@ +use crate::dictionary; +use crate::dictionary::Dictionary; use crate::guess::EntropyAlgorithm; use crate::guess::MinimaxAlgorithm; use crate::reporting::NullReporter; use crate::reporting::{ConsoleReporter, Reporter}; use crate::solver::Solve; use crate::solver::Solver; +use crate::word::Word; use clap::ValueEnum; @@ -16,15 +19,22 @@ pub enum SolverType { pub fn get_solver(solver: SolverType) -> Box { let reporter = get_reporter(true); + let all_words: Vec = dictionary::get_all_words(); + let potential_solns: Vec = dictionary::get_soln_words(); + let dictionary = Dictionary { + all_words, + potential_solns, + }; + match solver { SolverType::Entropy => { - let guess_factory = EntropyAlgorithm; - let solver = Solver::new(guess_factory, reporter); + let algorithm = EntropyAlgorithm; + let solver = Solver::new(algorithm, reporter, dictionary); Box::new(solver) } SolverType::Minimax => { - let guess_factory = MinimaxAlgorithm; - let solver = Solver::new(guess_factory, reporter); + let algorithm = MinimaxAlgorithm; + let solver = Solver::new(algorithm, reporter, dictionary); Box::new(solver) } } diff --git a/src/solver.rs b/src/solver.rs index b06870e..f85b6ba 100644 --- a/src/solver.rs +++ b/src/solver.rs @@ -1,5 +1,5 @@ use crate::boards::Scoreboard; -use crate::dictionary; +use crate::dictionary::Dictionary; use crate::guess::Algorithm; use crate::reporting::Reporter; use crate::scoring; @@ -13,20 +13,26 @@ pub trait Solve { pub struct Solver { algorithm: T, reporter: Box, + dictionary: Dictionary, } impl Solver { - pub fn new(algorithm: T, reporter: Box) -> Solver { + pub fn new(algorithm: T, reporter: Box, dictionary: Dictionary) -> Solver { Solver { algorithm, reporter, + dictionary, } } pub fn run(&self, soln: &Word, opening_guess: Word) -> Option { println!("Loading dictionaries..."); - let all_words: Vec = dictionary::get_all_words(); - let mut potential_solns: Vec = dictionary::get_soln_words(); + let &Dictionary { + ref all_words, + ref potential_solns, + } = &self.dictionary; + + let mut potential_solns: Vec = potential_solns.to_vec(); println!("Begin solve for solution {soln}...\n"); use std::time::Instant; @@ -47,7 +53,7 @@ impl Solver { return Some(scoreboard); } - guess = self.best_guess(&all_words, &potential_solns).into(); + guess = self.best_guess(all_words, &potential_solns).into(); } self.reporter.report_failure(&scoreboard);