module RubyLabs =begin rdoc == LifeLab Conway's Game of Life. #-- TODO all of it... #++ =end module LifeLab # -- Initializations -- These are "global" vars in the outer LifeLab scope that are # accessible to all the classes and modules defined inside LifeLab @@lifeDirectory = File.join(File.dirname(__FILE__), '..', 'data', 'life') @@rule = "B3/S23" @@params = { :rounds => 1000, :rows => 20, :cols => 20, :pause => 0.01, } LifeView = Struct.new(:rows, :cols, :options) @@viewOptions = { :cellSize => 2, :cellRows => 100, :cellCols => 200, :padding => 20, :traceSize => 10, :cellColor => '#DDDDDD', } @@drawing = nil =begin rdoc == Life The Life class defines a singleton object that has the methods used to initialize and run simulations. =end class Life # Execute one round. def Life.step puts "step" return true end # Print information about the system. def Life.state puts "state" return true end # Set the value of one of the run-time options. The options, and their default values, are: # [TBD] # # Example: # >>!blue Life.set_option(:name, value) # => value # # Note: Call Life.state to see the current settings for the options. def Life.set_option(key, val) case key # the branches of the case statement are places to validate parameter # values -- see examples from MARSLab below when :todo puts "options TBD" return nil # when :memSize # if val.class != Fixnum || val < 1024 || val > 16536 # puts ":memSize must be an integer between 1024 and 16536" # return nil # end # when :maxRounds, :buffer # if val.class != Fixnum # puts ":#{key} must be an integer" # return nil # end else puts "Unknown option: #{key}" puts "Call Life.state to see a list of options and their current settings." return nil end @@params[key] = val end # Initialize the RubyLabs canvas with a drawing of the Life board. The # display will show one rectangle for each cell. def Life.view(userOptions = {} ) options = @@viewOptions.merge(userOptions) cellsize = options[:cellSize] padding = options[:padding] width = options[:cellCols] * cellsize + 2*padding height = options[:cellRows] * cellsize + 2*padding Canvas.init(width, height, "LifeLab") cells = [] for i in 0...options[:cellRows] for j in 0...options[:cellCols] x = j * cellsize + padding y = i * cellsize + padding cells << Canvas::Rectangle.new( x, y, x+cellsize, y+cellsize, :outline => "#888888", :fill => options[:cellColor] ) end end @@drawing = LifeView.new(cells, options) return true end # Close the RubyLabs Canvas window. def Life.close_view Canvas.close end # Reset the game state. def Life.reset puts "fix this!" # if @@drawing # @@drawing.cells.each do |x| # x.fill = @@drawing.options[:cellColor] # end # end return true end # Print a list of programs in the LifeLab data directory. def Life.dir # puts "Redcode programs in #{@@marsDirectory}:" # Dir.open(@@marsDirectory).each do |file| # next if file[0] == ?. # file.slice!(/\.txt/) # puts " " + file # end return nil end private # Drawing hook, called from MARS.step when the canvas is active. # def MARS.updateCells(pc) # id = pc.id # a = pc.history[pc.current[:thread]] # d = @@drawing.palettes[id].length - a.length # a.each_with_index do |x, i| # @@drawing.cells[x].fill = @@drawing.palettes[id][i+d] # end # end end # class Life # Initialize any class variables here... # @@entries = Array.new end # LifeLab end # RubyLabs