Решение на Шеста задача от Георги Бойваленков

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

Към профила на Георги Бойваленков

Резултати

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

Код

module GameOfLife
class Board
def initialize(*coords)
return 0 if coords == nil
@living = coords.uniq
@diffs = [[-1, -1],[-1, 0],[-1, 1],[0, -1],[0, 1],[1, -1],[1, 0],[1, 1]]
#@neighbours_count = Hash.new(0)
end
def [](a, b)
(@living.count([a, b]) > 0)
end
def count_neighbours()
neighbours = Hash.new(0)
@living.each do |x, y|
@diffs.each {|px, py| neighbours[[x + px, y + py]] += 1}
end
neighbours
end
def proccess_cell(k, v, generation)
if (@living.count(k) > 0 and (v == 2 or v == 3)) or (@living.count(k) == 0 and v == 3)
generation << k
end
generation
end
def next_generation()
neighbours, new_generation = count_neighbours(), []
neighbours.each do |k, v|
new_generation = proccess_cell(k, v, new_generation)
end
Board.new(*coords = new_generation)
end
def each()
@living.each do |x, y|
yield x, y
end
end
def count()
@living.size
end
def to_a()
@living
end
end
end

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

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

Failures:

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

Finished in 0.36512 seconds
18 examples, 1 failure

Failed examples:

rspec /tmp/d20111221-3114-c4tx8p/spec.rb:15 # GameOfLife::Board enumeration responds to each, map, count, etc.

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

Георги обнови решението на 17.12.2011 17:03 (преди над 8 години)

+module GameOfLife
+
+ class Board
+
+ def initialize(*coords)
+ return 0 if coords == nil
+ @living = coords.uniq
+ @diffs = [[-1, -1],[-1, 0],[-1, 1],[0, -1],[0, 1],[1, -1],[1, 0],[1, 1]]
+ #@neighbours_count = Hash.new(0)
+ end
+
+ def [](a, b)
+ (@living.count([a, b]) > 0)
+ end
+
+ def count_neighbours()
+ neighbours = Hash.new(0)
+ @living.each do |x, y|
+ @diffs.each {|px, py| neighbours[[x + px, y + py]] += 1}
+ end
+ neighbours
+ end
+
+ def proccess_cell(k, v, generation)
+ if (@living.count(k) > 0 and (v == 2 or v == 3)) or (@living.count(k) == 0 and v == 3)
+ generation << k
+ end
+ generation
+ end
+
+
+ def next_generation()
+ neighbours, new_generation = count_neighbours(), []
+ neighbours.each do |k, v|
+ new_generation = proccess_cell(k, v, new_generation)
+ end
+ Board.new(*coords = new_generation)
+ end
+
+ def each()
+ @living.each do |x, y|
+ yield x, y
+ end
+ end
+
+ def count()
+ @living.size
+ end
+
+ def to_a()
+ @living
+ end
+
+ end
+
+end