Румен обнови решението на 16.12.2011 19:33 (преди около 13 години)
+module GameOfLife
+class Board
+ include Enumerable
+
+ attr_accessor :board
+
+ def initialize(*args)
+ @board = args
+ end
+
+ def each
+ @board.each { |cell| yield cell[0], cell[1] }
+ end
+
+ def [](x,y)
+ @board.include? [x, y]
+ end
+
+ def next_generation
+ args = (@board - die ) | revive
+ Board.new(*args)
+ end
+
+ private
+ def revive
+ array = []
+ @board.each do |cell|
+ array<< neighbours(cell).select{ |x| !@board.include? x }.select{ |i| nb_count(i) == 3 }
+ end
+ array.flatten(1) | array.flatten(1)
+ end
+
+ def die
+ @board.select { |cell| nb_count(cell) < 2 or nb_count(cell) > 3 }
+ end
+
+ def neighbours(cell)
+ [ [cell[0] - 1, cell[1]], [cell[0] + 1, cell[1]], [cell[0], cell[1] - 1], [cell[0], cell[1]+1],
+ [cell[0] - 1, cell[1] - 1], [cell[0] - 1, cell[1] + 1],
+ [cell[0] + 1, cell[1] + 1], [cell[0] + 1, cell[1] - 1] ]
+ end
+
+ def nb_count(cell)
+ cnt = 0
+ neighbours(cell).each { |i| cnt+=1 if(@board.include? i) }
+ cnt
+ end
+end
+end