Values Changing Mysteriously
Author |
Message |
BlakeJustBlake
|
Posted: Thu Apr 17, 2008 2:37 pm Post subject: Values Changing Mysteriously |
|
|
So, I usually don't like to post my programming problems for homework assignments, but this one just has me stumped at the moment. All I'm looking for is help solving this one problem, if you think you see a way I change or redo my program to solve my homework assignment better, don't tell me, I don't want to know, because I'm not looking for solutions to my assignment, I want to learn that part on my own. So please only give help as far as my described problem or design tips.
Here's a summary of the problem:
I've got a solver for a puzzle where there are four cubes on a rod with four sides on each cube. Each side (top bottom front back) has a color, solving the puzzle means that each cube has been rotated so that all the top sides of the cubes have different colors, all the bottom sides have different colors, etc. So there are several solutions and my program finds 5 solutions. So what I have is a two dimensional array to store all the solutions and a one dimensional array to send through the solving method and then after a solution is found be added to the two dimensional solution array. The solution array is like this: private List<String>[][] solutions= new List[5][4]; so that it's like solutions[(solution number)][(box in the solution number)] and each box contains an ArrayList of four Strings. The solution array is only called at two points; to add a solution to it and to compare it to the array being processed by the solve method so that it can see if the solution has already been found. So here's the problem: When the program compares the array being processed to the array of solutions already found if it finds that the solution is already found it will rotate a few of the boxes so that it will change up the way the boxes are arranged so that the program can solve it in a different way. But whenever it rotates the boxes in the array being processed, for some reason it also rotates the solution in the solutions array so that since it's sending the already solved solution back through the solve method it should see that it's already a solution, change it up, send it through the solve method. But, since the solutions array changes with it, when it goes back through the Solve method (since I'm doing it recursively) it will check to see if it's a solution again and it will always say it's already a solution and have an infinite loop giving a StackOverflowError. So what I'm trying to find is why the solutions array is changing with the array being processed, since I can't find anything referencing the solutions array anywhere where it should change anything since it's not returning anything or directly changing a value related to the solutions array.
Here's the relevant parts of the source code, there are other methods, but they don't really deal with this part:
code: | class insanitySolver {
private String Title;
private List<String>[] boxes;
private List<String>[][] solutions = new List[5][4];
private int k;
private int rotations = 0;
private int stuckBox;
private boolean noSolutionOutput = false;
public insanitySolver(String name, List<String>[] boxes) {
Title = name;
this.boxes = boxes;
for(k = 0; k < 5; k++) {
boxes = Solve(this.boxes, 2);
for(int i = 3;i>=0;i--) {
solutions[k][i]=boxes[i];
}
}
if(!noSolutionOutput) {
getSolutions();
}
}
private List<String>[] rotateBox (List<String>[] box,int current) {
box[current].add(box[current].get(0));
box[current].remove(0);
return box;
}//Rotates box by removing the first element and adding it to the end of the List.
private boolean solutionFound(List<String>[] box) {
boolean found = true;
for(int i = k-1;i>=0;i--) {
for(int j = 0;j<4;j++) {
if(!box[j].equals(solutions[i][j]))
found = false;
}
}
return found;
}//Checks to see if the current setup is already a solution before it calls it a solution again.
public List<String>[] Solve(List<String>[] cubes, int currentBox) {
if(k > 0 && currentBox == 2 && solutionFound(cubes)) {
cubes = rotateBox(cubes, currentBox + 1);
cubes = rotateBox(cubes, currentBox);
cubes = rotateBox(cubes, currentBox);
}
try{
boolean duplicated = isDuplicated(cubes, currentBox);
if(currentBox==0&&!duplicated) {
return cubes;
}
if(duplicated) {
if(rotations == 3){
rotations = 0;
return Solve(rotateBox(cubes, stuckBox), stuckBox);}
rotations++;
return Solve(rotateBox(cubes,currentBox), currentBox);
}
if(!duplicated) {
rotations = 0;
return Solve(cubes,currentBox-1);
}
}
catch(StackOverflowError e) {
if(!noSolutionOutput) {
System.out.print("No Solution\n");
noSolutionOutput = true;
}
}
return cubes;
} |
And here's the data being sent through it:
code: | This is the first test.
White, Blue, Yellow, Red
Green, Blue, White, Blue
White, Yellow, Green, Green
Blue, Red, Yellow, Red
|
And just so you can see how the data is processed in, here's the main method that calls the insanitySolver class:
code: | public class eakinProg5 {
public static void main(String[] args) throws FileNotFoundException {
Scanner retrieve = new Scanner(new File("test.txt"));
Scanner retrieveLines;
List<String>[] boxes = new List[4];
String title;
for(int i = 0;i<=3;i++) {
boxes[i] = new ArrayList<String>(4);
}
while(retrieve.hasNextLine()) {
title = retrieve.nextLine();
System.out.println(title);
for(int i = 3;i>=0;i--) {
retrieveLines = new Scanner(retrieve.nextLine()).useDelimiter("\\x2C\\s");
//System.out.print(i);
for(int j = 0;j<=3;j++) {
//System.out.print(j);
boxes[i].add(retrieveLines.next());
//System.out.print(boxes[i].get(j));
}
//System.out.print("\n");
}
insanitySolver solveSimple = new insanitySolver(title, boxes);
}
}
}
|
If you want the entire source code to run through a compiler or any clarification on what certain parts of code do or just any clarification on anything whatsoever, just ask. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
OneOffDriveByPoster
|
Posted: Thu Apr 17, 2008 2:44 pm Post subject: Re: Values Changing Mysteriously |
|
|
You should create a new copy when storing to the solutions array.
If you have
SomeClass a = b;
a.someVal = 0;
b.someVal = 1;
a.someVal would be 1. This is because the variables a and b refer to the same object. |
|
|
|
|
![](images/spacer.gif) |
BlakeJustBlake
|
Posted: Thu Apr 17, 2008 5:37 pm Post subject: Re: Values Changing Mysteriously |
|
|
I ended up scrapping the whole storing solutions thing and now I'm just trying to make it form unique solutions each time. |
|
|
|
|
![](images/spacer.gif) |
|
|