Making an array of arrays?

Hello,

I’m trying to make an array of arrays, but my code is not behaving as I
expect.

Specifically I am trying to make an array to hold Sudoku boards (each
board is a 2D array). I am doing this because I am trying to use genetic
programming techniques to attempt to solve a simple sudoku puzzle.

Here is the code I am using to copy the @board array in to @offspring:

#When the for loop can take the value from pop must subtract 1 from it
to avoid having to do nasty index manipulation on the array
i = 0
#Define an offspring array that will hold the copies of the @board array
@offspring = Array.new
for i in 0…3
@offspring[i] = @board.clone
end
#For debugging
#puts @offspring.length
end

I expected that the code above would populate the @offspring array, so
that each element contained an array of arrays (a copy of @board).

My aim is to itterate through @offspring, and replace each “" character
in @offspring with a random number. Note for testing purposes I am
trying to replace all instances of "
” with 9.

The code to do this is here:

#All empty cells in offspring can be populated with rand nums in one go
#Then run the ‘solved’ method again
def find_empty
##Loop through @offspring and where ever there is “" put a random
number.
i = 0
for i in 0…80
if @offspring[i] = "

then @offspring[i]= 9
end
#For debugging with the test puzzle
puts @offspring.inspect
end

@offspring.inspect simply prints out a line of 9s. I have tried
replacing ‘for i in 0…80’ with ‘for i in 0…4’, as I felt the number of
itterations should reflect the size of the offspring array. This doesn’t
return the result I want.

I am now stuck. I know what I want to achieve, but haven’t got the logic
quite right. I’m guessing I am dealing with 3D arrays, but am starting
to confuse myself now.

Note I am using a test puzzle. It only contains one “_” char, which must
be replaced with 3 for the puzzle to be solved.

Any suggestions that can enable me to achieve what I want are greatly
appreciated.

Many thanks in advance,
Jen.

On Tue, Mar 15, 2011 at 3:23 PM, Jen [email protected]
wrote:

Just as an aside – this seems awfully “un-ruby-ish” style –

#When the for loop can take the value from pop must subtract 1 from it to
avoid having to do nasty index manipulation on the array
i = 0
#Define an offspring array that will hold the copies of the @board array
@offspring = Array.new
for i in 0…3
@offspring[i] = @board.clone
end

@offspring = []
4.times{ @offspring << @board.clone }

But regardless,

##Loop through @offspring and where ever there is “" put a random number.
i = 0
for i in 0…80
if @offspring[i] = "

The line above is assigning (=) a value, not testing for one (==).

then @offspring[i]= 9

@offspring.inspect simply prints out a line of 9s.

Yep, that’s what it’ll do :slight_smile:

HTH!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs