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

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

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

Резултати

  • 5 точки от тестове
  • 0 бонус точки
  • 5 точки общо
  • 16 успешни тест(а)
  • 2 неуспешни тест(а)

Код

module GameOfLife
class Board
include Enumerable
def initialize(*alive_coordiantes)
flatten_array = alive_coordiantes.flatten
@board = []
(0..(flatten_array.length-2)).step(2) do |i|
@board.push( [flatten_array[i],flatten_array[i+1]] )
end
end
def [](x,y)
@board.index([x,y]) == nil ? false : true
end
def each(&block)
@board.each(&block)
end
def alive_nbours(x, y)
neigbours = [[x-1,y-1], [x,y-1], [x+1,y-1], [x-1,y], [x+1,y], [x-1,y+1], [x, y+1], [x+1,y+1]]
neigbours.keep_if { |x,y| self[x,y] }
neigbours.count
end
# Returns every cell within the borders of our simulation.
# Every dead cell that is too far alway from the living cells
# on the infinite board can't change it's state.
def get_borders
x_array = @board.map { |x,y| x }
y_array = @board.map { |x,y| y }
borders = [x_array.min-1, x_array.max+1, y_array.min-1, y_array.max+1]
(borders[0] .. borders[1]).to_a.product (borders[2] .. borders[3]).to_a
end
def next_generation
next_gen = @board.select{ |x,y| (cnt = alive_nbours(x, y)) == 2 || cnt == 3 }
for x,y in get_borders do
if alive_nbours(x,y) == 3 && self[x,y] == false then next_gen.push [x,y] end
end
return Board.new(next_gen)
end
end
end

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

......F...F.......

Failures:

  1) GameOfLife::Board GameOfLife::Board enumeration live cells count returns the number of live cells on a board
     Failure/Error: new_board([0, 0], [1, 1], [0, 0]).count.should eq 2
       
       expected: 2
            got: 3
       
       (compared using ==)
     # /tmp/d20111221-3114-1sszf83/spec.rb:46:in `block (4 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

  2) GameOfLife::Board evolution returns a new board
     Failure/Error: next_gen = board.next_generation
     NoMethodError:
       undefined method `-' for nil:NilClass
     # /tmp/d20111221-3114-1sszf83/solution.rb:35:in `get_borders'
     # /tmp/d20111221-3114-1sszf83/solution.rb:42:in `next_generation'
     # /tmp/d20111221-3114-1sszf83/spec.rb:74:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (3 levels) in <top (required)>'
     # ./lib/homework/run_with_timeout.rb:5:in `block (2 levels) in <top (required)>'

Finished in 0.39174 seconds
18 examples, 2 failures

Failed examples:

rspec /tmp/d20111221-3114-1sszf83/spec.rb:43 # GameOfLife::Board GameOfLife::Board enumeration live cells count returns the number of live cells on a board
rspec /tmp/d20111221-3114-1sszf83/spec.rb:72 # GameOfLife::Board evolution returns a new board

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

Камен обнови решението на 20.12.2011 17:02 (преди над 8 години)

+
+module GameOfLife
+
+ class Board
+ include Enumerable
+
+ def initialize(*alive_coordiantes)
+ flatten_array = alive_coordiantes.flatten
+ @board = []
+ (0..(flatten_array.length-2)).step(2) do |i|
+ @board.push( [flatten_array[i],flatten_array[i+1]] )
+ end
+ end
+
+ def [](x,y)
+ @board.index([x,y]) == nil ? false : true
+ end
+
+ def each(&block)
+ @board.each(&block)
+ end
+
+ def alive_nbours(x, y)
+ neigbours = [[x-1,y-1], [x,y-1], [x+1,y-1], [x-1,y], [x+1,y], [x-1,y+1], [x, y+1], [x+1,y+1]]
+ neigbours.keep_if { |x,y| self[x,y] }
+ neigbours.count
+ end
+
+ # Returns every cell within the borders of our simulation.
+ # Every dead cell that is too far alway from the living cells
+ # on the infinite board can't change it's state.
+ def get_borders
+ x_array = @board.map { |x,y| x }
+ y_array = @board.map { |x,y| y }
+ borders = [x_array.min-1, x_array.max+1, y_array.min-1, y_array.max+1]
+ (borders[0] .. borders[1]).to_a.product (borders[2] .. borders[3]).to_a
+ end
+
+ def next_generation
+ next_gen = @board.select{ |x,y| (cnt = alive_nbours(x, y)) == 2 || cnt == 3 }
+
+ for x,y in get_borders do
+ if alive_nbours(x,y) == 3 && self[x,y] == false then next_gen.push [x,y] end
+ end
+
+ return Board.new(next_gen)
+ end
+ end
+
+end