69 lines
1.6 KiB
Rust
69 lines
1.6 KiB
Rust
use std::io;
|
|
|
|
use wasm_bindgen::prelude::*;
|
|
|
|
use crate::{
|
|
pattern::{CharStatus, Pattern},
|
|
solver,
|
|
word_list::WordList,
|
|
};
|
|
|
|
static DEFAULT_VALID_WORDS: &str = include_str!("../../valid-words.txt");
|
|
static DEFAULT_ANSWERS: &str = include_str!("../../answers.txt");
|
|
|
|
#[wasm_bindgen]
|
|
struct Solver(solver::Solver);
|
|
|
|
#[wasm_bindgen]
|
|
pub enum Color {
|
|
GREY,
|
|
YELLOW,
|
|
GREEN,
|
|
}
|
|
|
|
#[allow(dead_code)]
|
|
#[wasm_bindgen]
|
|
impl Solver {
|
|
pub fn new() -> Result<Solver, String> {
|
|
let valid_words =
|
|
WordList::try_from(io::BufReader::new(io::Cursor::new(DEFAULT_VALID_WORDS)))?;
|
|
let answers = WordList::try_from(io::BufReader::new(io::Cursor::new(DEFAULT_ANSWERS)))?;
|
|
|
|
Ok(Solver(solver::Solver::new(valid_words, answers)))
|
|
}
|
|
|
|
pub fn guess(&mut self, word: &str, colors: Vec<Color>) -> Result<(), String> {
|
|
let pat = Pattern::try_new(
|
|
word,
|
|
colors
|
|
.iter()
|
|
.map(|c| match c {
|
|
Color::GREY => CharStatus::GREY,
|
|
Color::YELLOW => CharStatus::YELLOW,
|
|
Color::GREEN => CharStatus::GREEN,
|
|
})
|
|
.collect(),
|
|
)?;
|
|
|
|
self.0.apply_guess(pat)?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub fn guess_infos(&self) -> Vec<f64> {
|
|
self.0
|
|
.guesses()
|
|
.iter()
|
|
.map(|g| g.information_gained)
|
|
.collect()
|
|
}
|
|
|
|
pub fn solution_space(&self) -> u64 {
|
|
self.0.stats().0
|
|
}
|
|
|
|
pub fn solution_space_uncertainty(&self) -> f64 {
|
|
self.0.stats().1
|
|
}
|
|
}
|