Making an array of arrays?

J

Jen

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 = @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 = "_"
then @offspring= 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.
 
H

Hassan Schroeder

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 = @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 = "_"


The line above is *assigning* (=) a value, not testing for one (==).
then @offspring= 9

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

Yep, that's what it'll do :)

HTH!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top