Skip to content

Commit

Permalink
Fix integer underflow panic in game of life wrap around example
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchmindtree committed Mar 3, 2020
1 parent 09f7276 commit 8889288
Showing 1 changed file with 7 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,13 @@ impl Gol {
for y in 0..self.rows {
// Add up all the states in a 3x3 surrounding grid
let mut neighbors = 0;
for i in 0..3 {
for j in 0..3 {
neighbors += self.board[(x + (i - 1) + self.columns) % self.columns]
[(y + (j - 1) + self.rows) % self.rows];
for i in 0..3i32 {
for j in 0..3i32 {
let cols = self.columns as i32;
let rows = self.rows as i32;
let board_x = (x as i32 + (i - 1) + cols) % cols;
let board_y = (y as i32 + (j - 1) + rows) % rows;
neighbors += self.board[board_x as usize][board_y as usize];
}
}

Expand Down

0 comments on commit 8889288

Please sign in to comment.