1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
static OFFSETS: &'static[(i32,i32)] = &[
(-1,-1), (-1, 0), (-1, 1),
( 0,-1), ( 0, 1),
( 1,-1), ( 1, 0), ( 1, 1)
];
pub fn annotate(minefield: &[&str]) -> Vec<String> {
let height = minefield.len() as i32;
(0..height).map(|i| {
let width = minefield[i as usize].len() as i32;
(0..width).map(|j| {
if minefield[i as usize].as_bytes()[j as usize] == b' ' {
let count = OFFSETS
.iter()
.map(|(oi,oj)| (i+oi,j+oj))
.filter(|&(i,j)| i >= 0 && i < height && j >= 0 && j < width)
.filter(|&(i,j)| minefield[i as usize].as_bytes()[j as usize] == b'*')
.count();
if count > 0 { char::from_digit(count as u32,10).unwrap() } else { ' ' }
} else {
'*'
}
}).collect()
}).collect()
}
|