Results 1 to 7 of 7
- 07-30-2011, 04:53 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 4
- Rep Power
- 0
Maintain solutions found using a listener
Hi,
For my study I have to create a puzzle in java, which is almost done, but I have come across 1 problem.
I have a button in the GUI, which says compute solutions which activates this code:
The maintainer has to maintain all the solutions found so far. When a solution is found in the BackTrackSolver the puzzle (solved) is sent to the listener (Maintainer in this case) by the following code:Java Code:BacktrackSolver solver = new BacktrackSolver(puzzle); maintainer = new Maintainer(); solver.setListener(maintainer); solver.findAll(); nextSolutionButton.setEnabled(true);
solutionsFound in the Maintainer looks like this:Java Code:listener.solutionFound(nSolutionsFound, puzzle);
where solutions is an ArrayList with all solutions.Java Code:public void solutionFound(int solutionNumber, Puzzle puzzle) { solutions.add(puzzle); }
When I textually print the puzzle in the solutionsFound method, the puzzle is printed as being solved (so correct), but when I try to get a solution form the ArrayList in another method of the Maintainer, I get the puzzle, but unsolved. So for instance in the GUI I have a "next solution" button with:
Which returns the solved puzzle, but the puzzle is not solved anymore, so somehow the puzzle was not saved well?Java Code:puzzle = maintainer.nextSolution();
Anyone know why this is and maybe how I can solve this?
Thanks in advanceLast edited by Corallus; 07-30-2011 at 05:05 PM.
-
It suggests to me that perhaps you have more than one Maintainer object, but I'm not sure we can tell precisely what is wrong based on the information presented. You may need to tell us and show us more.
Oh, and welcome to Java-Forums.org!! :)Last edited by Fubarable; 07-30-2011 at 05:02 PM.
- 07-30-2011, 05:05 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 4
- Rep Power
- 0
Thanks for the quick response! I edited my post to be more precise
- 07-30-2011, 05:13 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
If your backtracking solver modifies your puzzle you should save a copy of the puzzle in your Maintainer object. Now it seems you're just saving a reference to a puzzle.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-30-2011, 05:30 PM #5
Member
- Join Date
- Jul 2011
- Posts
- 4
- Rep Power
- 0
So I have to implement a clone method for all the classes of the model used by the puzzle?
-
I'm not sure that this will fully solve your problem. I still suspect that you have more than one Maintainer object, that one is filled with solutions and the other not, but again, I can't tell based on the code posted.
- 07-30-2011, 06:05 PM #7
Member
- Join Date
- Jul 2011
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Maintain focus on JFrame
By nik_meback in forum AWT / SwingReplies: 2Last Post: 12-15-2010, 01:39 PM -
How to maintain scrollbar position on postback?
By marathaWarrior in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 01-02-2009, 07:35 AM -
can we maintain single session from applet?
By kuppi in forum Java AppletsReplies: 7Last Post: 11-04-2008, 05:24 PM -
how to maintain cookies throughout the website
By lukky in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 08-28-2008, 07:33 AM -
Lack of memory maintain to Website
By 82rathi.angara in forum Advanced JavaReplies: 3Last Post: 06-13-2008, 02:45 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks