From 99a380156e51bf9c85fe1a43cf544f1ed2251e78 Mon Sep 17 00:00:00 2001 From: BakerNet Date: Sat, 28 Sep 2024 01:34:54 -0700 Subject: [PATCH] Correctly handle Failure in analysis --- minesweeper-lib/src/replay/analysis.rs | 32 +++++++++++++++++++++----- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/minesweeper-lib/src/replay/analysis.rs b/minesweeper-lib/src/replay/analysis.rs index c2d90ea..187df34 100644 --- a/minesweeper-lib/src/replay/analysis.rs +++ b/minesweeper-lib/src/replay/analysis.rs @@ -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::>(); + 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