From 8b29970108f96ba0f23a13715e5598923b07b377 Mon Sep 17 00:00:00 2001 From: a14m Date: Wed, 2 Dec 2020 14:03:20 +0100 Subject: Implement the basics of GameOfLife universe --- CLI/ruby/lib/game_of_life/universe.rb | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'CLI/ruby') 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 -- cgit v1.2.3