Решение на Шеста задача от Михаил Петков

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

Към профила на Михаил Петков

Резултати

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

Код

module GameOfLife
class Board
attr_accessor :cells, :alive_cells, :dead_cells
include Enumerable
def each(&block)
@cells.each(&block)
end
def [](x,y)
@cells.include?([x,y]) ? true : false
end
def initialize(*alive)
@cells = alive.uniq
@alive_cells = []
end
def next_generation()
add_dead_cells()
conditions()
board = Board.new(*@alive_cells)
end
def add_dead_cells()
@dead_cells = []
@cells.each { |cell| add_deads(cell[0], cell[1], cell) }
end
def add_deads(x,y,cell)
n = [[x-1,y-1],[x-1,y],[x-1,y+1],[x,y+1],[x+1,y+1],[x+1,y],[x+1,y-1],[x,y-1]]
n = n - @cells
@dead_cells = @dead_cells | n
end
def conditions()
@cells.each { |cell| current_cell(cell[0], cell[1], cell) }
@dead_cells.each { |dead_cell| current_dead_cell(dead_cell[0], dead_cell[1], dead_cell) }
end
def current_cell(x,y,cell)
n = [[x-1,y-1],[x-1,y],[x-1,y+1],[x,y+1],[x+1,y+1],[x+1,y],[x+1,y-1],[x,y-1]]
@alive_cells << cell if (n & @cells).length() == 2 || (n & @cells).length() == 3
@dead_cells << cell if (n & @cells).length() < 2 || (n & @cells).length() > 3
end
def current_dead_cell(x,y,cell)
n = [[x-1,y-1],[x-1,y],[x-1,y+1],[x,y+1],[x+1,y+1],[x+1,y],[x+1,y-1],[x,y-1]]
@alive_cells << cell if (n & @cells).length() == 3
end
def count()
@cells.length()
end
end
end

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

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

Finished in 0.40739 seconds
18 examples, 0 failures

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

Михаил обнови решението на 16.12.2011 00:15 (преди над 12 години)

+module GameOfLife
+ class Board
+ attr_accessor :cells, :alive_cells, :dead_cells
+ include Enumerable
+
+ def each(&block)
+ @cells.each(&block)
+ end
+
+ def [](x,y)
+ @cells.include?([x,y]) ? true : false
+ end
+
+ def initialize(*alive)
+ @cells = alive.uniq
+ @alive_cells = []
+ end
+
+ def next_generation()
+ add_dead_cells()
+ conditions()
+ board = Board.new(*@alive_cells)
+ end
+
+ def add_dead_cells()
+ @dead_cells = []
+ @cells.each { |cell| add_deads(cell[0], cell[1], cell) }
+ end
+
+ def add_deads(x,y,cell)
+ n = [[x-1,y-1],[x-1,y],[x-1,y+1],[x,y+1],[x+1,y+1],[x+1,y],[x+1,y-1],[x,y-1]]
+ n = n - @cells
+ @dead_cells = @dead_cells | n
+ end
+
+ def conditions()
+ @cells.each { |cell| current_cell(cell[0], cell[1], cell) }
+ @dead_cells.each { |dead_cell| current_dead_cell(dead_cell[0], dead_cell[1], dead_cell) }
+ end
+
+ def current_cell(x,y,cell)
+ n = [[x-1,y-1],[x-1,y],[x-1,y+1],[x,y+1],[x+1,y+1],[x+1,y],[x+1,y-1],[x,y-1]]
+ @alive_cells << cell if (n & @cells).length() == 2 || (n & @cells).length() == 3
+ @dead_cells << cell if (n & @cells).length() < 2 || (n & @cells).length() > 3
+ end
+
+ def current_dead_cell(x,y,cell)
+ n = [[x-1,y-1],[x-1,y],[x-1,y+1],[x,y+1],[x+1,y+1],[x+1,y],[x+1,y-1],[x,y-1]]
+ @alive_cells << cell if (n & @cells).length() == 3
+ end
+
+ def count()
+ @cells.length()
+ end
+ end
+end