diff options
| author | a14m <[email protected]> | 2020-12-02 14:31:37 +0100 |
|---|---|---|
| committer | a14m <[email protected]> | 2020-12-02 14:36:20 +0100 |
| commit | 3cba16d0016a30c1af32336eac75918214693db7 (patch) | |
| tree | 0fe173dd4acbc2811e556b2e083220d8e03d7526 /CLI/ruby | |
| parent | 8b29970108f96ba0f23a13715e5598923b07b377 (diff) | |
Refactor the cell class to allow easier testing and SRP interface
Diffstat (limited to 'CLI/ruby')
| -rw-r--r-- | CLI/ruby/lib/game_of_life/cell.rb | 42 |
1 files changed, 9 insertions, 33 deletions
diff --git a/CLI/ruby/lib/game_of_life/cell.rb b/CLI/ruby/lib/game_of_life/cell.rb index 8f4a267..3f8fcd4 100644 --- a/CLI/ruby/lib/game_of_life/cell.rb +++ b/CLI/ruby/lib/game_of_life/cell.rb @@ -30,17 +30,16 @@ module GameOfLife end # Evolve a cell in the game of life to the new state in the next generation - # @param universe [Array<GameOfLife::Cell>] the {GameOfLife::Universe#current} state of universe - # @param living_neighbors [Integer] number of living neighbors, defaults to the private implementation of - # {GameOfLife::Cell#living_neighbors} implementation by default + # @param neighbors [Array<GameOfLife::Cell>] that are neighboring the current cell (size of 8) # @return [GameOfLife::Cell] representing the new state in the next generation # # @note There are some optimizations of returning a new object only when the state changes, otherwise returning self # This optimization is done to limit the spacial complexity to O(2*witdht*height) in the worst case scenario # and to O(width * hight) in the best case scenario (when the universe is stale and don't evolve anymore). - def evolve!(universe, living_neighbors: living_neighbors(universe)) - return repopulate! if dead? && living_neighbors == 3 - return survive! if alive? && [2, 3].include?(living_neighbors) + # @see #living_neighbors + def evolve!(neighbors) + return repopulate! if dead? && living_neighbors(neighbors) == 3 + return survive! if alive? && [2, 3].include?(living_neighbors(neighbors)) die! end @@ -57,35 +56,12 @@ module GameOfLife !alive? end - # Return the array of all the neighboring cells using the circular universe array - # @param universe [Array<GameOfLife::Cell>] the {GameOfLife::Universe#current} state of universe - # @return [Array<GameOfLife::Cell>] of all the immediate neighboring cells - # @example given a cell "C" in a universe of cells "." the immediate neighbors would be "x" as in - # the following different examples - # - # ..... | .xxx. | .xCx. | xCx.. | Cx..x | xx..x | - # .xxx. | .xCx. | .xxx. | xxx.. | xx..x | Cx..x | - # .xCx. | .xxx. | ..... | ..... | ..... | xx..x | - # .xxx. | ..... | ..... | ..... | ..... | ..... | - # ..... | ..... | .xxx. | xxx.. | xx..x | ..... | - # - # @smell AbcSize higher than configuration, but needed to return the neighboring cells array - # rubocop:disable Metrics/AbcSize - private def neighbors(universe) - [ - universe[@pos_y - 1][@pos_x - 1], universe[@pos_y - 1][@pos_x], universe[@pos_y - 1][@pos_x + 1], - universe[@pos_y][@pos_x - 1], universe[@pos_y][@pos_x + 1], - universe[@pos_y + 1][@pos_x - 1], universe[@pos_y + 1][@pos_x], universe[@pos_y + 1][@pos_x + 1] - ] - end - # rubocop:enable Metrics/AbcSize - # Count the living neighbor cells relatively to the current cell in the {GameOfLife::Universe} - # @param universe [Array<GameOfLife::Cell>] the {GameOfLife::Universe#current} state of universe + # @param neighbors [Array<GameOfLife::Cell>] that are neighboring the current cell (size of 8) # @return [Integer] the number of living neighbors - # @see #neighbors - private def living_neighbors(universe) - neighbors(universe).select(&:alive?).size + # @see GameOfLive::Universe#neighbors + private def living_neighbors(neighbors) + neighbors.select(&:alive?).size end # Helper method to define the language used in GameOfLife |
