[snip]
class A_cell
attr_accessor(
:state, # :filled or :empty
:neighbors, # 0..8
:loc # [x,y]
)
def initialize(x,y)
@state = :empty
@neighbors = 0
@loc = [x,y]
end
def fate
#depending on @state & @neighbors, return :filled or :empty
end
end
class A_board
attr_reader :cells # a hash of [x,y] => A_cell
def initialize
@cells = Hash.new
end
def step
update_neighbor_count
update_states
end
def update_neighbor_counts
@cells.keys.each { |x,y|
if @cells[[x,y]].state.filled
((x-1)..(x+1)).each { |x0|
((y-1)..(y+1)).each { |y0|
@cells[[x0,y0]] ||= A_cell.new(x0,y0)
@cells[[x0,y0]].neighbors += 1 unless
x0 == x and y0 == y
}
}
end
}
end
def update_states
@cells.keys.each { |x,y|
case @cells[[x,y]].fate
when :filled
@cells[[x,y]].state = :filled
@cells[[x,y]].neighbors = 0
when :empty
@cells.delete_at [x,y]
end
end
end
This has some fat & may be missing some details, but at least it should
give you the idea.
-- Markus
I have also made a gameoflife implementation.. it looks like this
bash-2.05b$ expand -t2 gameoflife.rb
module GameOfLife
def determine_destiny(alive, count)
unless alive
return (count == 3)
end
(count == 2) or (count == 3)
end
def get(cells, y, x)
return 0 if x < 0 or y < 0
return 0 if y >= cells.size
row = cells[y]
return 0 if x >= row.size
row
end
def count_neighbours(cells, x, y)
n = 0
n += get(cells, y-1, x-1)
n += get(cells, y-1, x)
n += get(cells, y-1, x+1)
n += get(cells, y, x-1)
n += get(cells, y, x+1)
n += get(cells, y+1, x-1)
n += get(cells, y+1, x)
n += get(cells, y+1, x+1)
n
end
def lifecycle(cells)
y = 0
next_cells = cells.map do |row|
x = 0
next_row = row.map do |cell|
n = count_neighbours(cells, x, y)
x += 1
determine_destiny((cell != 0), n) ? 1 : 0
end
y += 1
next_row
end
next_cells
end
end
if $0 == __FILE__
puts "lets play a game"
class Game
include GameOfLife
def initialize
@cells = [
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 0],
[0, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]
]
end
def next
@cells = lifecycle(@cells)
end
def inspect
rows = @cells.map do |row|
row.join(" ")
end
rows.join("\n")
end
end
game = Game.new
loop do
p game
gets
game.next
end
end
bash-2.05b$
···
On Friday 24 September 2004 06:25, Markus wrote:
--
Simon Strandgaard