Method: LifeCalculator#neighbors
- Defined in:
- lib/life_game_viewer/model/life_calculator.rb
#neighbors(model, row, col) ⇒ Object
Returns an array of [row, col] tuples corresponding to the cells neighboring the specified cell location. “Neighbor” is defined as a cell with up/down/left/right/diagonal adjacency to the specified cell.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/life_game_viewer/model/life_calculator.rb', line 27 def neighbors(model, row, col) neighbors = [] at_left_edge = col == 0 at_right_edge = col == model.column_count - 1 at_top_edge = row == 0 at_bottom_edge = row == model.row_count - 1 col_to_left = col - 1 col_to_right = col + 1 row_above = row - 1 row_below = row + 1 # In its own row, return the cell to the left and right as possible. unless at_left_edge neighbors << [row, col_to_left] end unless at_right_edge neighbors << [row, col_to_right] end # Process the row above unless at_top_edge unless at_left_edge neighbors << [row_above, col_to_left] end neighbors << [row_above, col] unless at_right_edge neighbors << [row_above, col_to_right] end end # Process the row below unless at_bottom_edge unless at_left_edge neighbors << [row_below, col_to_left] end neighbors << [row_below, col] unless at_right_edge neighbors << [row_below, col_to_right] end end neighbors end |