class Rsb::Gol::Grid
Constants
- BOTTOM_LEFT_CORNER
- BOTTOM_RIGHT_CORNER
- HORIZONTAL_BAR
- NEIGHBOR_NAMES
- TOP_LEFT_CORNER
- TOP_RIGHT_CORNER
- VERTICAL_BAR
Attributes
bottom_border[R]
columns[R]
rows[R]
top_border[R]
Public Class Methods
new(rows, columns, random_seed = false)
click to toggle source
# File lib/rsb/gol/grid.rb, line 20 def initialize(rows, columns, random_seed = false) @rows = rows @columns = columns @matrix = Matrix.zero(rows, columns) hbar = HORIZONTAL_BAR * columns @top_border = TOP_RIGHT_CORNER + hbar + TOP_LEFT_CORNER @bottom_border = BOTTOM_RIGHT_CORNER + hbar + BOTTOM_LEFT_CORNER apply_random_seed if random_seed == true end
Public Instance Methods
apply_random_seed()
click to toggle source
# File lib/rsb/gol/grid.rb, line 45 def apply_random_seed @matrix.each_with_index do |state, x, y| spawn(x, y) if Random.rand(2) == 1 end end
cell(x, y)
click to toggle source
# File lib/rsb/gol/grid.rb, line 41 def cell(x, y) @matrix[x, y] end
cell_neighbors() { |state, x, y, neighborhood(x,y)| ... }
click to toggle source
# File lib/rsb/gol/grid.rb, line 84 def cell_neighbors @matrix.each_with_index do |state, x, y| yield(state, x, y, neighborhood(x,y)) end end
kill(x, y)
click to toggle source
# File lib/rsb/gol/grid.rb, line 36 def kill(x, y) fail "coordinates #{x},#{y} are out of bounds" if out_of_bounds?(x,y) @matrix.send(:[]=, x, y, 0) end
neighbor_coordinates(neighbor, x, y)
click to toggle source
# File lib/rsb/gol/grid.rb, line 71 def neighbor_coordinates(neighbor, x, y) case neighbor when :north then [x - 1, y] when :north_east then [x - 1, y - 1] when :north_west then [x - 1, y + 1] when :east then [x, y - 1] when :west then [x, y + 1] when :south then [x + 1, y] when :south_east then [x + 1, y - 1] when :south_west then [x + 1, y + 1] end end
neighborhood(x, y)
click to toggle source
Collects all live cells in a given neighborhood. A neighborhood is defined as all other cells within on cell from the coordinates given. At most a cell can have neighborhood of 8 cells, boundry cells will only have 3 cell neighborhoods
# File lib/rsb/gol/grid.rb, line 59 def neighborhood(x, y) neighbors = [] NEIGHBOR_NAMES.each do |name| nx, ny = neighbor_coordinates(name, x, y) next if out_of_bounds?(nx, ny) state = cell(nx, ny) neighbors << state if state == 1 end neighbors end
out_of_bounds?(x, y)
click to toggle source
# File lib/rsb/gol/grid.rb, line 51 def out_of_bounds?(x, y) x < 0 || y < 0 || x > rows - 1 || y > columns - 1 end
spawn(x, y)
click to toggle source
# File lib/rsb/gol/grid.rb, line 31 def spawn(x, y) fail "coordinates #{x},#{y} are out of bounds" if out_of_bounds?(x,y) @matrix.send(:[]=, x, y, 1) end
visualize(alive = 'o', dead = ' ')
click to toggle source
# File lib/rsb/gol/grid.rb, line 90 def visualize(alive = 'o', dead = ' ') body = '' vbar = VERTICAL_BAR line = '' @matrix.each_with_index do |state, x, y| line << (state == 1 ? alive : dead) if y + 1 == columns body << "#{vbar}#{line}#{vbar}\n" line = '' end end "#{top_border}\n#{body}#{bottom_border}\n" end