Results 1 to 7 of 7
  1. #1
    Corallus is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default 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:

    Java Code:
    BacktrackSolver solver = new BacktrackSolver(puzzle);
    maintainer = new Maintainer();
    solver.setListener(maintainer);
    solver.findAll();
    nextSolutionButton.setEnabled(true);
    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:
    listener.solutionFound(nSolutionsFound, puzzle);
    solutionsFound in the Maintainer looks like this:

    Java Code:
    public void solutionFound(int solutionNumber, Puzzle puzzle) {
        solutions.add(puzzle);
    }
    where solutions is an ArrayList with all solutions.

    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:

    Java Code:
    puzzle = maintainer.nextSolution();
    Which returns the solved puzzle, but the puzzle is not solved anymore, so somehow the puzzle was not saved well?

    Anyone know why this is and maybe how I can solve this?

    Thanks in advance
    Last edited by Corallus; 07-30-2011 at 05:05 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.

  3. #3
    Corallus is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default

    Thanks for the quick response! I edited my post to be more precise

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default

    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Corallus is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default

    So I have to implement a clone method for all the classes of the model used by the puzzle?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.

  7. #7
    Corallus is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default

    Thanks for helping me out. I am probably going to solve the issue by serializing the puzzle.

Similar Threads

  1. Maintain focus on JFrame
    By nik_meback in forum AWT / Swing
    Replies: 2
    Last Post: 12-15-2010, 01:39 PM
  2. How to maintain scrollbar position on postback?
    By marathaWarrior in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-02-2009, 07:35 AM
  3. can we maintain single session from applet?
    By kuppi in forum Java Applets
    Replies: 7
    Last Post: 11-04-2008, 05:24 PM
  4. how to maintain cookies throughout the website
    By lukky in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-28-2008, 07:33 AM
  5. Lack of memory maintain to Website
    By 82rathi.angara in forum Advanced Java
    Replies: 3
    Last Post: 06-13-2008, 02:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •