Class: GameOfLife::Map
- Inherits:
-
Object
- Object
- GameOfLife::Map
- Includes:
- Enumerable
- Defined in:
- lib/game_of_life/map.rb
Overview
Represents a 2-dimensional collection of Cells
Instance Attribute Summary collapse
-
#cells ⇒ Object
Returns the value of attribute cells.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Instance Method Summary collapse
- #[](x, y) ⇒ Object
- #[]=(x, y, value) ⇒ Object
- #deep_copy ⇒ Object
- #each(&block) ⇒ Object
- #from_s(map) ⇒ Object
-
#initialize(width = 10, height = 10) ⇒ Map
constructor
A new instance of Map.
- #next_generation ⇒ Object
- #next_generation! ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(width = 10, height = 10) ⇒ Map
Returns a new instance of Map.
13 14 15 16 |
# File 'lib/game_of_life/map.rb', line 13 def initialize(width=10, height=10) @width, @height = width, height @cells = Array.new(@width) { Array.new(@height) { GameOfLife::Cell.new } } end |
Instance Attribute Details
#cells ⇒ Object
Returns the value of attribute cells.
11 12 13 |
# File 'lib/game_of_life/map.rb', line 11 def cells @cells end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
10 11 12 |
# File 'lib/game_of_life/map.rb', line 10 def height @height end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
10 11 12 |
# File 'lib/game_of_life/map.rb', line 10 def width @width end |
Instance Method Details
#[](x, y) ⇒ Object
18 19 20 |
# File 'lib/game_of_life/map.rb', line 18 def [](x,y) @cells[y].nil? || y < 0 || x < 0 ? [] : @cells[y][x] end |
#[]=(x, y, value) ⇒ Object
22 23 24 |
# File 'lib/game_of_life/map.rb', line 22 def []=(x,y, value) @cells[y][x] = value end |
#deep_copy ⇒ Object
89 90 91 92 93 |
# File 'lib/game_of_life/map.rb', line 89 def deep_copy copy_map = self.dup copy_map.cells = Marshal.load(Marshal.dump(@cells)) copy_map end |
#each(&block) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/game_of_life/map.rb', line 27 def each &block @cells.each do |x| x.each do |cell| yield cell end end end |
#from_s(map) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/game_of_life/map.rb', line 46 def from_s(map) return if map.empty? new_cells = [] new_row = [] map.each_char do |c| case c when "." new_row << GameOfLife::Cell.new(:dead) when "#" new_row << GameOfLife::Cell.new(:live) when "\n" new_cells << new_row.dup new_row = [] end end @cells = new_cells @width = new_cells[0].length @height = new_cells.size end |
#next_generation ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/game_of_life/map.rb', line 66 def next_generation next_map = deep_copy for y in 0..@height-1 do for x in 0..@width-1 do neighbors = [self[x-1,y-1], self[x,y-1], self[x+1,y-1], self[x-1,y], self[x+1,y], self[x-1,y+1], self[x,y+1], self[x+1,y+1]].flatten.keep_if { |n| !n.nil? } next_map[x,y].update_state neighbors end end next_map end |
#next_generation! ⇒ Object
84 85 86 87 |
# File 'lib/game_of_life/map.rb', line 84 def next_generation! next_map = next_generation @cells = next_map.cells end |
#to_s ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/game_of_life/map.rb', line 35 def to_s result_string = "" @cells.each do |x| x.each do |cell| result_string += cell.to_s end result_string += "\n" end result_string end |