Class: RubyLabs::LifeLab::Life

Inherits:
Object
  • Object
show all
Defined in:
lib/lifelab.rb

Overview

Life

The Life class defines a singleton object that has the methods used to initialize and run simulations.

Class Method Summary collapse

Class Method Details

.close_viewObject

Close the RubyLabs Canvas window.



132
133
134
# File 'lib/lifelab.rb', line 132

def Life.close_view
  Canvas.close
end

.dirObject

Print a list of programs in the LifeLab data directory.



150
151
152
153
154
155
156
157
158
# File 'lib/lifelab.rb', line 150

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

.resetObject

Reset the game state.



138
139
140
141
142
143
144
145
146
# File 'lib/lifelab.rb', line 138

def Life.reset
  puts "fix this!"
  # if @@drawing
  #   @@drawing.cells.each do |x|
  #     x.fill = @@drawing.options[:cellColor]
  #   end
  # end
  return true
end

.set_option(key, val) ⇒ Object

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.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/lifelab.rb', line 77

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

.stateObject

Print information about the system.



62
63
64
65
# File 'lib/lifelab.rb', line 62

def Life.state
  puts "state"
  return true
end

.stepObject

Execute one round.



55
56
57
58
# File 'lib/lifelab.rb', line 55

def Life.step
  puts "step"
  return true
end

.view(userOptions = {}) ⇒ Object

Initialize the RubyLabs canvas with a drawing of the Life board. The display will show one rectangle for each cell.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/lifelab.rb', line 105

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