Skip to content

Commit

Permalink
Add counter on playback
Browse files Browse the repository at this point in the history
  • Loading branch information
mkouhia committed May 28, 2024
1 parent 0fecdb4 commit f7ae731
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,12 +510,15 @@ impl Maze {
.ok_or_else(|| anyhow!("No connection from dragon position to hero position"))
}

/// Print solution to console
/// Print solution to console, step by step
///
/// Clear screen, display animation with the same characters as in
/// the original puzzle. Swap characters around the map with each
/// movement.
///
/// Print hero step counter under the maze, and when playback has
/// finished, replace step counter with solution report.
///
/// ## Arguments
/// - `solution`: Solution to the maze.
/// - `step_ms`: Time step for each frame, milliseconds.
Expand All @@ -532,8 +535,9 @@ impl Maze {

let mut squares = self.squares.clone();
print_squares(&squares);
println!("Step 0");

for swap in all_swaps {
for (i, swap) in all_swaps.enumerate() {
thread::sleep(Duration::from_millis(step_ms as u64));

let sq0 = squares[swap[0].y][swap[0].x];
Expand All @@ -554,7 +558,11 @@ impl Maze {
squares[swap[1].y][swap[1].x] = sq0;

print_squares(&squares);
println!("Step {}", i / 2 + 1)
}
// Clear step counter, as the print_report replaces it
print!("\x1B[A\x1B[2K");
solution.print_report()
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ fn main() -> anyhow::Result<()> {
let solution = maze.solve()?;
if args.playback {
maze.playback(&solution, args.frame_length);
} else {
solution.print_report();
}
solution.print_report();
Ok(())
}

0 comments on commit f7ae731

Please sign in to comment.