On 5/14/06, Mike Nelson <miken700 / yahoo.com> wrote: > I took some golfed code from a discussion here little while back ( > http://www.ruby-forum.com/search?query=Golfing+a+signature.&forums%5B%5D=4 > ) and tried my hand at de-golfing and re-working it a bit to make it > readable. > > There are some things here that are a little clumsy, I think. I'm > wondering if there are other things that I might do to it to make it > even more readable. Any ideas? I made a game of life example some time ago, for an online ruby book (in danish): http://aeditor.rubyforge.org/book/refining.html -- Simon Strandgaard 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[x] 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