From: Josef 'Jupp' SCHUGT Date: 2003-11-30T07:02:58+09:00 Subject: Re: ruby game of life program Hi! * Van Jacques; 2003-11-29, 12:40 UTC: > 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. Here's some suggestions. 'tos' stands for 'the old situations', 'tng' for 'the next generation'. You could as well use 'last_generation' and 'next_generation'. #!/usr/bin/env ruby N = 7 tos = Array.new tng = Array.new 0.upto(N) { |row| tos[row] = Array.new(N+1) tng[row] = Array.new(N+1) tos[row] = [0, 0, 0, 0, 0, 0, 0, 0] tng[row] = [0, 0, 0, 0, 0, 0, 0, 0] } 2.upto(5) { |column| tos[3][column] = 1 } 0.upto(N) { |row| print tos[row], "\n" } puts 1.upto(6) { |generation| 1.upto(N - 1) { |row| 1.upto(N - 1) { |column| neighbors = 0 -1.upto(1) { |row_offset| -1.upto(1) { |column_offset| unless row_offset == 0 and column_offset == 0 neighbors += tos[row+row_offset][column+column_offset] end } } if tos[row][column] == 0 tng[row][column] = neighbors == 3 ? 1 : 0 else tng[row][column] = (neighbors == 2 or neighbors == 3) ? 1 : 0 end } } 0.upto(N) { |row| 0.upto(N) { |column| tos[row][column] = tng[row][column] } print tos[row], "\n" } puts 0.upto(N) { |row| 0.upto(N) { |column| print (tos[row][column] == 0 ? ' ' : 'X') } puts } } #done Wow, that was my first game of life ever :-> Josef 'Jupp' Schugt -- begin SPAM-POLICY.txt.vbs if msg.size > 100 kB or msg.sender.is_spammer or msg.text.is_spam discard message end