|
@@ -319,6 +319,9 @@ impl AnyBoard {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /// Output board as strings.
|
|
|
|
+ /// - Uses 1-9 (for 9x9), and A-? for others.
|
|
|
|
+ /// - Used by tests to confirm the board is what we think it should be.
|
|
pub fn to_strings(&self) -> Vec<String> {
|
|
pub fn to_strings(&self) -> Vec<String> {
|
|
let mut result = Vec::<String>::new();
|
|
let mut result = Vec::<String>::new();
|
|
let alpha_display = self.width >= 10;
|
|
let alpha_display = self.width >= 10;
|
|
@@ -356,24 +359,22 @@ impl AnyBoard {
|
|
true
|
|
true
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /// Solve by brute force
|
|
|
|
+ /// - Returns up to max # of solutions.
|
|
|
|
+ /// - Returns total solutions found, and vector of boards (up to max).
|
|
|
|
+ /// - Uses brute_force (recursive function) to solve.
|
|
pub fn brute_force_solver(&self, max: u16) -> (u16, Vec<AnyBoard>) {
|
|
pub fn brute_force_solver(&self, max: u16) -> (u16, Vec<AnyBoard>) {
|
|
let mut workset = self.clone();
|
|
let mut workset = self.clone();
|
|
let mut total_solutions: u16 = 0;
|
|
let mut total_solutions: u16 = 0;
|
|
let mut solutions: Vec<AnyBoard> = Vec::new();
|
|
let mut solutions: Vec<AnyBoard> = Vec::new();
|
|
let groups = AnyGroup::new(workset.size);
|
|
let groups = AnyGroup::new(workset.size);
|
|
solutions.reserve(max as usize);
|
|
solutions.reserve(max as usize);
|
|
- workset.brute_force(&mut total_solutions, &mut solutions, &groups);
|
|
|
|
|
|
|
|
- /*
|
|
|
|
- if solutions.len() > 0 {
|
|
|
|
- println!("*** A Solution:");
|
|
|
|
- solutions[0].display();
|
|
|
|
- println!("***");
|
|
|
|
- }
|
|
|
|
- */
|
|
|
|
|
|
+ workset.brute_force(&mut total_solutions, &mut solutions, &groups);
|
|
(total_solutions, solutions)
|
|
(total_solutions, solutions)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /// Recursive brute force solver.
|
|
fn brute_force(
|
|
fn brute_force(
|
|
&mut self,
|
|
&mut self,
|
|
total_solutions: &mut u16,
|
|
total_solutions: &mut u16,
|
|
@@ -525,12 +526,6 @@ impl AnyPossible {
|
|
)
|
|
)
|
|
}
|
|
}
|
|
|
|
|
|
- /*
|
|
|
|
- How can I find the max size of each display possible item?
|
|
|
|
- I'd like to cache the info so I don't re-calc it, and I'd like a pony! :P
|
|
|
|
-
|
|
|
|
- */
|
|
|
|
-
|
|
|
|
/// Format all possible, finding max length.
|
|
/// Format all possible, finding max length.
|
|
/// - Return vec<string> of each item formatted.
|
|
/// - Return vec<string> of each item formatted.
|
|
/// - Return max length.
|
|
/// - Return max length.
|
|
@@ -586,6 +581,7 @@ pub struct AnySolver {
|
|
}
|
|
}
|
|
|
|
|
|
impl AnySolver {
|
|
impl AnySolver {
|
|
|
|
+ /// Construct new solver from given board size. (3,4,5)
|
|
pub fn new(board_size: u8) -> Self {
|
|
pub fn new(board_size: u8) -> Self {
|
|
let mut s = Self {
|
|
let mut s = Self {
|
|
board: AnyBoard::new(board_size),
|
|
board: AnyBoard::new(board_size),
|
|
@@ -596,6 +592,7 @@ impl AnySolver {
|
|
s
|
|
s
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /// Construct new solver from given board.
|
|
pub fn new_from(initial_board: &AnyBoard) -> Self {
|
|
pub fn new_from(initial_board: &AnyBoard) -> Self {
|
|
let mut s = AnySolver {
|
|
let mut s = AnySolver {
|
|
board: initial_board.clone(),
|
|
board: initial_board.clone(),
|
|
@@ -606,6 +603,8 @@ impl AnySolver {
|
|
s
|
|
s
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /// Clear out board and possible.
|
|
|
|
+ /// - group is ok, unless they change the board size.
|
|
pub fn clear(&mut self) {
|
|
pub fn clear(&mut self) {
|
|
let board_size: u8 = self.board.size;
|
|
let board_size: u8 = self.board.size;
|
|
self.board = AnyBoard::new(board_size);
|
|
self.board = AnyBoard::new(board_size);
|
|
@@ -740,12 +739,16 @@ impl AnySolver {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /// Make completed board.
|
|
|
|
+ /// - uses fill_board (recursive)
|
|
pub fn make(&mut self) {
|
|
pub fn make(&mut self) {
|
|
let mut rng = ChaCha20Rng::from_entropy();
|
|
let mut rng = ChaCha20Rng::from_entropy();
|
|
self.reset_possible();
|
|
self.reset_possible();
|
|
self.fill_board(&mut rng);
|
|
self.fill_board(&mut rng);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /// Recursively completely fill the board.
|
|
|
|
+ /// This takes - quite a bit of time - for a 25x25 board.
|
|
fn fill_board(&mut self, rng: &mut ChaCha20Rng) -> bool {
|
|
fn fill_board(&mut self, rng: &mut ChaCha20Rng) -> bool {
|
|
let backup = self.clone();
|
|
let backup = self.clone();
|
|
let max_index = self.board.max_index;
|
|
let max_index = self.board.max_index;
|