Skip to content

Commit 6e2afc4

Browse files
Simplify arithmetic of calculating neighboring fields
1 parent 0f66abb commit 6e2afc4

File tree

1 file changed

+4
-9
lines changed
  • minesweeper/src/minesweeper_logic

1 file changed

+4
-9
lines changed

minesweeper/src/minesweeper_logic/table.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -290,18 +290,13 @@ fn get_neighbor_fields(
290290
row: SizeType,
291291
col: SizeType,
292292
) -> HashSet<(SizeType, SizeType)> {
293-
fn add(u: SizeType, i: i8) -> Option<SizeType> {
294-
if i.is_negative() {
295-
u.checked_sub(i.abs() as u8 as SizeType)
296-
} else {
297-
u.checked_add(i as SizeType)
298-
}
299-
};
300-
301293
let mut neighbors = HashSet::new();
302294

303295
for offset in &NEIGHBOR_OFFSETS {
304-
match (add(row, offset.0), add(col, offset.1)) {
296+
match (
297+
row.checked_add(offset.0 as i64),
298+
col.checked_add(offset.1 as i64),
299+
) {
305300
(Some(r), Some(c)) if r >= 0 && r < height && c >= 0 && c < width => {
306301
neighbors.insert((r, c));
307302
}

0 commit comments

Comments
 (0)