I’m trying to make an array of arrays, but my code is not behaving as I
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
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
##Loop through @offspring and where ever there is “" put a random
i = 0
for i in 0…80
if @offspring[i] = "”
then @offspring[i]= 9
#For debugging with the test puzzle
@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
Many thanks in advance,