From: vanjac12@... (Van Jacques) Date: 2003-11-29T15:42:11+09:00 Subject: ruby game of life program Finally its right. It seems so obvious now. I thought I could take this or that shortcut without thinking it thru. I must be more methodical. I couldn't quit till I did it. Now to do some more interesting patterns. Here it is if you are interested. =========== # Size of grid; nn = 8 nnm = nn - 1 nmm = nnm - 1 # Create and Initialize Grid a a = Array.new b = Array.new 0.upto( nnm ) do |i| a[i] = Array.new( nn ) b[i] = Array.new( nn ) a[i] = [0,0,0,0,0,0,0,0] b[i] = [0,0,0,0,0,0,0,0] end # Initial state a[3][2] = 1 a[3][3] = 1 a[3][4] = 1 a[3][5] = 1 0.upto( nnm ) do |i| print a[i] puts "" end puts "" # Apply rules to set a[i][j] = 0 or 1 # What to do at edges? # If a cell is off and has 3 living neighbors (out of 8), it will become alive # in the next generation. # If a cell is on and has 2 or 3 living neighbors, it survives; otherwise, it # dies in the next generation. Otherwise no change. 1.upto( 6 ) do |k| 1.upto( nmm ) do |i| 1.upto( nmm ) do |j| # count n = number of black cells around [i][j] n = a[i-1][j-1] + a[i-1][j] + a[i-1][j+1] + a[i][j-1] + a[i][j+1] + \ a[i+1][j-1] + a[i+1][j] + a[i+1][j+1] if (a[i][j] == 0 and n == 3) then b[i][j] = 1 elsif (a[i][j] == 1 && (n != 2 and n != 3)) then b[i][j] = 0 elsif (a[i][j] == 0 and n != 3) then b[i][j] = 0 elsif (a[i][j] == 1 && (n == 2 or n == 3)) then b[i][j] = 1 end end end 0.upto( nnm ) do |i| 0.upto( nnm ) do |j| a[i][j] = b[i][j] end print a[i] puts "" end puts "" 0.upto( nnm ) do |i| 0.upto( nnm ) do |j| if (a[i][j] == 0) then print ' ' else print 'X' end end puts "" end end ===== Sylvan Jacques ; (Van)vanjac12@yahoo.com