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

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

Към профила на Николай Беличев

Резултати

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

Код

module GameOfLife
class Board
include Enumerable
def initialize(*args)
@hash_board = Hash.new (false)
args.each { |cell| @hash_board[cell[0].to_s + ' ' + cell[1].to_s] = true }
end
def add(cell)
@hash_board[cell[0].to_s + ' ' + cell[1].to_s] = true
end
def each
@hash_board.each_key do
|cell|
numbers = cell.split()
yield numbers[0].to_i, numbers[1].to_i
end
end
def [](x, y)
@hash_board[x.to_s + ' ' + y.to_s]
end
def init_min_max
x_array, y_array = Array.new, Array.new
@hash_board.each_key { |cell| x_array << cell.split()[0].to_i }
@hash_board.each_key { |cell| y_array << cell.split()[1].to_i }
@max_x, @max_y, @min_x, @min_y = x_array.max, y_array.max, x_array.min, y_array.min
end
def next_generation
@next_gen = GameOfLife::Board.new
if @hash_board.count > 0
all_cell_loop
end
@next_gen
end
def all_cell_loop
init_min_max
for i in (@min_x - 1)..(@max_x + 1) do
for j in (@min_y - 1)..(@max_y + 1) do
apply_rules(i, j, @hash_board[i.to_s + ' ' + j.to_s])
end
end
end
def apply_rules(x, y, cell_alive)
neighbours = AliveNeighbours.new(@hash_board, x, y).count
if cell_alive && (neighbours == 2 || neighbours == 3) then @next_gen.add [x, y]
elsif !cell_alive && neighbours == 3 then @next_gen.add [x, y]
else false
end
end
end
class AliveNeighbours
def initialize (hash_board, x, y)
@hash_board = hash_board
@x = x
@y = y
end
def left
@hash_board[(@x-1).to_s + ' ' + (@y).to_s] ? 1 : 0
end
def right
@hash_board[(@x+1).to_s + ' ' + (@y).to_s] ? 1 : 0
end
def upleft
@hash_board[(@x-1).to_s + ' ' + (@y+1).to_s] ? 1 : 0
end
def up
@hash_board[(@x).to_s + ' ' + (@y+1).to_s] ? 1 : 0
end
def upright
@hash_board[(@x+1).to_s + ' ' + (@y+1).to_s] ? 1 : 0
end
def downleft
@hash_board[(@x-1).to_s + ' ' + (@y-1).to_s] ? 1 : 0
end
def down
@hash_board[(@x).to_s + ' ' + (@y-1).to_s] ? 1 : 0
end
def downright
@hash_board[(@x+1).to_s + ' ' + (@y-1).to_s] ? 1 : 0
end
def count
left + right + upleft + up + upright + downleft + down + downright
end
end
end

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

..................

Finished in 0.3758 seconds
18 examples, 0 failures

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

Николай обнови решението на 18.12.2011 03:41 (преди над 8 години)

+module GameOfLife
+ class Board
+ include Enumerable
+
+ def initialize(*args)
+ @hash_board = Hash.new (false)
+ args.each { |cell| @hash_board[cell[0].to_s + ' ' + cell[1].to_s] = true }
+ end
+
+ def add(cell)
+ @hash_board[cell[0].to_s + ' ' + cell[1].to_s] = true
+ end
+
+ def each
+ @hash_board.each_key do
+ |cell|
+ numbers = cell.split()
+ yield numbers[0].to_i, numbers[1].to_i
+ end
+ end
+
+ def [](x, y)
+ @hash_board[x.to_s + ' ' + y.to_s]
+ end
+
+ def init_min_max
+ x_array, y_array = Array.new, Array.new
+ @hash_board.each_key { |cell| x_array << cell.split()[0].to_i }
+ @hash_board.each_key { |cell| y_array << cell.split()[1].to_i }
+ @max_x, @max_y, @min_x, @min_y = x_array.max, y_array.max, x_array.min, y_array.min
+ end
+
+ def next_generation
+ @next_gen = GameOfLife::Board.new
+ if @hash_board.count > 0
+ all_cell_loop
+ end
+ @next_gen
+ end
+
+ def all_cell_loop
+ init_min_max
+ for i in (@min_x - 1)..(@max_x + 1) do
+ for j in (@min_y - 1)..(@max_y + 1) do
+ apply_rules(i, j, @hash_board[i.to_s + ' ' + j.to_s])
+ end
+ end
+ end
+
+ def apply_rules(x, y, cell_alive)
+ neighbours = AliveNeighbours.new(@hash_board, x, y).count
+ if cell_alive && (neighbours == 2 || neighbours == 3) then @next_gen.add [x, y]
+ elsif !cell_alive && neighbours == 3 then @next_gen.add [x, y]
+ else false
+ end
+ end
+ end
+
+ class AliveNeighbours
+ def initialize (hash_board, x, y)
+ @hash_board = hash_board
+ @x = x
+ @y = y
+ end
+
+ def left
+ @hash_board[(@x-1).to_s + ' ' + (@y).to_s] ? 1 : 0
+ end
+
+ def right
+ @hash_board[(@x+1).to_s + ' ' + (@y).to_s] ? 1 : 0
+ end
+
+ def upleft
+ @hash_board[(@x-1).to_s + ' ' + (@y+1).to_s] ? 1 : 0
+ end
+
+ def up
+ @hash_board[(@x).to_s + ' ' + (@y+1).to_s] ? 1 : 0
+ end
+
+ def upright
+ @hash_board[(@x+1).to_s + ' ' + (@y+1).to_s] ? 1 : 0
+ end
+
+ def downleft
+ @hash_board[(@x-1).to_s + ' ' + (@y-1).to_s] ? 1 : 0
+ end
+
+ def down
+ @hash_board[(@x).to_s + ' ' + (@y-1).to_s] ? 1 : 0
+ end
+
+ def downright
+ @hash_board[(@x+1).to_s + ' ' + (@y-1).to_s] ? 1 : 0
+ end
+
+ def count
+ left + right + upleft + up + upright + downleft + down + downright
+ end
+ end
+end