Skip to content

Commit 1fde93e

Browse files
committed
Solve minesweeper with MineField struct
1 parent 1a35b7d commit 1fde93e

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

minesweeper/src/lib.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,24 @@ impl MineField {
1515
}
1616

1717
pub fn annotate(&mut self) {
18-
self.enumerate_positions(|col, row| {
18+
for (col, row) in self.enumerate_positions() {
1919
if self.is_mine(col, row) == 1 {
20-
return;
20+
continue;
2121
}
2222
let mines_around = self.compute_mines_around(col, row);
23+
if mines_around == 0 {
24+
continue;
25+
}
2326
match (
24-
self.grid.get_mut(col).and_then(|cell_row| cell_row.get_mut(row)),
27+
self.grid
28+
.get_mut(col)
29+
.and_then(|cell_row| cell_row.get_mut(row)),
2530
std::char::from_digit(mines_around, 10),
2631
) {
2732
(Some(cell), Some(digit)) => *cell = digit,
2833
_ => (),
2934
}
30-
});
35+
}
3136
}
3237

3338
pub fn get_annotated_rows(self) -> Vec<String> {
@@ -37,12 +42,14 @@ impl MineField {
3742
})
3843
}
3944

40-
fn enumerate_positions<F: FnMut(usize, usize)>(&self, mut position: F) {
45+
fn enumerate_positions(&self) -> Vec<(usize, usize)> {
46+
let mut positions = vec![];
4147
for column_idx in 0..self.grid.len() {
4248
for row_idx in 0..self.grid[column_idx].len() {
43-
position(column_idx, row_idx);
49+
positions.push((column_idx, row_idx));
4450
}
4551
}
52+
positions
4653
}
4754

4855
fn compute_mines_around(&self, column_idx: usize, row_idx: usize) -> u32 {

minesweeper/tests/minesweeper.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ fn run_test(test_case: &[&str]) {
2121
}
2222

2323
#[test]
24+
#[ignore]
2425
fn no_rows() {
2526
run_test(&[]);
2627
}
@@ -62,7 +63,7 @@ fn horizontal_line() {
6263
}
6364

6465
#[test]
65-
#[ignore]
66+
// #[ignore]
6667
fn horizontal_line_mines_at_edges() {
6768
run_test(&["*1 1*"]);
6869
}

0 commit comments

Comments
 (0)