class LifeGame
Public Class Methods
new(width = 60, height = 40)
click to toggle source
# File lib/simple_life_game.rb, line 5 def initialize(width = 60, height = 40) @width = width @height = height @card = Array.new(height) { Array.new(width) { Cell.new } } end
Public Instance Methods
next_gen!()
click to toggle source
# File lib/simple_life_game.rb, line 11 def next_gen! @card.each_with_index do |line, y| line.each_with_index do |cell, x| cell.neighbors = alive_neighbors(y, x) end end @card.each { |line| line.each { |cell| cell.next! } } end
print_card()
click to toggle source
# File lib/simple_life_game.rb, line 20 def print_card @card.each_with_index { |line, num| line.map { |cell| print cell.alive ? "■".light_blue : ' ' }; puts "\n" } end
Private Instance Methods
alive_neighbors(y, x)
click to toggle source
# File lib/simple_life_game.rb, line 25 def alive_neighbors(y, x) up = (y == 0) ? @height - 1 : y - 1 down = (y == @height - 1) ? 0 : y + 1 left = (x == 0) ? @width - 1 : x - 1 right = (x == @width - 1) ? 0 : x + 1 [[y, left], [y, right], # sides [up, right], [up, x], [up, left], # over [down, left], [down, x], [down, right] # under ].inject(0) do |sum, pos| sum += @card[pos[0]][pos[1]].to_i end end