Class: Rsb::Gol::Grid

Inherits:
Object
  • Object
show all
Defined in:
lib/rsb/gol/grid.rb

Constant Summary collapse

TOP_RIGHT_CORNER =
"┏"
TOP_LEFT_CORNER =
"┓"
BOTTOM_RIGHT_CORNER =
"┛"
BOTTOM_LEFT_CORNER =
"┛"
HORIZONTAL_BAR =
"━"
VERTICAL_BAR =
"┃"
NEIGHBOR_NAMES =
[
  :north, :north_east, :north_west,
  :east, :west,
  :south, :south_east, :south_west
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rows, columns, random_seed = false) ⇒ Grid

Returns a new instance of Grid.



20
21
22
23
24
25
26
27
28
29
# 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

Instance Attribute Details

#bottom_borderObject (readonly)

Returns the value of attribute bottom_border.



18
19
20
# File 'lib/rsb/gol/grid.rb', line 18

def bottom_border
  @bottom_border
end

#columnsObject (readonly)

Returns the value of attribute columns.



18
19
20
# File 'lib/rsb/gol/grid.rb', line 18

def columns
  @columns
end

#rowsObject (readonly)

Returns the value of attribute rows.



18
19
20
# File 'lib/rsb/gol/grid.rb', line 18

def rows
  @rows
end

#top_borderObject (readonly)

Returns the value of attribute top_border.



18
19
20
# File 'lib/rsb/gol/grid.rb', line 18

def top_border
  @top_border
end

Instance Method Details

#apply_random_seedObject



45
46
47
48
49
# 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) ⇒ Object



41
42
43
# File 'lib/rsb/gol/grid.rb', line 41

def cell(x, y)
  @matrix[x, y]
end

#cell_neighborsObject



84
85
86
87
88
# 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) ⇒ Object



36
37
38
39
# 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) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# 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) ⇒ Object

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



59
60
61
62
63
64
65
66
67
68
69
# 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) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
# 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) ⇒ Object



31
32
33
34
# 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 = ' ') ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
# 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