Решение на Шеста задача от Томислав Дюлгеров

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

Към профила на Томислав Дюлгеров

Резултати

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

Код

module GameOfLife
class Cell
attr_accessor :x, :y
def initialize(x, y)
@x, @y = x, y
end
def neighbours
[ [@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] ]
end
def to_coords
return [@x, @y]
end
end
class Board
include Enumerable
def initialize(*args)
@board = []
args.each do |coords|
@board.push(Cell.new coords[0], coords[1])
end
end
def [](x, y)
@board.each do |cell|
return true if cell.to_coords == [x, y]
end
false
end
def stable?(cell_coords)
alive_neighbours(Cell.new *cell_coords) == 2 or
alive_neighbours(Cell.new *cell_coords) == 3
end
def reproduce?(cell_coords)
alive_neighbours(Cell.new *cell_coords) == 3
end
def next_generation
Board.new *new_cells_coords.uniq
end
def each
@board.each { |cell| yield cell.x, cell.y }
end
def count
@board.count
end
private
def alive_cells
alive_cells = []
@board.each do |cell|
alive_cells << [cell.x, cell.y]
end
alive_cells
end
def alive_neighbours(cell)
count = 0
cell.neighbours.each do |neighbour|
count += 1 if alive_cells.include? neighbour
end
count
end
# Решението работи ако се махнат коментарите.. :(
def new_cells_coords
result = []
# @board.each do |cell|
# result << cell.to_coords if stable? cell.to_coords
# cell.neighbours.select { |neighbour| reproduce? neighbour}.each { |item| result << item }
# end
result
end
end
end

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

......F.....F.FFFF

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-11i6ck0/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 preserves stable cells
     Failure/Error: next_gen[1, 1].should be_true
       expected false to be true
     # /tmp/d20111221-3114-11i6ck0/spec.rb:103: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 sprouts new life when appropriate
     Failure/Error: next_gen[1, 0].should be_true
       expected false to be true
     # /tmp/d20111221-3114-11i6ck0/spec.rb:125: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)>'

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

  5) GameOfLife::Board GameOfLife::Board evolution rules oscilates the oscilators
     Failure/Error: board.count.should eq cells.count
       
       expected: 3
            got: 0
       
       (compared using ==)
     # /tmp/d20111221-3114-11i6ck0/spec.rb:172:in `expect_generation_in'
     # /tmp/d20111221-3114-11i6ck0/spec.rb:145:in `block (5 levels) in <top (required)>'
     # /tmp/d20111221-3114-11i6ck0/spec.rb:142:in `times'
     # /tmp/d20111221-3114-11i6ck0/spec.rb:142: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)>'

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

Failed examples:

rspec /tmp/d20111221-3114-11i6ck0/spec.rb:43 # GameOfLife::Board GameOfLife::Board enumeration live cells count returns the number of live cells on a board
rspec /tmp/d20111221-3114-11i6ck0/spec.rb:95 # GameOfLife::Board GameOfLife::Board evolution rules preserves stable cells
rspec /tmp/d20111221-3114-11i6ck0/spec.rb:117 # GameOfLife::Board GameOfLife::Board evolution rules sprouts new life when appropriate
rspec /tmp/d20111221-3114-11i6ck0/spec.rb:129 # GameOfLife::Board GameOfLife::Board evolution rules evolves a formation correctly
rspec /tmp/d20111221-3114-11i6ck0/spec.rb:139 # GameOfLife::Board GameOfLife::Board evolution rules oscilates the oscilators
rspec /tmp/d20111221-3114-11i6ck0/spec.rb:151 # GameOfLife::Board GameOfLife::Board evolution rules keeps stable formations stable

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

Томислав обнови решението на 20.12.2011 23:17 (преди над 12 години)

+module GameOfLife
+ class Cell
+ attr_accessor :x, :y
+
+ def initialize(x, y)
+ @x, @y = x, y
+ end
+
+ def neighbours
+ [ [@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] ]
+ end
+
+ def to_coords
+ return [@x, @y]
+ end
+ end
+
+ class Board
+ include Enumerable
+
+ def initialize(*args)
+ @board = []
+
+ args.each do |coords|
+ @board.push(Cell.new coords[0], coords[1])
+ end
+ end
+
+ def [](x, y)
+ @board.each do |cell|
+ return true if cell.to_coords == [x, y]
+ end
+
+ false
+ end
+
+ def stable?(cell_coords)
+ alive_neighbours(Cell.new *cell_coords) == 2 or
+ alive_neighbours(Cell.new *cell_coords) == 3
+ end
+
+ def reproduce?(cell_coords)
+ alive_neighbours(Cell.new *cell_coords) == 3
+ end
+
+ def next_generation
+ Board.new *new_cells_coords.uniq
+ end
+
+ def each
+ @board.each { |cell| yield cell.x, cell.y }
+ end
+
+ def count
+ @board.count
+ end
+
+ private
+
+ def alive_cells
+ alive_cells = []
+ @board.each do |cell|
+ alive_cells << [cell.x, cell.y]
+ end
+ alive_cells
+ end
+
+ def alive_neighbours(cell)
+ count = 0
+ cell.neighbours.each do |neighbour|
+ count += 1 if alive_cells.include? neighbour
+ end
+ count
+ end
+
+# Решението работи ако се махнат коментарите.. :(
+ def new_cells_coords
+ result = []
+# @board.each do |cell|
+# result << cell.to_coords if stable? cell.to_coords
+# cell.neighbours.select { |neighbour| reproduce? neighbour}.each { |item| result << item }
+# end
+ result
+ end
+ end
+end