diff options
| author | a14m <[email protected]> | 2020-12-02 14:03:20 +0100 |
|---|---|---|
| committer | a14m <[email protected]> | 2020-12-02 14:03:20 +0100 |
| commit | 8b29970108f96ba0f23a13715e5598923b07b377 (patch) | |
| tree | fd6ca8c2a72b4e642bffe7c8e94aa5d321847515 /CLI/ruby | |
| parent | fd2615b57dccff21c2d54011fa251d97a504ccd4 (diff) | |
Implement the basics of GameOfLife universe
Diffstat (limited to 'CLI/ruby')
| -rw-r--r-- | CLI/ruby/lib/game_of_life/universe.rb | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/CLI/ruby/lib/game_of_life/universe.rb b/CLI/ruby/lib/game_of_life/universe.rb index b92386c..ccaf0cd 100644 --- a/CLI/ruby/lib/game_of_life/universe.rb +++ b/CLI/ruby/lib/game_of_life/universe.rb @@ -5,22 +5,31 @@ require "forwardable" module GameOfLife class Universe extend Forwardable - delegate :[] => :@universe + delegate :[] => :@current - attr_accessor :universe - - # Initialize the empty initial universe 2D array + # Initialize the empty initial universe 2D plane (circular arrays) # @param width [Integer] the width of the universe array # @param height [Integer] the height of the universe array def initialize(width:, height:) - @universe = Array.new(height) { Array.new(width) } + @current = GameOfLife::Plane.new(height) { GameOfLife::Plane.new(width) } + @future = GameOfLife::Plane.new(height) { GameOfLife::Plane.new(width) } end # Representation of the game of life universe # @return [String] representing the current state # @see ::GameOfLife::Cell#to_s def to_s - @universe.map(&:join).join("\n") + @current.map(&:join).join("\n") + end + + def evolve! + @current.each do |row| + row.each do |cell| + @future[cell.pos_y][cell.pos_x] = cell.evolve!(@current) + # @future[cell.pos_y][cell.pos_x] = cell.send(:living_neighbors, @current) + end + end + @current = @future end end end |
