From 2f3555b3031a322845d9b9b082cff4c50be3781d Mon Sep 17 00:00:00 2001 From: WanderingPenwing Date: Wed, 4 Dec 2024 17:10:35 +0100 Subject: [PATCH] detect impossible puzzle --- src/main.rs | 4 +++- src/solver/cell.rs | 15 +++++++++++---- src/solver/mod.rs | 2 +- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index ae15aec..240e9ff 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,6 +47,8 @@ fn main() { if let Err(reason) = solver.solve(args.limit) { println!("{}", reason); - solver.display(solver::ui::DisplayMode::Full); + if args.debug { + solver.display(solver::ui::DisplayMode::Full); + } } } diff --git a/src/solver/cell.rs b/src/solver/cell.rs index 6ff510c..12b6694 100644 --- a/src/solver/cell.rs +++ b/src/solver/cell.rs @@ -57,6 +57,10 @@ impl Cell { } } + pub fn get_state(&self) -> Option { + self.state + } + pub fn get_allowed(&self) -> Vec { self.blocking_states .iter() @@ -107,15 +111,18 @@ impl Cell { } pub fn remove_allowed(&mut self, blocking_cells: &Vec) -> Result { - if !self.state.is_none() { - return Ok(RemoveResult::Filled) - } - for blocking_cell in blocking_cells { if let Some(blocking) = self.blocking_states.get_mut(&blocking_cell.state) { blocking.push(blocking_cell.position); } } + + if let Some(state) = self.state { + if self.blocking_states[&state].len() > 0 { + return Err(CellError::StateNotAllowed) + } + return Ok(RemoveResult::Filled) + } let allowed_states = self.get_allowed(); diff --git a/src/solver/mod.rs b/src/solver/mod.rs index c536b26..6fe7802 100644 --- a/src/solver/mod.rs +++ b/src/solver/mod.rs @@ -15,7 +15,7 @@ impl fmt::Display for WaveError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { WaveError::Contradiction => write!(f, "Error: The puzzle contradicts itself."), - WaveError::NoHistory => write!(f, "Error: Tried to backtrack but the History is empty."), + WaveError::NoHistory => write!(f, "Error: The puzzle is impossible (backtracked to start)."), } } }