Решение на Шеста задача от Николай Стоицев

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

Към профила на Николай Стоицев

Резултати

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

Код

module GameOfLife
class Board
include Enumerable
def initialize(*args)
@cells = args
end
def [](x, y)
@cells.include? [x, y]
end
def each
@cells.each { |x| yield x}
end
def count
@cells.count
end
def next_generation
BoardProcessor.new(*@cells).process
end
def kill_cell(cell)
@cells.delete cell
end
end
class BoardProcessor
def initialize(*cells)
@board = Board.new *cells
@new_board = []
end
def neighbours(cell)
x = cell[0]
y = cell[1]
neighbours_list = [[x - 1, y + 1], [x, y + 1], [x + 1, y + 1], [x - 1, y], [x + 1, y]]
neighbours_list += [[x - 1, y - 1], [x, y - 1], [x + 1, y - 1]]
end
def process
@board.each { |cell|
process_cell cell
}
Board.new *@new_board
end
def process_cell(cell)
neighbours_list = neighbours cell
num_life_neighbours = alive_neighbours neighbours_list
apply_rules cell, num_life_neighbours
grow_cells neighbours_list
end
def apply_rules(cell, num)
if num >= 2 and num <= 3 then @new_board << cell end
end
def alive_neighbours(list)
list.select { |cell| @board[cell[0], cell[1]] }.count
end
def grow_cells(list)
list.each { |cell|
if (alive_neighbours cell) >= 3 then @new_board << cell end
}
end
end
end

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

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

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-1syal70/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 sprouts new life when appropriate
     Failure/Error: next_gen[1, 0].should be_true
       expected false to be true
     # /tmp/d20111221-3114-1syal70/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)>'

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

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

Finished in 0.35886 seconds
18 examples, 4 failures

Failed examples:

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

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

Николай обнови решението на 20.12.2011 23:19 (преди над 8 години)

+module GameOfLife
+
+ class Board
+ include Enumerable
+
+ def initialize(*args)
+ @cells = args
+ end
+
+ def [](x, y)
+ @cells.include? [x, y]
+ end
+
+ def each
+ @cells.each { |x| yield x}
+ end
+
+ def count
+ @cells.count
+ end
+
+ def next_generation
+ BoardProcessor.new(*@cells).process
+ end
+
+ def kill_cell(cell)
+ @cells.delete cell
+ end
+
+ end
+
+ class BoardProcessor
+
+ def initialize(*cells)
+ @board = Board.new *cells
+ @new_board = []
+ end
+
+ def neighbours(cell)
+ x = cell[0]
+ y = cell[1]
+ neighbours_list = [[x - 1, y + 1], [x, y + 1], [x + 1, y + 1], [x - 1, y], [x + 1, y]]
+ neighbours_list += [[x - 1, y - 1], [x, y - 1], [x + 1, y - 1]]
+ end
+
+ def process
+ @board.each { |cell|
+ process_cell cell
+ }
+ Board.new *@new_board
+ end
+
+ def process_cell(cell)
+ neighbours_list = neighbours cell
+ num_life_neighbours = alive_neighbours neighbours_list
+ apply_rules cell, num_life_neighbours
+ grow_cells neighbours_list
+ end
+
+ def apply_rules(cell, num)
+ if num >= 2 and num <= 3 then @new_board << cell end
+ end
+
+ def alive_neighbours(list)
+ list.select { |cell| @board[cell[0], cell[1]] }.count
+ end
+
+ def grow_cells(list)
+ list.each { |cell|
+ if (alive_neighbours cell) >= 3 then @new_board << cell end
+ }
+ end
+
+ end
+
+end