Class: Life::Board
- Inherits:
-
Object
- Object
- Life::Board
- Defined in:
- lib/life/board.rb
Instance Attribute Summary collapse
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Class Method Summary collapse
Instance Method Summary collapse
- #[](x, y) ⇒ Object
- #[]=(x, y, value) ⇒ Object
- #clone ⇒ Object
- #count_neighbors(x, y) ⇒ Object
- #each ⇒ Object
- #each_neighbor(x, y) ⇒ Object
-
#initialize(width, height) ⇒ Board
constructor
A new instance of Board.
- #tick ⇒ Object
- #to_a ⇒ Object
- #update_from_array(cells) ⇒ Object
Constructor Details
#initialize(width, height) ⇒ Board
Returns a new instance of Board.
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/life/board.rb', line 5 def initialize(width, height) if width < 3 || height < 3 raise ArgumentError.new("Life Board dimensions must be bigger than 3") end @width = width @height = height @cells = Array.new(height) do Array.new(width) { false } end end |
Instance Attribute Details
#height ⇒ Object (readonly)
Returns the value of attribute height.
3 4 5 |
# File 'lib/life/board.rb', line 3 def height @height end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
3 4 5 |
# File 'lib/life/board.rb', line 3 def width @width end |
Class Method Details
.from_array(cells) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/life/board.rb', line 35 def self.from_array(cells) width = cells[0].size height = cells.size board = new(width, height) board.update_from_array(cells) board end |
.from_text(text) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/life/board.rb', line 43 def self.from_text(text) lines = text.split("\n") width = lines[0].size height = lines.size board = Life::Board.new(width, height) lines.each_with_index do |line, y| line.each_char.with_index do |char, x| board[x, y] = (char == "#") end end board end |
Instance Method Details
#[](x, y) ⇒ Object
17 18 19 20 |
# File 'lib/life/board.rb', line 17 def [](x, y) wx, wy = wrapped_coords(x, y) @cells[wy][wx] end |
#[]=(x, y, value) ⇒ Object
22 23 24 25 |
# File 'lib/life/board.rb', line 22 def []=(x, y, value) wx, wy = wrapped_coords(x, y) @cells[wy][wx] = value end |
#clone ⇒ Object
69 70 71 |
# File 'lib/life/board.rb', line 69 def clone self.class.from_array(@cells) end |
#count_neighbors(x, y) ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/life/board.rb', line 59 def count_neighbors(x, y) count = 0 each_neighbor(x, y) do |nx, ny, alive| count += 1 if alive end count end |
#each ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/life/board.rb', line 73 def each (0...height).each do |y| (0...width).each do |x| yield x, y, self[x, y] end end end |
#each_neighbor(x, y) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/life/board.rb', line 81 def each_neighbor(x, y) (-1..1).each do |dy| (-1..1).each do |dx| next if dx == 0 && dy == 0 nx = x + dx ny = y + dy yield nx, ny, self[nx, ny] end end end |
#tick ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/life/board.rb', line 93 def tick old_board = clone() each do |x, y, alive| neighbor_count = old_board.count_neighbors(x, y) if alive self[x, y] = (2..3).include?(neighbor_count) else self[x, y] = (neighbor_count == 3) end end end |
#to_a ⇒ Object
27 28 29 |
# File 'lib/life/board.rb', line 27 def to_a clone_2d_array(@cells) end |
#update_from_array(cells) ⇒ Object
31 32 33 |
# File 'lib/life/board.rb', line 31 def update_from_array(cells) @cells = clone_2d_array(cells) end |