diff options
Diffstat (limited to 'rust/minesweeper/src/lib.rs')
-rw-r--r-- | rust/minesweeper/src/lib.rs | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/rust/minesweeper/src/lib.rs b/rust/minesweeper/src/lib.rs new file mode 100644 index 0000000..febfdb3 --- /dev/null +++ b/rust/minesweeper/src/lib.rs | |||
@@ -0,0 +1,25 @@ | |||
1 | static OFFSETS: &'static[(i32,i32)] = &[ | ||
2 | (-1,-1), (-1, 0), (-1, 1), | ||
3 | ( 0,-1), ( 0, 1), | ||
4 | ( 1,-1), ( 1, 0), ( 1, 1) | ||
5 | ]; | ||
6 | |||
7 | pub fn annotate(minefield: &[&str]) -> Vec<String> { | ||
8 | let height = minefield.len() as i32; | ||
9 | (0..height).map(|i| { | ||
10 | let width = minefield[i as usize].len() as i32; | ||
11 | (0..width).map(|j| { | ||
12 | if minefield[i as usize].as_bytes()[j as usize] == b' ' { | ||
13 | let count = OFFSETS | ||
14 | .iter() | ||
15 | .map(|(oi,oj)| (i+oi,j+oj)) | ||
16 | .filter(|&(i,j)| i >= 0 && i < height && j >= 0 && j < width) | ||
17 | .filter(|&(i,j)| minefield[i as usize].as_bytes()[j as usize] == b'*') | ||
18 | .count(); | ||
19 | if count > 0 { char::from_digit(count as u32,10).unwrap() } else { ' ' } | ||
20 | } else { | ||
21 | '*' | ||
22 | } | ||
23 | }).collect() | ||
24 | }).collect() | ||
25 | } | ||