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

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

Към профила на Владимир Ценев

Резултати

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

Код

module GameOfLife
class Board
include Enumerable
def initialize(*args)
@alive_cells = []
unless args.empty?
args.each {|coords| @alive_cells << Cell.new(coords.first, coords.last) }
end
@new_life = []
end
def each &block
@alive_cells.each do |cell|
if block_given?
block.call cell.x, cell.y
else
yield cell.x, cell.y
end
end
end
def next_generation
killed = []
@alive_cells.each {|cell| killed << cell if nb_alive(cell) < 2 || nb_alive(cell) > 3 }
@alive_cells.each {|cell| cell.neighbours.each {|nbour_xy| spurr_life(nbour_xy) } }
next_g = Board.new(*convert(@alive_cells - killed + @new_life))
end
def convert(cells)
result = []
cells.each {|cell| result << [cell.x, cell.y] }
result
end
def [](x, y)
@alive_cells.each {|cell| return true if cell.x == x && cell.y == y }
false
end
def count
@alive_cells.size
end
def spurr_life(cell_coords)
unless cell_alive?(cell_coords.first, cell_coords.last)
Cell.new(cell_coords.first, cell_coords.last).neighbours.each do |coords|
empty_cell = Cell.new(coords.first, coords.last)
check_and_add(empty_cell)
end
end
end
def check_and_add(empty_cell)
if nb_alive(empty_cell) == 3
@new_life << empty_cell unless included_already?(empty_cell)
end
end
def included_already?(new_cell)
@new_life.each {|cell| return true if cell.x == new_cell.x && cell.y == new_cell.y }
false
end
def nb_alive(cell)
result = 0
cell.neighbours.each {|coords| result += 1 if cell_alive?(coords.first, coords.last) }
result
end
alias :cell_alive? :[]
end
class Cell
attr_reader :x, :y, :neighbours
def initialize(x, y)
@x = x
@y = y
@neighbours = get_neighbours
end
private
def get_neighbours
neighbours = []
neighbours << [@x - 1, @y - 1] << [@x - 1, @y] << [@x - 1, @y + 1]
neighbours << [@x + 1, @y - 1] << [@x + 1, @y] << [@x + 1, @y + 1]
neighbours << [@x, @y - 1] << [@x, @y + 1]
end
end
end

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

......F........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-1c7sy25/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 GameOfLife::Board evolution rules evolves a formation correctly
     Failure/Error: board.count.should eq cells.count
       
       expected: 7
            got: 9
       
       (compared using ==)
     # /tmp/d20111221-3114-1c7sy25/spec.rb:172:in `expect_generation_in'
     # /tmp/d20111221-3114-1c7sy25/spec.rb:136: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)>'

  3) GameOfLife::Board GameOfLife::Board evolution rules keeps stable formations stable
     Failure/Error: board.count.should eq cells.count
       
       expected: 4
            got: 8
       
       (compared using ==)
     # /tmp/d20111221-3114-1c7sy25/spec.rb:172:in `expect_generation_in'
     # /tmp/d20111221-3114-1c7sy25/spec.rb:160:in `block (6 levels) in <top (required)>'
     # /tmp/d20111221-3114-1c7sy25/spec.rb:158:in `times'
     # /tmp/d20111221-3114-1c7sy25/spec.rb:158:in `block (5 levels) in <top (required)>'
     # /tmp/d20111221-3114-1c7sy25/spec.rb:155:in `each'
     # /tmp/d20111221-3114-1c7sy25/spec.rb:155: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)>'

Finished in 0.40312 seconds
18 examples, 3 failures

Failed examples:

rspec /tmp/d20111221-3114-1c7sy25/spec.rb:43 # GameOfLife::Board GameOfLife::Board enumeration live cells count returns the number of live cells on a board
rspec /tmp/d20111221-3114-1c7sy25/spec.rb:129 # GameOfLife::Board GameOfLife::Board evolution rules evolves a formation correctly
rspec /tmp/d20111221-3114-1c7sy25/spec.rb:151 # GameOfLife::Board GameOfLife::Board evolution rules keeps stable formations stable

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

Владимир обнови решението на 20.12.2011 20:53 (преди над 12 години)

+module GameOfLife
+
+ class Board
+ include Enumerable
+
+ def initialize(*args)
+ @alive_cells = []
+ unless args.empty?
+ args.each {|coords| @alive_cells << Cell.new(coords.first, coords.last) }
+ end
+ @new_life = []
+ end
+
+ def each &block
+ @alive_cells.each do |cell|
+ if block_given?
+ block.call cell.x, cell.y
+ else
+ yield cell.x, cell.y
+ end
+ end
+ end
+
+ def next_generation
+ killed = []
+ @alive_cells.each {|cell| killed << cell if nb_alive(cell) < 2 || nb_alive(cell) > 3 }
+ @alive_cells.each {|cell| cell.neighbours.each {|nbour_xy| spurr_life(nbour_xy) } }
+ next_g = Board.new(*convert(@alive_cells - killed + @new_life))
+ end
+
+ def convert(cells)
+ result = []
+ cells.each {|cell| result << [cell.x, cell.y] }
+ result
+ end
+
+ def [](x, y)
+ @alive_cells.each {|cell| return true if cell.x == x && cell.y == y }
+ false
+ end
+
+ def count
+ @alive_cells.size
+ end
+
+ def spurr_life(cell_coords)
+ unless cell_alive?(cell_coords.first, cell_coords.last)
+ Cell.new(cell_coords.first, cell_coords.last).neighbours.each do |coords|
+ empty_cell = Cell.new(coords.first, coords.last)
+ check_and_add(empty_cell)
+ end
+ end
+ end
+
+ def check_and_add(empty_cell)
+ if nb_alive(empty_cell) == 3
+ @new_life << empty_cell unless included_already?(empty_cell)
+ end
+ end
+
+ def included_already?(new_cell)
+ @new_life.each {|cell| return true if cell.x == new_cell.x && cell.y == new_cell.y }
+ false
+ end
+
+ def nb_alive(cell)
+ result = 0
+ cell.neighbours.each {|coords| result += 1 if cell_alive?(coords.first, coords.last) }
+ result
+ end
+
+ alias :cell_alive? :[]
+ end
+
+ class Cell
+ attr_reader :x, :y, :neighbours
+
+ def initialize(x, y)
+ @x = x
+ @y = y
+ @neighbours = get_neighbours
+ end
+
+ private
+ def get_neighbours
+ neighbours = []
+ neighbours << [@x - 1, @y - 1] << [@x - 1, @y] << [@x - 1, @y + 1]
+ neighbours << [@x + 1, @y - 1] << [@x + 1, @y] << [@x + 1, @y + 1]
+ neighbours << [@x, @y - 1] << [@x, @y + 1]
+ end
+
+ end
+
+end