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

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

Към профила на Ивайло Сачански

Резултати

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

Код

module GameOfLife
class Board
def initialize *arg
init_members
init_dimensions arg
(0...@matrix_rows).each { |x| @ma3x[x] = Array.new @matrix_cols, 0 }
arg.each { |cell| @ma3x[cell[0]][cell[1]] = 1 }
end
def init_members
@points = []
@ma3x = []
@matrix_rows = 0
@matrix_cols = 0
end
def each
@points.each { |point| yield point }
end
def init_dimensions arg
arg.each do |x|
@points << x
@matrix_cols < x[1] + 1 ? @matrix_cols = x[1] + 1: nil
@matrix_rows < x[0] + 1 ? @matrix_rows = x[0] + 1: nil
end
end
def [] x, y
@points.each do |p|
if p[0] == x && p[1] == y
return true
end
end
return false
end
def count
@points.size
end
def next_generation
helper = BoardHelper.new @matrix_cols, @matrix_rows, @ma3x
helper.next_gen
end
end
class BoardHelper
def initialize cols, rows, ma3x
@cols = cols
@rows = rows
@ma3x = ma3x
end
def next_gen
new_points = []
@ma3x.each_index do |row|
add_points! row, new_points
end
Board.new *new_points
end
def add_points! row, new_points
@ma3x[row].each_index do |cell|
res = (new_cell row, cell)
if res then new_points << res end
end
end
def new_cell x, y
if ((@ma3x[x][y] == 1) && (alive_neighbours(x, y) < 2)) then return nil end
if ((@ma3x[x][y] == 1) && (2..3).include?(alive_neighbours(x, y))) then return [x,y] end
if ((@ma3x[x][y] == 1) && (alive_neighbours(x, y) > 3)) then return nil end
if ((@ma3x[x][y] == 0) && (alive_neighbours(x, y) == 3)) then return [x,y] end
end
def alive_neighbours x, y
(cell_value x - 1, y - 1) + (cell_value x - 1, y) + (cell_value x - 1, y + 1) +
(cell_value x, y - 1) + (cell_value x, y + 1) +
(cell_value x + 1, y - 1) + (cell_value x + 1, y) + (cell_value x + 1, y + 1)
end
def cell_value x, y
x >= 0 && x < @rows && y >= 0 && y < @cols ? @ma3x[x][y] : 0
end
end
end

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

..F...F.......FFFF

Failures:

  1) GameOfLife::Board enumeration responds to each, map, count, etc.
     Failure/Error: board.should respond_to(enumerable_method)
       expected #<GameOfLife::Board:0x922da40 @points=[], @ma3x=[], @matrix_rows=0, @matrix_cols=0> to respond to :each_cons
     # /tmp/d20111221-3114-noygyi/spec.rb:19:in `block (4 levels) in <top (required)>'
     # /tmp/d20111221-3114-noygyi/spec.rb:18:in `each'
     # /tmp/d20111221-3114-noygyi/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-noygyi/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 GameOfLife::Board evolution rules sprouts new life when appropriate
     Failure/Error: next_gen[1, 2].should be_true
       expected false to be true
     # /tmp/d20111221-3114-noygyi/spec.rb:126: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 evolves a formation correctly
     Failure/Error: (board.to_a - cells).should eq []
     NoMethodError:
       undefined method `to_a' for #<GameOfLife::Board:0x93874cc>
     # /tmp/d20111221-3114-noygyi/spec.rb:173:in `expect_generation_in'
     # /tmp/d20111221-3114-noygyi/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:0x938cecc>
     # /tmp/d20111221-3114-noygyi/spec.rb:173:in `expect_generation_in'
     # /tmp/d20111221-3114-noygyi/spec.rb:145:in `block (5 levels) in <top (required)>'
     # /tmp/d20111221-3114-noygyi/spec.rb:142:in `times'
     # /tmp/d20111221-3114-noygyi/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:0x93901f8>
     # /tmp/d20111221-3114-noygyi/spec.rb:173:in `expect_generation_in'
     # /tmp/d20111221-3114-noygyi/spec.rb:160:in `block (6 levels) in <top (required)>'
     # /tmp/d20111221-3114-noygyi/spec.rb:158:in `times'
     # /tmp/d20111221-3114-noygyi/spec.rb:158:in `block (5 levels) in <top (required)>'
     # /tmp/d20111221-3114-noygyi/spec.rb:155:in `each'
     # /tmp/d20111221-3114-noygyi/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.32506 seconds
18 examples, 6 failures

Failed examples:

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

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

Ивайло обнови решението на 20.12.2011 12:37 (преди над 8 години)

+module GameOfLife
+ class Board
+ def initialize *arg
+ init_members
+ init_dimensions arg
+ (0...@matrix_rows).each { |x| @ma3x[x] = Array.new @matrix_cols, 0 }
+ arg.each { |cell| @ma3x[cell[0]][cell[1]] = 1 }
+ end
+
+ def init_members
+ @points = []
+ @ma3x = []
+ @matrix_rows = 0
+ @matrix_cols = 0
+ end
+
+ def each
+ @points.each { |point| yield point }
+ end
+
+ def init_dimensions arg
+ arg.each do |x|
+ @points << x
+ @matrix_cols < x[1] + 1 ? @matrix_cols = x[1] + 1: nil
+ @matrix_rows < x[0] + 1 ? @matrix_rows = x[0] + 1: nil
+ end
+ end
+
+ def [] x, y
+ @points.each do |p|
+ if p[0] == x && p[1] == y
+ return true
+ end
+ end
+ return false
+ end
+
+ def count
+ @points.size
+ end
+
+ def next_generation
+ helper = BoardHelper.new @matrix_cols, @matrix_rows, @ma3x
+ helper.next_gen
+ end
+ end
+
+ class BoardHelper
+ def initialize cols, rows, ma3x
+ @cols = cols
+ @rows = rows
+ @ma3x = ma3x
+ end
+
+ def next_gen
+ new_points = []
+ @ma3x.each_index do |row|
+ add_points! row, new_points
+ end
+ Board.new *new_points
+ end
+
+ def add_points! row, new_points
+ @ma3x[row].each_index do |cell|
+ res = (new_cell row, cell)
+ if res then new_points << res end
+ end
+ end
+
+ def new_cell x, y
+ if ((@ma3x[x][y] == 1) && (alive_neighbours(x, y) < 2)) then return nil end
+ if ((@ma3x[x][y] == 1) && (2..3).include?(alive_neighbours(x, y))) then return [x,y] end
+ if ((@ma3x[x][y] == 1) && (alive_neighbours(x, y) > 3)) then return nil end
+ if ((@ma3x[x][y] == 0) && (alive_neighbours(x, y) == 3)) then return [x,y] end
+ end
+
+ def alive_neighbours x, y
+ (cell_value x - 1, y - 1) + (cell_value x - 1, y) + (cell_value x - 1, y + 1) +
+ (cell_value x, y - 1) + (cell_value x, y + 1) +
+ (cell_value x + 1, y - 1) + (cell_value x + 1, y) + (cell_value x + 1, y + 1)
+ end
+
+ def cell_value x, y
+ x >= 0 && x < @rows && y >= 0 && y < @cols ? @ma3x[x][y] : 0
+ end
+ end
+end