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

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

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

Резултати

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

Код

require 'matrix'
module GameOfLife
class Board
include Enumerable
attr_accessor :board
def initialize(*array_of_cells)
@board = array_of_cells
end
def each
0.step(@board.size-1, 1) { |iter| yield @board[iter] }
end
def [](i, j)
@board.include?([i, j])
end
def count
@board.size
end
def to_matrix
matrix = Matrix.build(max_rows, max_columns) { |i, j| 0 }
@board.each { |array| matrix[array[0], array[1]] = 1 }
matrix
end
def next_generation
self.to_matrix.next_gen(self.to_matrix.clone).to_board
end
private
def max_rows
max = 0
@board.each { |x| max = x[0] if x[0] > max }
max + 2
end
def max_columns
max = 0
@board.each { |x| max = x[1] if x[1] > max }
max + 2
end
end
end
class Matrix
include GameOfLife
def []=(i, j, x)
@rows[i][j] = x
end
def next_gen(matrix)
self.each_with_index do |value, row, col|
matrix[row, col] = 0 if value == 1 && (neighbours(row, col) < 2 || neighbours(row, col) > 3)
matrix[row, col] = 1 if value == 0 && neighbours(row, col) == 3
end
matrix
end
def to_board
array = []
self.each_with_index do |value, row, col|
array << [row, col] if value == 1
end
board = Board.new(*array)
end
private
def upper_neighbours(i, j, sum)
sum += @rows[i-1][j-1] if i > 0 && j > 0
sum += @rows[i-1][j] if i > 0
sum += @rows[i-1][j+1] if i > 0 && j < self.column_size-1
sum
end
def side_neighbours(i, j, sum)
sum += @rows[i][j-1] if j > 0
sum += @rows[i][j+1] if j < self.column_size - 1
sum
end
def bottom_neighbours(i, j, sum)
sum += @rows[i+1][j-1] if i < self.row_size - 1 && j > 0
sum += @rows[i+1][j] if i < self.row_size - 1
sum += @rows[i+1][j+1] if i < self.row_size - 1 && j < self.column_size - 1
sum
end
def neighbours(i, j)
upper_neighbours(i, j, 0) + side_neighbours(i, j, 0) + bottom_neighbours(i, j, 0)
end
end

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

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

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-88c1vj/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 keeps stable formations stable
     Failure/Error: board.count.should eq cells.count
       
       expected: 6
            got: 5
       
       (compared using ==)
     # /tmp/d20111221-3114-88c1vj/spec.rb:172:in `expect_generation_in'
     # /tmp/d20111221-3114-88c1vj/spec.rb:160:in `block (6 levels) in <top (required)>'
     # /tmp/d20111221-3114-88c1vj/spec.rb:158:in `times'
     # /tmp/d20111221-3114-88c1vj/spec.rb:158:in `block (5 levels) in <top (required)>'
     # /tmp/d20111221-3114-88c1vj/spec.rb:155:in `each'
     # /tmp/d20111221-3114-88c1vj/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.36565 seconds
18 examples, 2 failures

Failed examples:

rspec /tmp/d20111221-3114-88c1vj/spec.rb:43 # GameOfLife::Board GameOfLife::Board enumeration live cells count returns the number of live cells on a board
rspec /tmp/d20111221-3114-88c1vj/spec.rb:151 # GameOfLife::Board GameOfLife::Board evolution rules keeps stable formations stable

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

Ростислав обнови решението на 20.12.2011 18:48 (преди над 8 години)

+require 'matrix'
+
+module GameOfLife
+ class Board
+ include Enumerable
+ attr_accessor :board
+
+ def initialize(*array_of_cells)
+ @board = array_of_cells
+ end
+
+ def each
+ 0.step(@board.size-1, 1) { |iter| yield @board[iter] }
+ end
+
+ def [](i, j)
+ @board.include?([i, j])
+ end
+
+ def count
+ @board.size
+ end
+
+ def to_matrix
+ matrix = Matrix.build(max_rows, max_columns) { |i, j| 0 }
+ @board.each { |array| matrix[array[0], array[1]] = 1 }
+ matrix
+ end
+
+ def next_generation
+ self.to_matrix.next_gen(self.to_matrix.clone).to_board
+ end
+
+ private
+ def max_rows
+ max = 0
+ @board.each { |x| max = x[0] if x[0] > max }
+ max + 2
+ end
+
+ def max_columns
+ max = 0
+ @board.each { |x| max = x[1] if x[1] > max }
+ max + 2
+ end
+ end
+end
+
+class Matrix
+ include GameOfLife
+
+ def []=(i, j, x)
+ @rows[i][j] = x
+ end
+
+ def next_gen(matrix)
+ self.each_with_index do |value, row, col|
+ matrix[row, col] = 0 if value == 1 && (neighbours(row, col) < 2 || neighbours(row, col) > 3)
+ matrix[row, col] = 1 if value == 0 && neighbours(row, col) == 3
+ end
+ matrix
+ end
+
+ def to_board
+ array = []
+ self.each_with_index do |value, row, col|
+ array << [row, col] if value == 1
+ end
+ board = Board.new(*array)
+ end
+
+ private
+ def upper_neighbours(i, j, sum)
+ sum += @rows[i-1][j-1] if i > 0 && j > 0
+ sum += @rows[i-1][j] if i > 0
+ sum += @rows[i-1][j+1] if i > 0 && j < self.column_size-1
+ sum
+ end
+
+ def side_neighbours(i, j, sum)
+ sum += @rows[i][j-1] if j > 0
+ sum += @rows[i][j+1] if j < self.column_size - 1
+ sum
+ end
+
+ def bottom_neighbours(i, j, sum)
+ sum += @rows[i+1][j-1] if i < self.row_size - 1 && j > 0
+ sum += @rows[i+1][j] if i < self.row_size - 1
+ sum += @rows[i+1][j+1] if i < self.row_size - 1 && j < self.column_size - 1
+ sum
+ end
+
+ def neighbours(i, j)
+ upper_neighbours(i, j, 0) + side_neighbours(i, j, 0) + bottom_neighbours(i, j, 0)
+ end
+end