class GameOfLife::World
Attributes
board[RW]
cells[RW]
Public Class Methods
new(width,height)
click to toggle source
# File lib/game-of-life/world.rb, line 7 def initialize(width,height) @width, @height = width, height setup(false) end
Public Instance Methods
dead_cells()
click to toggle source
# File lib/game-of-life/world.rb, line 21 def dead_cells @cells.select { |cell| cell.dead? } end
get(x,y)
click to toggle source
# File lib/game-of-life/world.rb, line 29 def get(x,y) @board.element(x,y) end
kill(x,y)
click to toggle source
# File lib/game-of-life/world.rb, line 25 def kill(x,y) @board.element(x,y).die! end
live_cells()
click to toggle source
# File lib/game-of-life/world.rb, line 17 def live_cells @cells.select { |cell| cell.alive? } end
live_neighbours_of(x,y)
click to toggle source
# File lib/game-of-life/world.rb, line 37 def live_neighbours_of(x,y) live_neighbours = [] # up-left if x-1 >= 0 and y+1 <= @height-1 neighbour = get(x-1,y+1) live_neighbours << neighbour if neighbour.alive? end # up if y+1 <= @height-1 neighbour = get(x,y+1) live_neighbours << neighbour if neighbour.alive? end # up-right if x+1 <= @width-1 and y+1 <= @height-1 neighbour = get(x+1,y+1) live_neighbours << neighbour if neighbour.alive? end # left if x-1 >= 0 neighbour = get(x-1,y) live_neighbours << neighbour if neighbour.alive? end # right if x+1 <= @width-1 neighbour = get(x+1,y) live_neighbours << neighbour if neighbour.alive? end # down-left if x-1 >= 0 and y-1 >= 0 neighbour = get(x-1,y-1) live_neighbours << neighbour if neighbour.alive? end # down if y-1 >= 0 neighbour = get(x,y-1) live_neighbours << neighbour if neighbour.alive? end # down-right if x+1 <= @width-1 and y-1 >= 0 neighbour = get(x+1,y-1) live_neighbours << neighbour if neighbour.alive? end live_neighbours end
revive(x,y)
click to toggle source
# File lib/game-of-life/world.rb, line 33 def revive(x,y) @board.element(x,y).reborn! end
rotate!()
click to toggle source
# File lib/game-of-life/world.rb, line 91 def rotate! future_alive_cells = [] future_dead_cells = [] @cells.each do |cell| live_neighbours = live_neighbours_of(cell.x,cell.y) future_dead_cells << cell if apply_rule_1(cell,live_neighbours) future_alive_cells << cell if apply_rule_2(cell,live_neighbours) future_dead_cells << cell if apply_rule_3(cell,live_neighbours) future_alive_cells << cell if apply_rule_4(cell,live_neighbours) end update_board(future_alive_cells,future_dead_cells) update_cells end
seed!()
click to toggle source
# File lib/game-of-life/world.rb, line 12 def seed! clean_cells setup(true) end
Private Instance Methods
apply_rule_1(cell,live_neighbours)
click to toggle source
# File lib/game-of-life/world.rb, line 109 def apply_rule_1(cell,live_neighbours) cell.alive? and live_neighbours.count < 2 end
apply_rule_2(cell,live_neighbours)
click to toggle source
# File lib/game-of-life/world.rb, line 113 def apply_rule_2(cell,live_neighbours) cell.alive? and (live_neighbours.count == 2 or live_neighbours.count == 3) end
apply_rule_3(cell,live_neighbours)
click to toggle source
# File lib/game-of-life/world.rb, line 117 def apply_rule_3(cell,live_neighbours) cell.alive? and live_neighbours.count > 3 end
apply_rule_4(cell,live_neighbours)
click to toggle source
# File lib/game-of-life/world.rb, line 121 def apply_rule_4(cell,live_neighbours) cell.dead? and live_neighbours.count == 3 end
clean_cells()
click to toggle source
# File lib/game-of-life/world.rb, line 150 def clean_cells @cells = nil @board = nil end
setup(random)
click to toggle source
# File lib/game-of-life/world.rb, line 125 def setup(random) @board = Matrix.build(@width, @height) { |row,column| if random cell = Cell.new(row,column,[true,false].sample) else cell = Cell.new(row,column) end } update_cells end
update_board(to_live,to_die)
click to toggle source
# File lib/game-of-life/world.rb, line 137 def update_board(to_live,to_die) to_live.each do |cell| revive(cell.x,cell.y) end to_die.each do |cell| kill(cell.x,cell.y) end end
update_cells()
click to toggle source
# File lib/game-of-life/world.rb, line 146 def update_cells @cells = @board.to_a.flatten! end