From: vanjac12@... (Van Jacques) Date: 2003-12-01T20:42:15+09:00 Subject: Re: ruby game of life program Thanks for the suggestions. A great improvement. I was thinking about what to do about the edges when the initial pattern expanded to the edges. (This initial pattern stays in the middle, and only changes for the first few generations, but others do all kinds of things, as I'm sure most people know.) Have you run this? I am going to. You appear to know the language pretty well. On the LHS (row = 0) the edges won't be a problem, since -1 in ruby will wrap around to the RHS. But I haven't thought about what to do when a live cell reaches the RHS, since when row goes out of range it will get nil. Maybe I can turn nil into 0. I will have the same behavior at the top and bottom of the grid too. ============= "Josef 'Jupp' SCHUGT" wrote in message news:<20031129174431.GB2320@jupp%gmx.de>... > > 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