From: vanjac12@... (Van Jacques) Date: 2003-12-03T06:57:07+09:00 Subject: Re: New to ruby--trouble with initializing arrays "Robert Klemme" wrote in message news:... > > > > Does this help: > > Slightly improved to tackle the borders. > > class Board > def initialize(size=8) > @size = 8 > @values = Array.new( size * size, 0 ) > end > > def [](x,y) > @values[pos(x,y)]=v > end > > def []=(x,y,v) > @values[pos(x,y)]=v > end > > protected > def pos(x,y) > ( x % @size ) * @size + ( y % @size ) > end > end > > > board = Board.new > > tmp = board.dup > > # manipulate tmp > > board = tmp > > robert This is much more sophiscated than my program (with help from another poster in a similar thread here in comp.lang.ruby), but I used the row % size method for dealing with the edges also. The following initial configuration goes a long time before oscillating (I think), and I recommend running it for anyone who is interested. As a beginner, I'm not used to defining my own classes or def statements, but that will come with time--I hope :-) =========== NN = 20 ; N = NN - 1 n_steps = 100 tos = Array.new tng = Array.new 0.upto(N) { |row| tos[row] = Array.new(NN, 0) tng[row] = Array.new(NN, 0) } tos[4][4] = tos[5][6] = tos[6][3] = tos[6][4] = 1 tos[6][7] = tos[6][8] = tos[6][9] = 1 0.upto(N) do |row| print tos[row], "\n" end puts 1.upto(n_steps) do |generation| 0.upto(N) do |row| 0.upto(N) do |column| neighbors = 0 -1.upto(1) do |row_offset| -1.upto(1) do |column_offset| unless row_offset == 0 and column_offset == 0 i = (row+row_offset) % NN j = (column+column_offset) % NN neighbors += tos[i][j] end end 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 end end 0.upto(N) do |row| 0.upto(N) do |column| tos[row][column] = tng[row][column] end print tos[row], "\n" end puts puts generation puts 0.upto(N) do |row| 0.upto(N) do |column| print (tos[row][column] == 0 ? ' ' : 'X') end puts end end #done