detect impossible puzzle

This commit is contained in:
WanderingPenwing 2024-12-04 17:10:35 +01:00
parent 855472e42a
commit 2f3555b303
3 changed files with 15 additions and 6 deletions

View file

@ -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);
}
}
}

View file

@ -57,6 +57,10 @@ impl Cell {
}
}
pub fn get_state(&self) -> Option<usize> {
self.state
}
pub fn get_allowed(&self) -> Vec<usize> {
self.blocking_states
.iter()
@ -107,15 +111,18 @@ impl Cell {
}
pub fn remove_allowed(&mut self, blocking_cells: &Vec<BlockingCell>) -> Result<RemoveResult, CellError> {
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();

View file

@ -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)."),
}
}
}