diff --git a/src/solver/cell.rs b/src/solver/cell.rs index 9977e8e..6ff510c 100644 --- a/src/solver/cell.rs +++ b/src/solver/cell.rs @@ -57,10 +57,6 @@ impl Cell { } } - pub fn get_state(&self) -> Option { - self.state - } - pub fn get_allowed(&self) -> Vec { self.blocking_states .iter() diff --git a/src/solver/mod.rs b/src/solver/mod.rs index d281525..6a236a0 100644 --- a/src/solver/mod.rs +++ b/src/solver/mod.rs @@ -72,23 +72,15 @@ impl Solver { pub fn solve(&mut self, solver_limit: Option) -> Result<(), WaveError> { let now = Instant::now(); self.display(ui::DisplayMode::Full); - let mut number_cell_init: usize = 0; - for row in &self.grid { - for cell in row { - if !cell.is_none() { - number_cell_init += 1; - } - } - } + let mut propagation_counter: usize = 0; let mut collapse_counter: usize = 0; println!("# started"); self.debug("--------"); - self.update_possibilities()?; - while number_cell_init + self.history.len() < self.size * self.size { + while self.history.len() < self.size * self.size { self.debug(&format!("\n## while, h={}/{}", self.last_move_index, self.history.len())); - while self.last_move_index < self.history.len() && number_cell_init + self.history.len() < self.size * self.size { + while self.last_move_index < self.history.len() && self.history.len() < self.size * self.size { let mut backtrack = 0; match self.propagate_collapse() { Ok(_) => {}, @@ -119,7 +111,7 @@ impl Solver { collapse_counter += 1; if !self.debug_display && !self.grid_display { - self.progress_bar(number_cell_init); + self.progress_bar(); } if let Some(limit) = solver_limit { if collapse_counter >= limit { @@ -182,6 +174,10 @@ impl Solver { fn backtrack(&mut self) -> Result<(), WaveError> { let mut fork: Option = None; while let Some(step) = self.history.pop() { + if self.last_move_index == 0 { + self.debug("there is no last move"); + return Err(WaveError::NoHistory) + } self.last_move_index -= 1; self.grid[step.position.0][step.position.1].reset_state(); diff --git a/src/solver/sudoku.rs b/src/solver/sudoku.rs index 9d00298..1ab579d 100644 --- a/src/solver/sudoku.rs +++ b/src/solver/sudoku.rs @@ -5,39 +5,6 @@ use super::WaveError; use super::cell; impl Solver { - pub fn update_possibilities(&mut self) -> Result<(), WaveError> { - let mut row_used_states: Vec> = vec![vec![]; self.size]; - let mut column_used_states: Vec> = vec![vec![]; self.size]; - let mut square_used_states: Vec>> = vec![vec![vec![]; self.square_size]; self.square_size]; - - for row_index in 0..self.size { - for column_index in 0..self.size { - let Some(state) = self.grid[row_index][column_index].get_state() else { - continue - }; - let blocking_cell = cell::BlockingCell { - state, - position: (row_index, column_index), - }; - row_used_states[row_index].push(blocking_cell.clone()); - column_used_states[column_index].push(blocking_cell.clone()); - square_used_states[row_index/self.square_size][column_index/self.square_size].push(blocking_cell); - } - } - - for row_index in 0..self.size { - for column_index in 0..self.size { - self.remove_allowed((row_index, column_index), &row_used_states[row_index] - .iter() - .chain(&column_used_states[column_index]) - .chain(&square_used_states[row_index / self.square_size][column_index / self.square_size]) - .cloned() - .collect())?; - } - } - Ok(()) - } - pub fn propagate_collapse(&mut self) -> Result<(), WaveError> { if self.last_move_index >= self.history.len() { self.debug(&format!("x nothing to propagate")); diff --git a/src/solver/ui.rs b/src/solver/ui.rs index 25b7915..00bf550 100644 --- a/src/solver/ui.rs +++ b/src/solver/ui.rs @@ -2,6 +2,7 @@ use text_io::read; use super::Solver; use super::cell; +use super::Step; const CHAR_SET: &str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; @@ -80,6 +81,11 @@ impl Solver { Some(value) => { if value <= self.size { self.grid[row_index][column_index].collapse(&cell::CollapseOption::Set(value))?; + self.history.push(Step { + position: (row_index, column_index), + state_set: value, + num_allowed_states: 1, + }); } } None => {} @@ -95,9 +101,9 @@ impl Solver { Ok(()) } - pub fn progress_bar(&self, number_cell_init: usize) { + pub fn progress_bar(&self) { let bar_size = (self.size + self.square_size - 1)*2 + 1; - let progress = self.history.len()*bar_size/(self.size*self.size - number_cell_init); + let progress = self.history.len()*bar_size/(self.size*self.size); let to_do = bar_size - progress; print!("\r[{}{}]", "#".repeat(progress), "-".repeat(to_do)); }