Randomised objects are assigning themselves to more than one array location
Posted
by
Thaddeus Aid
on Stack Overflow
See other posts from Stack Overflow
or by Thaddeus Aid
Published on 2011-03-06T23:50:16Z
Indexed on
2011/03/07
0:10 UTC
Read the original article
Hit count: 174
this.size = 9;
this.populationSize = 10;
Random rand = new Random();
Integer[][] easy1 = new Integer[size][size];
easy1 = this.initializeEasy1(easy1);
this.sudokuArray = new Sudoku[this.populationSize];
for (int i = 0; i < this.sudokuArray.length; i++){
long seed = rand.nextLong();
System.out.println("" + seed);
this.sudokuArray[i] = new Sudoku(easy1, this.size, seed);
}
I am building an evolutionary sudoku solver and I am having a problem where the last Sudoku object is overwriting all the other objects in the array. Where in the code did I mess up?
/edit here is the constructor of the class
public Sudoku(Integer[][] givensGrid, int s, long seed){
this.size = s;
this.givens = givensGrid;
this.grid = this.givens.clone();
Random rand = new Random(seed);
System.out.println("Random " + rand.nextInt());
// step though each row of the grid
for (int i = 0; i < size; i++){
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers = this.makeNumbers(numbers);
// step through each column to find the givens and remove from numbers
for (int j = 0; j < size; j++){
if (this.grid[i][j] != 0){
numbers.remove(this.grid[i][j]);
}
}
// go back through the row and assign the numbers randomly
for (int j = 0; j < size; j++){
if (this.grid[i][j] == 0){
int r = rand.nextInt(numbers.size());
this.grid[i][j] = numbers.get(r);
numbers.remove(r);
}
}
}
System.out.println("=============");
System.out.println(this.toString());
}
© Stack Overflow or respective owner