I asked a similar question yesterday and got a brilliant answer, but unsure how I could adapt these methods.
The methods perform a BFS on a state and then store the values in a multi-dimensional array.
You can see that the code is repeating itself, just with different array dimensions.
Method 1:
public void bfs2(int[][][] three, PDB15Tile.State s, int[] storedNums, String fileName) throws IOException {
Queue<PDB15Tile.State> q = new LinkedList<>();
int[] positions = new int[storedNums.length];
for (int i = 0; i < storedNums.length; i++) {
positions[i] = getPatternPosition(storedNums[i], s.getState());
}
three[positions[0]][positions[1]][positions[2]] = 0;
q.add(s);
PDB15Tile.State current;
while (!q.isEmpty()) {
current = q.poll();
// System.out.println(Arrays.toString(current.getState()));
for (PDB15Tile.State neighbour : current.findNeighbours2()) {
// System.out.println(Arrays.toString(neighbour.getState()));
for (int i = 0; i < storedNums.length; i++) {
positions[i] = getPatternPosition(storedNums[i], neighbour.getState());
}
if (three[positions[0]][positions[1]][positions[2]] == 0) {
three[positions[0]][positions[1]][positions[2]] = neighbour.getH();
q.add(neighbour);
}
}
}
serializeArrayToFile(three, fileName);
}
Method 2:
public void bfs2(int[][][][][][] six, PDB15Tile.State s, int[] storedNums, String fileName) throws IOException {
Queue<PDB15Tile.State> q = new LinkedList<>();
int[] positions = new int[storedNums.length];
for (int i = 0; i < storedNums.length; i++) {
positions[i] = getPatternPosition(storedNums[i], s.getState());
}
six[positions[0]][positions[1]][positions[2]][positions[3]][positions[4]][positions[5]] = 0;
q.add(s);
PDB15Tile.State current;
while (!q.isEmpty()) {
current = q.poll();
// System.out.println(Arrays.toString(current.getState()));
for (PDB15Tile.State neighbour : current.findNeighbours2()) {
// System.out.println(Arrays.toString(neighbour.getState()));
for (int i = 0; i < storedNums.length; i++) {
positions[i] = getPatternPosition(storedNums[i], neighbour.getState());
}
//System.out.println(six[positions[0]][positions[1]][positions[2]][positions[3]][positions[4]][positions[5]]);
if (six[positions[0]][positions[1]][positions[2]][positions[3]][positions[4]][positions[5]] == 0) {
six[positions[0]][positions[1]][positions[2]][positions[3]][positions[4]][positions[5]] = neighbour.getH();
q.add(neighbour);
}
}
}
serializeArrayToFile(six, fileName);
}
How do I determine the number of dimensions of the object that is passed in and then manipulate the array accordingly?
three
andsix
arrays? I don't see any iterating over the 3 or 6 dimensions. You are only ever retrieving values from fixed indexes, which makes me wonder if your choice of data structures is correct. \$\endgroup\$