boggle_maker/
total_boggle_score_calculator.rs1use std::collections::HashSet;
2use crate::boggle_dfs::{WordVisitor,BoggleDfsContext,BoggleDfs,get_word_score};
3use word_trie::trie::Trie;
4
5struct TotalScoreWordVisitor(HashSet<String>,u32);
6
7impl TotalScoreWordVisitor {
8 pub fn score(&self) -> u32 {
9 self.1
10 }
11}
12
13impl WordVisitor for TotalScoreWordVisitor {
14 fn visit(&mut self, word: &str, _: &Vec<u16>){
15 if !self.0.contains(word) {
16 self.1 += get_word_score(word);
17 self.0.insert(word.to_string());
18 }
19 }
20}
21
22#[derive(Debug, Clone)]
23pub struct TotalBoggleScoreCalculator<'a> (BoggleDfsContext<'a>);
24
25impl <'a> TotalBoggleScoreCalculator<'a> {
26 pub fn new(dictionary : &'a Trie, width:usize, length:usize) -> Self{
27 Self(BoggleDfsContext::new(dictionary, width, length))
28 }
29
30 pub fn score(&mut self, board: & Vec<char>) -> u32{
31 if board.len() != self.0.count() {
32 panic!("The board size must be fit to the length:{0} and width:{1}", self.0.length(), self.0.width());
33 }
34
35 let mut visitor = TotalScoreWordVisitor(HashSet::new(),0);
36 let mut session = BoggleDfs::new(&self.0 , board);
37 session.with_visitor(&mut visitor).search();
38
39 visitor.score()
40 }
41}