Skip to content

Commit

Permalink
Correctly handle Failure in analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
BakerNet committed Sep 28, 2024
1 parent d424729 commit 99a3801
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions minesweeper-lib/src/replay/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,32 @@ impl MinesweeperAnalysis {
current_log_entry.push((bp, LogEntry { from, to: None }));
}
let mut contents = rc.contents;
// reduce cell numbers by the number of mines
analysis_board
.neighbors(&bp)
.iter()
.filter(|&np| is_mine(np, &analysis_board))
.for_each(|_| contents = contents.decrement());
match contents {
Cell::Empty(_) => {
// reduce newly revealed cell by the number of known mines
analysis_board
.neighbors(&bp)
.iter()
.filter(|&np| is_mine(np, &analysis_board))
.for_each(|_| contents = contents.decrement());
}
Cell::Mine => {
if !is_mine(&bp, &analysis_board) {
// we now know this is a mine so we reduce existing revealed cells
let empty_neighbors = analysis_board
.neighbors(&bp)
.into_iter()
.filter_map(|np| match analysis_board[np] {
AnalysisCell::Revealed(c) => Some((np, c)),
_ => None,
})
.collect::<ArrayVec<[(BoardPoint, Cell); 8]>>();
empty_neighbors.iter().for_each(|(np, c)| {
analysis_board[np] = AnalysisCell::Revealed(c.decrement());
});
}
}
}
analysis_board[bp] = AnalysisCell::Revealed(contents);
});
let mut points_to_analyze = new_revealed
Expand Down

0 comments on commit 99a3801

Please sign in to comment.