# Решение на Шеста задача от Красимир Кирилов

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

Към профила на Красимир Кирилов

## Резултати

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

## Код

module GameOfLife
class Board
include Enumerable
attr_accessor :board, :living_cell, :next_board
def initialize(*cells)
@board, @living_cell = {}, []
cells.each do |cell|
@board[[cell,cell]] = true
@living_cell.include?(cell)? next : @living_cell << cell
end
end
def [](x, y)
board[[x, y]] ? true : false
end
def each(&block)
living_cell.each(&block)
end
def next_generation()
next_board = NextBoard.new(living_cell)
next_population = next_board.next_living_cells().uniq
Board.new(*next_population)
end
end
class NextBoard
def initialize(living_cell)
@living_cells = living_cell
find_population()
end
def find_population()
continue_living()
reborning()
end
def reborning()
if (alive_neighbors(cell,cell) == 3)
@next_living_cells << cell
end
end
end
def continue_living()
@living_cells.each do |cell|
if ((alive_neighbors(cell, cell) == 2) || (alive_neighbors(cell, cell) == 3))
@next_living_cells << cell
end
end
end
def neighbors(x, y)
[[x-1, y], [x+1, y], [x, y+1], [x, y-1], [x+1, y+1], [x+1, y-1], [x-1, y+1], [x-1, y-1]]
end
neighb = neighbors(x, y)
end
def alive_neighbors(x, y)
neighb = neighbors(x, y)
(neighb & living_cells).count
end
living_cells.each do |cell|
end
end
end
end
end

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

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

Finished in 0.39378 seconds
18 examples, 0 failures

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

### Красимир обнови решението на 16.12.2011 00:35 (преди над 8 години)

+module GameOfLife
+
+ class Board
+ include Enumerable
+
+ attr_accessor :board, :living_cell, :next_board
+
+ def initialize(*cells)
+ @board, @living_cell = {}, []
+ cells.each do |cell|
+ @board[[cell,cell]] = true
+ @living_cell.include?(cell)? next : @living_cell << cell
+ end
+ end
+
+ def [](x, y)
+ board[[x, y]] ? true : false
+ end
+
+ def each(&block)
+ living_cell.each(&block)
+ end
+
+ def next_generation()
+ next_board = NextBoard.new(living_cell)
+ next_population = next_board.next_living_cells().uniq
+ Board.new(*next_population)
+ end
+
+ end
+
+ class NextBoard
+
+
+ def initialize(living_cell)
+ @living_cells = living_cell
+ @next_living_cells, @dead_cells = [], []
+ find_population()
+ end
+
+ def find_population()
+ continue_living()
+ reborning()
+ end
+
+ def reborning()
+ if (alive_neighbors(cell,cell) == 3)
+ @next_living_cells << cell
+ end
+ end
+ end
+
+ def continue_living()
+ @living_cells.each do |cell|
+ if ((alive_neighbors(cell, cell) == 2) || (alive_neighbors(cell, cell) == 3))
+ @next_living_cells << cell
+ end
+ end
+ end
+
+ def neighbors(x, y)
+ [[x-1, y], [x+1, y], [x, y+1], [x, y-1], [x+1, y+1], [x+1, y-1], [x-1, y+1], [x-1, y-1]]
+ end
+
+ neighb = neighbors(x, y)
+ dead_neighbors = neighb - living_cells
+ end
+
+ def alive_neighbors(x, y)
+ neighb = neighbors(x, y)
+ (neighb & living_cells).count
+ end
+