I'm attempting to write a sudoku solver in ruby and I've run into an issue. Consider the following code:
@grid = Array.new(9,Array.new(9,Cell.new))
where Cell
is defined as:
class Cell
def initialize
@value = 0
@possibles = Array (1..9)
end
attr_accessor :value, :possibles
end
When I initialize my 9x9 @grid
2D array, it prints out the values
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
which is what I expect. However, when I try running something like @grid[5][5].value = 7
then the @grid
prints out the values
7 7 7 7 7 7 7 7 7
7 7 7 7 7 7 7 7 7
7 7 7 7 7 7 7 7 7
7 7 7 7 7 7 7 7 7
7 7 7 7 7 7 7 7 7
7 7 7 7 7 7 7 7 7
7 7 7 7 7 7 7 7 7
7 7 7 7 7 7 7 7 7
7 7 7 7 7 7 7 7 7
I realize that my 9x9 @grid
2D array is full of the same instance of Cell
What is the syntax to create a 2D array of unique instances so that I can manipulate them individually?