Решение на Шеста задача от Димитър Илиев

Обратно към всички решения

Към профила на Димитър Илиев

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 18 успешни тест(а)
  • 0 неуспешни тест(а)

Код

module GameOfLife
class SimpleBoard
include Enumerable
attr_accessor :live_cells
def initialize( *args )
@live_cells = []
args.each do |arg|
# @live_cells << Cell.new( arg[0], arg[1])
add_cell [arg[0], arg[1]]
end
end
def each
if block_given? then
@live_cells.each do |cell|
yield( cell.x, cell.y )
end
end
end
def [](cell_x, cell_y)
@live_cells.any? { |cell| cell.x == cell_x && cell.y == cell_y}
end
def delete coords
@live_cells.delete_if { |cell| cell.same_position? Cell.new(coords[0],coords[1]) }
end
def empty?
@live_cells.empty?
end
def add_cell new_cell
if(!@live_cells.any?{|c| c.same_position? Cell.new(new_cell[0],new_cell[1])} ) then
@live_cells << Cell.new(new_cell[0],new_cell[1])
end
end
end
class Board < SimpleBoard
def is_good? cell
good = ( cell.live == 3 || (cell.live == 4 && cell.gen > 0) )
good
end
def next_generation
new_generation = Board.new
get_potential_gen.each do |cell|
new_generation.add_cell [cell.x,cell.y] if is_good? cell
end
new_generation
end
def get_potential_gen
potential_gen = []
@live_cells.each{|cell| potential_gen << get_near_cell_to( cell ) }
get_unique_cells potential_gen.flatten(1)
end
def get_near_cell_to c
new_c = [] << Cell.new(c.x-1, c.y-1, 0)<< Cell.new(c.x, c.y-1, 0) << Cell.new(c.x+1, c.y-1, 0)
new_c << Cell.new(c.x-1, c.y, 0)<< Cell.new(c.x, c.y, 1, c.gen+1) << Cell.new(c.x+1, c.y, 0)
new_c << Cell.new(c.x-1, c.y+1, 0) << Cell.new(c.x, c.y+1, 0) << Cell.new(c.x+1, c.y+1, 0)
new_c
end
def get_unique_cells cells
unique_cells = []
cells.each do |cell|
unique_cells = filter_unique_cells cell, unique_cells
end
unique_cells
end
def filter_unique_cells cell, unique_cells
if(unique_cells.any?{|uc| uc.same_position? cell}) then
unique_cells.map{|c| c.mutate cell }
else
unique_cells << cell.give_life
end
unique_cells
end
end
class Cell
attr_accessor :x, :y, :live, :gen
def initialize( cell_x, cell_y, live = 1, gen = 0)
@x = cell_x
@y = cell_y
@live = live
@gen = gen
end
def add_lives live = 1
@live = @live + (live > 0 ? live : 1)
end
def same_position? cell_2
cell_2.x == @x && cell_2.y == @y
end
def mutate cell
if same_position? cell then
add_lives( cell.live + 1) if cell.gen == 0
@live = @live + cell.live if cell.gen != 0
@gen = cell.gen if cell.gen > @gen
end
end
def give_life
@live = 1 if @live < 1
self
end
end
end

Лог от изпълнението

..................

Finished in 0.37199 seconds
18 examples, 0 failures

История (1 версия и 0 коментара)

Димитър обнови решението на 20.12.2011 23:12 (преди над 8 години)

+module GameOfLife
+
+ class SimpleBoard
+ include Enumerable
+
+ attr_accessor :live_cells
+
+ def initialize( *args )
+ @live_cells = []
+ args.each do |arg|
+# @live_cells << Cell.new( arg[0], arg[1])
+ add_cell [arg[0], arg[1]]
+ end
+ end
+
+ def each
+ if block_given? then
+ @live_cells.each do |cell|
+ yield( cell.x, cell.y )
+ end
+ end
+ end
+
+ def [](cell_x, cell_y)
+ @live_cells.any? { |cell| cell.x == cell_x && cell.y == cell_y}
+ end
+
+ def delete coords
+ @live_cells.delete_if { |cell| cell.same_position? Cell.new(coords[0],coords[1]) }
+ end
+
+ def empty?
+ @live_cells.empty?
+ end
+
+ def add_cell new_cell
+ if(!@live_cells.any?{|c| c.same_position? Cell.new(new_cell[0],new_cell[1])} ) then
+ @live_cells << Cell.new(new_cell[0],new_cell[1])
+ end
+ end
+ end
+
+ class Board < SimpleBoard
+
+ def is_good? cell
+ good = ( cell.live == 3 || (cell.live == 4 && cell.gen > 0) )
+ good
+ end
+
+ def next_generation
+ new_generation = Board.new
+ get_potential_gen.each do |cell|
+ new_generation.add_cell [cell.x,cell.y] if is_good? cell
+ end
+ new_generation
+ end
+
+ def get_potential_gen
+ potential_gen = []
+ @live_cells.each{|cell| potential_gen << get_near_cell_to( cell ) }
+ get_unique_cells potential_gen.flatten(1)
+ end
+
+ def get_near_cell_to c
+ new_c = [] << Cell.new(c.x-1, c.y-1, 0)<< Cell.new(c.x, c.y-1, 0) << Cell.new(c.x+1, c.y-1, 0)
+ new_c << Cell.new(c.x-1, c.y, 0)<< Cell.new(c.x, c.y, 1, c.gen+1) << Cell.new(c.x+1, c.y, 0)
+ new_c << Cell.new(c.x-1, c.y+1, 0) << Cell.new(c.x, c.y+1, 0) << Cell.new(c.x+1, c.y+1, 0)
+ new_c
+ end
+
+ def get_unique_cells cells
+ unique_cells = []
+ cells.each do |cell|
+ unique_cells = filter_unique_cells cell, unique_cells
+ end
+ unique_cells
+ end
+
+ def filter_unique_cells cell, unique_cells
+ if(unique_cells.any?{|uc| uc.same_position? cell}) then
+ unique_cells.map{|c| c.mutate cell }
+ else
+ unique_cells << cell.give_life
+ end
+ unique_cells
+ end
+
+
+ end
+
+
+ class Cell
+ attr_accessor :x, :y, :live, :gen
+
+ def initialize( cell_x, cell_y, live = 1, gen = 0)
+ @x = cell_x
+ @y = cell_y
+ @live = live
+ @gen = gen
+ end
+
+ def add_lives live = 1
+ @live = @live + (live > 0 ? live : 1)
+ end
+
+ def same_position? cell_2
+ cell_2.x == @x && cell_2.y == @y
+ end
+
+ def mutate cell
+ if same_position? cell then
+ add_lives( cell.live + 1) if cell.gen == 0
+ @live = @live + cell.live if cell.gen != 0
+ @gen = cell.gen if cell.gen > @gen
+ end
+ end
+
+ def give_life
+ @live = 1 if @live < 1
+ self
+ end
+ end
+end