Skip to content

Commit

Permalink
Replace double for in 'neighbour_positions' for NEIGHBOUR_OFFSETS
Browse files Browse the repository at this point in the history
  • Loading branch information
iñaki llorens authored and iñaki llorens committed Aug 27, 2024
1 parent 206f2b7 commit 149158f
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions src/cell.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
const NEIGHBOUR_OFFSETS: [(i32, i32); 8] = [
(-1, -1),
(-1, 0),
(-1, 1),
(0, -1),
(0, 1),
(1, -1),
(1, 0),
(1, 1),
];

/// Represents a cell of the Game of Life.
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
pub struct Cell {
Expand All @@ -13,22 +24,13 @@ impl Cell {

// Returns the positions of the 8 neighbours of a cell.
pub fn neighbour_positions(&self) -> Vec<Cell> {
let mut positions = Vec::with_capacity(8);

for dx in -1..=1 {
for dy in -1..=1 {
if dx == 0 && dy == 0 {
continue;
}

positions.push(Cell {
x: self.x + dx,
y: self.y + dy,
});
}
}

positions
NEIGHBOUR_OFFSETS
.iter()
.map(|&(dx, dy)| Cell {
x: self.x + dx,
y: self.y + dy,
})
.collect()
}

/// Returns the position of the cell as a tuple.
Expand Down

0 comments on commit 149158f

Please sign in to comment.