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

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

Към профила на Ангел Миланов

Резултати

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

Код

class GameOfLife
class Board
include Enumerable
attr_accessor :cells
def each
@cells.map{|n| yield n[0], n[1]}
end
def initialize (*cells)
@cells=cells
end
def [](x,y)
self.include? [x,y]
end
def print
self.map do |x, y|
puts "The cell at (#{x}, #{y}) is alive"
end
end
def neighbours(x,y)
[[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 living_neighbours_count(x,y)
res=neighbours(x,y).select{|x,y| self[x,y]}
res.count
end
def surviving_cells
self.select {|x,y| living_neighbours_count(x,y)==2 or living_neighbours_count(x,y)==3}
end
def new_neighbours(x,y)
res=neighbours(x,y).select {|x,y| not self[x,y] }
res.select {|x,y| living_neighbours_count(x,y)==2 or living_neighbours_count(x,y)==3}
end
def next_generation
new_board_values=self.inject(surviving_cells) {|res, x| res | new_neighbours(x[0],x[1])}
next_gen=Board.new
next_gen.cells=new_board_values
next_gen
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-dr8az0/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 evolves a formation correctly
     Failure/Error: board.count.should eq cells.count
       
       expected: 7
            got: 9
       
       (compared using ==)
     # /tmp/d20111221-3114-dr8az0/spec.rb:172:in `expect_generation_in'
     # /tmp/d20111221-3114-dr8az0/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.count.should eq cells.count
       
       expected: 3
            got: 7
       
       (compared using ==)
     # /tmp/d20111221-3114-dr8az0/spec.rb:172:in `expect_generation_in'
     # /tmp/d20111221-3114-dr8az0/spec.rb:145:in `block (5 levels) in <top (required)>'
     # /tmp/d20111221-3114-dr8az0/spec.rb:142:in `times'
     # /tmp/d20111221-3114-dr8az0/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.count.should eq cells.count
       
       expected: 4
            got: 12
       
       (compared using ==)
     # /tmp/d20111221-3114-dr8az0/spec.rb:172:in `expect_generation_in'
     # /tmp/d20111221-3114-dr8az0/spec.rb:160:in `block (6 levels) in <top (required)>'
     # /tmp/d20111221-3114-dr8az0/spec.rb:158:in `times'
     # /tmp/d20111221-3114-dr8az0/spec.rb:158:in `block (5 levels) in <top (required)>'
     # /tmp/d20111221-3114-dr8az0/spec.rb:155:in `each'
     # /tmp/d20111221-3114-dr8az0/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.34206 seconds
18 examples, 4 failures

Failed examples:

rspec /tmp/d20111221-3114-dr8az0/spec.rb:43 # GameOfLife::Board GameOfLife::Board enumeration live cells count returns the number of live cells on a board
rspec /tmp/d20111221-3114-dr8az0/spec.rb:129 # GameOfLife::Board GameOfLife::Board evolution rules evolves a formation correctly
rspec /tmp/d20111221-3114-dr8az0/spec.rb:139 # GameOfLife::Board GameOfLife::Board evolution rules oscilates the oscilators
rspec /tmp/d20111221-3114-dr8az0/spec.rb:151 # GameOfLife::Board GameOfLife::Board evolution rules keeps stable formations stable

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

Ангел обнови решението на 17.12.2011 12:23 (преди над 8 години)

+class GameOfLife
+class Board
+ include Enumerable
+
+
+ attr_accessor :cells
+
+
+ def each
+ @cells.map{|n| yield n[0], n[1]}
+ end
+
+
+ def initialize (*cells)
+ @cells=cells
+ end
+
+
+ def [](x,y)
+ self.include? [x,y]
+ end
+
+
+ def print
+ self.map do |x, y|
+ puts "The cell at (#{x}, #{y}) is alive"
+ end
+ end
+
+
+ def neighbours(x,y)
+ [[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 living_neighbours_count(x,y)
+ res=neighbours(x,y).select{|x,y| self[x,y]}
+ res.count
+ end
+
+
+ def surviving_cells
+ self.select {|x,y| living_neighbours_count(x,y)==2 or living_neighbours_count(x,y)==3}
+ end
+
+
+ def new_neighbours(x,y)
+ res=neighbours(x,y).select {|x,y| not self[x,y] }
+ res.select {|x,y| living_neighbours_count(x,y)==2 or living_neighbours_count(x,y)==3}
+ end
+
+
+ def next_generation
+ new_board_values=self.inject(surviving_cells) {|res, x| res | new_neighbours(x[0],x[1])}
+ next_gen=Board.new
+ next_gen.cells=new_board_values
+ next_gen
+ end
+
+
+end
+
+
+end