From: "Jesús Gabriel y Galán" Date: 2009-02-23T17:43:08+09:00 Subject: Re: [QUIZ] Game of Life (#193) On Fri, Feb 20, 2009 at 6:18 PM, Daniel Moore wrote: > > ## Game of Life (#193) > > This weeks quiz is to produce an implementation of [Conway's Game of > Life][1]. The Game of Life is a cellular automaton with simple rules: Hi, Thanks for this very interesting quiz. I have to admit that I already had developed this, so I'm just sending what I had. I think I have learned some Ruby since I did this, but I haven't had the time to review this solution, so there might be some things that I'd do differently now. Anyway, here's what I got. The programs does an "animation" (printing the grid to the screen, so if you only look to the lower part of your monitor it "might" look as an animation :-): class GameOfLifeStateMachine DEATH = [0, 1, 4, 5, 6, 7, 8] LIFE = [2, 3] BIRTH = [3] LIVING_CELL = 1 DEAD_CELL = 0 def self.next_state(previous, alive_neighbours) case previous when DEAD_CELL if BIRTH.include? alive_neighbours LIVING_CELL else DEAD_CELL end when LIVING_CELL if DEATH.include? alive_neighbours DEAD_CELL else LIVING_CELL end else DEAD_CELL end end end class GameOfLife attr_reader :grid def initialize(rows, columns) @rows = rows @columns = columns @grid = Array.new(rows) {Array.new(columns, GameOfLifeStateMachine::DEAD_CELL)} end # State is an array of arrays containing points to make alive def set_initial_state(state) state.each {|a,b| @grid[a][b]=GameOfLifeStateMachine::LIVING_CELL} end def next_state new_grid = [] @grid.each_with_index do |row, i| new_row = [] row.each_with_index do |column, j| new_row << GameOfLifeStateMachine.next_state(@grid[i][j], alive_neighbours(i,j)) end new_grid << new_row end @grid = new_grid end def alive_neighbours(row, column) count = 0 (-1..1).each do |i| (-1..1).each do |j| next if (i.zero? and j.zero?) row_index = row + i col_index = column + j if row_index >= 0 and row_index < @rows and col_index >= 0 and col_index < @columns count += 1 if @grid[row_index][col_index] == GameOfLifeStateMachine::LIVING_CELL end end end count end def to_s s = "" @grid.each {|row| row.each {|col| s << col.to_s << " "}; s << "\n"} s end end rows = 10 cols = 20 game = GameOfLife.new rows,cols # Oscillators #game.set_initial_state([[5,5],[5,6],[5,7]]) #game.set_initial_state([[5,5],[5,6],[5,7],[6,4],[6,5],[6,6]]) # Glider game.set_initial_state([[0,1],[1,2],[2,0],[2,1],[2,2]]) puts game.to_s.gsub(/1/,"#") puts while true # puts "Press enter for next state" # gets game.next_state puts game.to_s.gsub(/1/,"#") puts "-" * cols sleep(0.5) end Jesus.