loading bar, better ui

This commit is contained in:
WanderingPenwing 2024-11-29 15:57:54 +01:00
parent 29aa1b9e00
commit 3d2f846858

View file

@ -160,6 +160,39 @@ impl Sudoku {
}
}
fn propagate_collapse(&mut self, _debug_display: bool) {
let Some(last_choice) = self.history.last() else {
self.update_possibilities();
return
};
let collapsed_row = last_choice.cell_selected[0];
let collapsed_column = last_choice.cell_selected[1];
let Some(collapsed_value) = self.grid[collapsed_row][collapsed_column].value else {
return
};
let mut collapsed_possibility = HashSet::new();
collapsed_possibility.insert(collapsed_value);
for column_index in 0..self.size {
self.grid[collapsed_row][column_index].remove_possibilities(&collapsed_possibility);
}
for row_index in 0..self.size {
self.grid[row_index][collapsed_column].remove_possibilities(&collapsed_possibility);
}
for row_index in 0..self.square_size {
for column_index in 0..self.square_size {
let row = (collapsed_row/self.square_size)*self.square_size + row_index;
let column = (collapsed_column/self.square_size)*self.square_size + column_index;
self.grid[row][column].remove_possibilities(&collapsed_possibility);
}
}
}
fn collapse(&mut self, debug_display: bool) -> bool {
let mut min_row_index: usize = 0;
let mut min_column_index: usize = 0;
@ -260,6 +293,15 @@ impl Sudoku {
if debug_display {
println!("--------");
}
let mut filled_cells_number: usize = 0;
for row in &self.grid {
for cell in row {
if !cell.value.is_none() {
filled_cells_number += 1;
}
}
}
println!("# started");
@ -267,9 +309,18 @@ impl Sudoku {
let mut counter: usize = 0;
while self.collapse(debug_display) {
self.update_possibilities();
//self.update_possibilities();
self.propagate_collapse(debug_display);
counter +=1;
if !debug_display {
let bar_size = (self.size + self.square_size - 1)*2 + 1;
let progress = self.history.len()*bar_size/(self.size*self.size - filled_cells_number);
let to_do = bar_size - progress;
print!("\r[{}{}]", "#".repeat(progress), "-".repeat(to_do));
}
}
println!();
let elapsed = now.elapsed();
@ -295,6 +346,12 @@ impl Sudoku {
None => {}
}
}
let height = self.size + self.square_size + 2;
print!("\x1b[{}A", height);
for i in 0..height {
println!("{}"," ".repeat((self.size + self.square_size - 1)*2 + 5));
}
print!("\x1b[{}A", height);
}
}
}