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

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

Към профила на Ростислав Георгиев

Резултати

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

Код

module GameOfLife
class Board
def initialize *cells
@cells = cells
temp_array = []
cells.each { |cell| temp_array << cell.max }
@max_side = 1 + temp_array.max if temp_array != []
end
def count
return @cells.count if @cells
0
end
def each
@cells.each { |cell| yield cell }
end
def next_generation
cell_arr = []
@max_side.times { |x| scan_row x, cell_arr }
Board.new *cell_arr
end
def [] x, y
@cells.include? [x, y]
end
private
def scan_row x, cell_arr
@max_side.times do |y|
cnt = get_alive_neighbour_count x, y
cell_arr << [x, y] if cnt == 3 or (cnt == 2 and self[x, y])
end
end
def get_alive_neighbour_count x, y
count = 0
each { |cell_x, cell_y| count += 1 if (x - cell_x).abs == 1 or (y - cell_y).abs == 1 }
count
end
end
end

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

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

Failures:

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

  3) GameOfLife::Board evolution returns a new board
     Failure/Error: next_gen = board.next_generation
     NoMethodError:
       undefined method `times' for nil:NilClass
     # /tmp/d20111221-3114-ivbdbt/solution.rb:23:in `next_generation'
     # /tmp/d20111221-3114-ivbdbt/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)>'

  4) GameOfLife::Board GameOfLife::Board evolution rules evolves a formation correctly
     Failure/Error: board.count.should eq cells.count
       
       expected: 7
            got: 5
       
       (compared using ==)
     # /tmp/d20111221-3114-ivbdbt/spec.rb:172:in `expect_generation_in'
     # /tmp/d20111221-3114-ivbdbt/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.to_a - cells).should eq []
     NoMethodError:
       undefined method `to_a' for #<GameOfLife::Board:0x9feacdc>
     # /tmp/d20111221-3114-ivbdbt/spec.rb:173:in `expect_generation_in'
     # /tmp/d20111221-3114-ivbdbt/spec.rb:145:in `block (5 levels) in <top (required)>'
     # /tmp/d20111221-3114-ivbdbt/spec.rb:142:in `times'
     # /tmp/d20111221-3114-ivbdbt/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.to_a - cells).should eq []
     NoMethodError:
       undefined method `to_a' for #<GameOfLife::Board:0x9fefef8>
     # /tmp/d20111221-3114-ivbdbt/spec.rb:173:in `expect_generation_in'
     # /tmp/d20111221-3114-ivbdbt/spec.rb:160:in `block (6 levels) in <top (required)>'
     # /tmp/d20111221-3114-ivbdbt/spec.rb:158:in `times'
     # /tmp/d20111221-3114-ivbdbt/spec.rb:158:in `block (5 levels) in <top (required)>'
     # /tmp/d20111221-3114-ivbdbt/spec.rb:155:in `each'
     # /tmp/d20111221-3114-ivbdbt/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.32372 seconds
18 examples, 6 failures

Failed examples:

rspec /tmp/d20111221-3114-ivbdbt/spec.rb:15 # GameOfLife::Board enumeration responds to each, map, count, etc.
rspec /tmp/d20111221-3114-ivbdbt/spec.rb:43 # GameOfLife::Board GameOfLife::Board enumeration live cells count returns the number of live cells on a board
rspec /tmp/d20111221-3114-ivbdbt/spec.rb:72 # GameOfLife::Board evolution returns a new board
rspec /tmp/d20111221-3114-ivbdbt/spec.rb:129 # GameOfLife::Board GameOfLife::Board evolution rules evolves a formation correctly
rspec /tmp/d20111221-3114-ivbdbt/spec.rb:139 # GameOfLife::Board GameOfLife::Board evolution rules oscilates the oscilators
rspec /tmp/d20111221-3114-ivbdbt/spec.rb:151 # GameOfLife::Board GameOfLife::Board evolution rules keeps stable formations stable

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

Ростислав обнови решението на 20.12.2011 16:30 (преди над 12 години)

+module GameOfLife
+ class Board
+
+ def initialize *cells
+ @cells = cells
+
+ temp_array = []
+ cells.each { |cell| temp_array << cell.max }
+ @max_side = 1 + temp_array.max if temp_array != []
+ end
+
+ def count
+ return @cells.count if @cells
+ 0
+ end
+
+ def each
+ @cells.each { |cell| yield cell }
+ end
+
+ def next_generation
+ cell_arr = []
+ @max_side.times { |x| scan_row x, cell_arr }
+ Board.new *cell_arr
+ end
+
+ def [] x, y
+ @cells.include? [x, y]
+ end
+
+ private
+ def scan_row x, cell_arr
+ @max_side.times do |y|
+ cnt = get_alive_neighbour_count x, y
+ cell_arr << [x, y] if cnt == 3 or (cnt == 2 and self[x, y])
+ end
+ end
+
+ def get_alive_neighbour_count x, y
+ count = 0
+ each { |cell_x, cell_y| count += 1 if (x - cell_x).abs == 1 or (y - cell_y).abs == 1 }
+ count
+ end
+ end
+end