Решение на Шеста задача от Георги Лозев

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

Към профила на Георги Лозев

Резултати

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

Код

module GameOfLife
class Board
def initialize(*args)
@grid = {}
@next_grid = Hash.new(0)
args.each{ |cell| @grid[cell] = 0 }
end
def each
@grid.keys.each{ |cell| yield cell }
end
def [](x, y)
@grid.has_key?([x, y])
end
def count
@grid.size
end
def next_generation
add_neighbours
rule_1 = @grid.select{ |cell, neighbours| neighbours == 3 or neighbours == 2 }.keys
rule_2 = @next_grid.select{ |cell, neighbours| neighbours == 3}.keys
Board.new *(rule_1 + rule_2)
end
private
def all_neighbours(cell)
[[-1, 0], [1, 0], [-1, 1], [0, 1], [1, 1], [-1, -1], [0, -1], [1, -1]].map do |item|
item.zip(cell).map { |a| a.inject(:+) }
end
end
def add_neighbours
@grid.keys.each do |cell|
all_neighbours(cell).each do |neighbour|
increment_neighbours(neighbour)
end
end
end
def increment_neighbours(neighbour)
@grid[neighbour] += 1 if @grid.has_key?(neighbour)
@next_grid[neighbour] += 1
end
end
end

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

..F............FFF

Failures:

  1) GameOfLife::Board enumeration responds to each, map, count, etc.
     Failure/Error: board.should respond_to(enumerable_method)
       expected #<GameOfLife::Board:0x94ab704 @grid={}, @next_grid={}> to respond to :each_cons
     # /tmp/d20111221-3114-1aq9kss/spec.rb:19:in `block (4 levels) in <top (required)>'
     # /tmp/d20111221-3114-1aq9kss/spec.rb:18:in `each'
     # /tmp/d20111221-3114-1aq9kss/spec.rb:18: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)>'

  2) GameOfLife::Board GameOfLife::Board evolution rules evolves a formation correctly
     Failure/Error: (board.to_a - cells).should eq []
     NoMethodError:
       undefined method `to_a' for #<GameOfLife::Board:0x9628960>
     # /tmp/d20111221-3114-1aq9kss/spec.rb:173:in `expect_generation_in'
     # /tmp/d20111221-3114-1aq9kss/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 oscilates the oscilators
     Failure/Error: (board.to_a - cells).should eq []
     NoMethodError:
       undefined method `to_a' for #<GameOfLife::Board:0x962d8c0>
     # /tmp/d20111221-3114-1aq9kss/spec.rb:173:in `expect_generation_in'
     # /tmp/d20111221-3114-1aq9kss/spec.rb:145:in `block (5 levels) in <top (required)>'
     # /tmp/d20111221-3114-1aq9kss/spec.rb:142:in `times'
     # /tmp/d20111221-3114-1aq9kss/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)>'

  4) GameOfLife::Board GameOfLife::Board evolution rules keeps stable formations stable
     Failure/Error: (board.to_a - cells).should eq []
     NoMethodError:
       undefined method `to_a' for #<GameOfLife::Board:0x96dcbcc>
     # /tmp/d20111221-3114-1aq9kss/spec.rb:173:in `expect_generation_in'
     # /tmp/d20111221-3114-1aq9kss/spec.rb:160:in `block (6 levels) in <top (required)>'
     # /tmp/d20111221-3114-1aq9kss/spec.rb:158:in `times'
     # /tmp/d20111221-3114-1aq9kss/spec.rb:158:in `block (5 levels) in <top (required)>'
     # /tmp/d20111221-3114-1aq9kss/spec.rb:155:in `each'
     # /tmp/d20111221-3114-1aq9kss/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.33354 seconds
18 examples, 4 failures

Failed examples:

rspec /tmp/d20111221-3114-1aq9kss/spec.rb:15 # GameOfLife::Board enumeration responds to each, map, count, etc.
rspec /tmp/d20111221-3114-1aq9kss/spec.rb:129 # GameOfLife::Board GameOfLife::Board evolution rules evolves a formation correctly
rspec /tmp/d20111221-3114-1aq9kss/spec.rb:139 # GameOfLife::Board GameOfLife::Board evolution rules oscilates the oscilators
rspec /tmp/d20111221-3114-1aq9kss/spec.rb:151 # GameOfLife::Board GameOfLife::Board evolution rules keeps stable formations stable

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

Георги обнови решението на 19.12.2011 23:54 (преди над 8 години)

+module GameOfLife
+ class Board
+ def initialize(*args)
+ @grid = {}
+ @next_grid = Hash.new(0)
+ args.each{ |cell| @grid[cell] = 0 }
+ end
+
+ def each
+ @grid.keys.each{ |cell| yield cell }
+ end
+
+ def [](x, y)
+ @grid.has_key?([x, y])
+ end
+
+ def count
+ @grid.size
+ end
+
+ def next_generation
+ add_neighbours
+ rule_1 = @grid.select{ |cell, neighbours| neighbours == 3 or neighbours == 2 }.keys
+ rule_2 = @next_grid.select{ |cell, neighbours| neighbours == 3}.keys
+
+ Board.new *(rule_1 + rule_2)
+ end
+
+ private
+
+ def all_neighbours(cell)
+ [[-1, 0], [1, 0], [-1, 1], [0, 1], [1, 1], [-1, -1], [0, -1], [1, -1]].map do |item|
+ item.zip(cell).map { |a| a.inject(:+) }
+ end
+ end
+
+ def add_neighbours
+ @grid.keys.each do |cell|
+ all_neighbours(cell).each do |neighbour|
+ increment_neighbours(neighbour)
+ end
+ end
+ end
+
+ def increment_neighbours(neighbour)
+ @grid[neighbour] += 1 if @grid.has_key?(neighbour)
+ @next_grid[neighbour] += 1
+ end
+ end
+end