Skip to content

Commit

Permalink
Tidy the solver - inject the dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
CatchemAL committed Nov 30, 2022
1 parent 7cc39af commit 82bac3e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
5 changes: 5 additions & 0 deletions src/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Word>,
pub potential_solns: Vec<Word>,
}

pub fn get_all_words() -> Vec<Word> {
let all_words = get_words(ALL_WORDS);
let solutions = get_words(SOLUTIONS);
Expand Down
18 changes: 14 additions & 4 deletions src/factory.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -16,15 +19,22 @@ pub enum SolverType {
pub fn get_solver(solver: SolverType) -> Box<dyn Solve> {
let reporter = get_reporter(true);

let all_words: Vec<Word> = dictionary::get_all_words();
let potential_solns: Vec<Word> = 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)
}
}
Expand Down
16 changes: 11 additions & 5 deletions src/solver.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,20 +13,26 @@ pub trait Solve {
pub struct Solver<T> {
algorithm: T,
reporter: Box<dyn Reporter>,
dictionary: Dictionary,
}

impl<T: Algorithm> Solver<T> {
pub fn new(algorithm: T, reporter: Box<dyn Reporter>) -> Solver<T> {
pub fn new(algorithm: T, reporter: Box<dyn Reporter>, dictionary: Dictionary) -> Solver<T> {
Solver {
algorithm,
reporter,
dictionary,
}
}

pub fn run(&self, soln: &Word, opening_guess: Word) -> Option<Scoreboard> {
println!("Loading dictionaries...");
let all_words: Vec<Word> = dictionary::get_all_words();
let mut potential_solns: Vec<Word> = dictionary::get_soln_words();
let &Dictionary {
ref all_words,
ref potential_solns,
} = &self.dictionary;

let mut potential_solns: Vec<Word> = potential_solns.to_vec();

println!("Begin solve for solution {soln}...\n");
use std::time::Instant;
Expand All @@ -47,7 +53,7 @@ impl<T: Algorithm> Solver<T> {
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);
Expand Down

0 comments on commit 82bac3e

Please sign in to comment.