Results 1 to 7 of 7
  1. #1
    sonofshirt is offline Member
    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0

    Default Problem with method updating node I don't want it to...

    Here's my problem. In the code below, I'm trying to generate successors to a starting node that contains a 9 int array that represents a 9 square puzzle board, with the "0" representing the blank space.

    The key method is 'generateSuccessor'. I'm trying to generate all the possible moves (at least 2, up to 4) from a given starting position. In any given puzzle, there can only be one blank space. My problem is that for some reason, when I swap the values of the array, it updates the reference array in the PuzzleNode. Which throws off the second swap, and so on. So I end up with an array with more than 1 zero, which is NOT what I want. I can't figure out how to dereference that array so it just updates my temporary array (swapArray). I've been spinning my wheels on this for a handful of hours now, and I need some help. Thanks!

    Java Code:
    package gensuccessorstest;
    
    import java.util.PriorityQueue;
    import java.util.Comparator;
    
    public class Main {
    
        public static void main(String[] args) {
    
            PriorityQueue<PuzzleNode> tempList = new PriorityQueue<PuzzleNode>
                (1,
                new Comparator<PuzzleNode>(){
                    public int compare(PuzzleNode a, PuzzleNode b){
                        if (a.getPathCost() > b.getPathCost())
                            return 1;
                        else if (a.getPathCost() < b.getPathCost())
                            return -1;
                        else
                            return 0;
                        }
                    }
                );
            PuzzleNode testNode = new PuzzleNode();
            int[] testArray = new int[9];
            // creating a simple array with the same numbers as position in array
            for (int i=0; i<testArray.length; i++){
                testArray[i] = i;
            }
            // set some parameters for the test PuzzleNode
            testNode.setPuzzle(testArray);
            testNode.setDirection("start");
            testNode.setG(0);
            testNode.setH(1);
            testNode.updatePathCost();
            generateSuccessors(testNode, tempList);
        }
    
            // Should pass in the open list, along the node with the lowest value (next one to search from)
        public static void generateSuccessors(PuzzleNode n, PriorityQueue pq){
            PuzzleNode temp = new PuzzleNode();
            temp = n;
            int[] swapArray = temp.getPuzzle();
            int position = 0;
            // Loop through the array to get the position of the open box
            for (int i = 0; i < temp.getPuzzle().length; i++){
                if (temp.getPuzzle()[i] == 0){
                    position = i;
                    break;
                }
            }
            // Based on the position of the open box, generate the successor nodes
            if (position == 0){
                // update the node info, add n as a parent, and add to the tempQueue
                swapArray = positionSwap(position, 1, swapArray);
                pq.add(createSuccessorNode(swapArray, n, "right"));
                // reset array to original state
                swapArray = temp.getPuzzle();
                // update for 2nd possible position (from 0)
                swapArray = positionSwap(position, 3, swapArray);
                pq.add(createSuccessorNode(swapArray, n, "down"));
                swapArray = n.getPuzzle();
            } else if (position == 1){
                swapArray = positionSwap(position, 0, swapArray);
                pq.add(createSuccessorNode(swapArray, n, "left"));
                swapArray = n.getPuzzle();
                swapArray = positionSwap(position, 2, swapArray);
                pq.add(createSuccessorNode(swapArray, n, "right"));
                swapArray = n.getPuzzle();
                swapArray = positionSwap(position, 4, swapArray);
                pq.add(createSuccessorNode(swapArray, n, "down"));
            } else if (position == 2) {
                swapArray = positionSwap(position, 1, swapArray);
                pq.add(createSuccessorNode(swapArray, n, "left"));
                swapArray = n.getPuzzle();
                swapArray = positionSwap(position, 5, swapArray);
                pq.add(createSuccessorNode(swapArray, n, "down"));
            } else if (position == 3){
                swapArray = positionSwap(position, 0, swapArray);
                pq.add(createSuccessorNode(swapArray, n, "up"));
                swapArray = n.getPuzzle();
                swapArray = positionSwap(position, 4, swapArray);
                pq.add(createSuccessorNode(swapArray, n, "right"));
                swapArray = n.getPuzzle();
                swapArray = positionSwap(position, 6, swapArray);
                pq.add(createSuccessorNode(swapArray, n, "down"));
            } else if (position == 4){
                swapArray = positionSwap(position, 1, swapArray);
                pq.add(createSuccessorNode(swapArray, n, "up"));
                swapArray = n.getPuzzle();
                swapArray = positionSwap(position, 3, swapArray);
                pq.add(createSuccessorNode(swapArray, n, "left"));
                swapArray = n.getPuzzle();
                swapArray = positionSwap(position, 5, swapArray);
                pq.add(createSuccessorNode(swapArray, n, "right"));
                swapArray = n.getPuzzle();
                swapArray = positionSwap(position, 7, swapArray);
                pq.add(createSuccessorNode(swapArray, n, "down"));
            } else if (position == 5){
                swapArray = positionSwap(position, 2, swapArray);
                pq.add(createSuccessorNode(swapArray, n, "up"));
                swapArray = n.getPuzzle();
                swapArray = positionSwap(position, 4, swapArray);
                pq.add(createSuccessorNode(swapArray, n, "left"));
                swapArray = n.getPuzzle();
                swapArray = positionSwap(position, 8, swapArray);
                pq.add(createSuccessorNode(swapArray, n, "down"));
            } else if (position == 6){
                swapArray = positionSwap(position, 3, swapArray);
                pq.add(createSuccessorNode(swapArray, n, "up"));
                swapArray = n.getPuzzle();
                swapArray = positionSwap(position, 6, swapArray);
                pq.add(createSuccessorNode(swapArray, n, "right"));
            } else if (position ==7){
                swapArray = positionSwap(position, 4, swapArray);
                pq.add(createSuccessorNode(swapArray, n, "up"));
                swapArray = n.getPuzzle();
                swapArray = positionSwap(position, 6, swapArray);
                pq.add(createSuccessorNode(swapArray, n, "left"));
                swapArray = n.getPuzzle();
                swapArray = positionSwap(position, 8, swapArray);
                pq.add(createSuccessorNode(swapArray, n, "right"));
                swapArray = n.getPuzzle();
            } else if (position == 8){
                swapArray = positionSwap(position, 5, swapArray);
                pq.add(createSuccessorNode(swapArray, n, "up"));
                swapArray = n.getPuzzle();
                swapArray = positionSwap(position, 7, swapArray);
                pq.add(createSuccessorNode(swapArray, n, "left"));
            }
        }
    
        public static PuzzleNode createSuccessorNode(int[] puzzle, PuzzleNode n, String direction){
            PuzzleNode newNode = new PuzzleNode(puzzle, n);
            newNode.setH(n.getH());
            newNode.setG(n.getG()+1);
            newNode.updatePathCost();
            newNode.setDirection(direction);
            newNode.printPuzzle(newNode.getPuzzle());
            return newNode;
        }
    
        public static int[] positionSwap(int blank, int swapInt, int[] array){
            int[] tmp = array;
            tmp[blank] = tmp[swapInt];
            tmp[swapInt] = 0;
            return tmp;
        }
    
    public static class PuzzleNode {
        private int[] puzzleArray;      //representation of the puzzle tiles
        private int hCost;              //represents h(n) in A*
        private int gCost;              //represents g(n) in A*
        private int pathCost;           //represents f(n) in A*
        private PuzzleNode parentNode;  //parent node of the current node
        private String direction;       //tells which direction parent node is
    
        public PuzzleNode(){
        }
    
        public PuzzleNode(int[] pArray, PuzzleNode parentNode){
            this.puzzleArray = pArray;
            this.parentNode = parentNode;
        }
    
        public int[] getPuzzle(){
            return this.puzzleArray;
        }
    
        public void setPuzzle(int[] puzzleArray){
            this.puzzleArray = puzzleArray;
        }
    
        public int getPathCost(){
            return this.pathCost;
        }
    
        public void setPathCost(int pathCost){
            this.pathCost = pathCost;
        }
    
        public int getH(){
            return this.hCost;
        }
    
        public void setH(int hCost){
            this.hCost = hCost;
        }
    
        public int getG(){
            return this.gCost;
        }
    
        public void setG(int gCost){
            this.gCost = gCost;
        }
    
        public void updatePathCost(){
            this.pathCost = this.hCost + this.gCost;
        }
    
        public PuzzleNode getParentNode(){
            return this.parentNode;
        }
    
        public void setParentNode(PuzzleNode pNode){
            this.parentNode = pNode;
        }
    
        public void setDirection(String d){
            this.direction = d;
        }
    
        public String getDirection(){
            return this.direction;
        }
    
        public void printDirectionChain(){
            PuzzleNode tempNode = this;
            while (tempNode.parentNode != null){
                System.out.print(tempNode.direction + " ");
                System.out.println();
                tempNode = tempNode.parentNode;
            }
        }
    
        public void printPuzzle(int[] board){
            System.out.println(" ------------- ");
            // Print out the first row
            for (int i=0; i < 3; i++){
                System.out.print(" | " + board[i]);
            }
            System.out.println(" |");
            System.out.println(" ------------- ");
            // Print out the second row
            for (int i=3; i < 6; i++){
                System.out.print(" | " + board[i]);
            }
            System.out.println(" |");
            System.out.println(" ------------- ");
            // Print out the third row
            for (int i=6; i < 9; i++){
                System.out.print(" | " + board[i]);
            }
            System.out.println(" |");
            System.out.println(" ------------- ");
        }
    }
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Did you try to debug and see what's really happening in your code. It's really hard to go through the complete code, it's too long. So try to take some efforts like that.

  3. #3
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    All you need to do is clone the array, not pass its reference. Just run this sample code to see what I mean:
    Java Code:
    public class ArrTest {
      public static void main(String[] args) {
        int[] arr1 = new int[10], arr2, arr3 = new int[10];
        for(int i = 0; i < 10; i++)
          arr1[i] = i;
        arr2 = arr1; //the two int[] variables point to the same array!!!
        arr2[5] = 4;
        System.out.println(arr1[5] + " " + arr2[5]); //even though arr1 wasn't changed explicitly, by setting arr2, you modified arr1 as well
        for(int i = 0; i < 10; i++)
          arr3[i] = arr1[i];
        arr3[7] = 8;
        System.out.println(arr1[7] + " " + arr3[7]); //this time, only arr3 is changed
      }
    }
    Now, this solution only works on arrays of primitives, if your array elements are objects, you'd need to clone them, or the same problem would apply.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

  5. #5
    sonofshirt is offline Member
    Join Date
    Sep 2010
    Posts
    6
    Rep Power
    0

    Default

    I appreciate the responses. I created a clone/copy of the array and that method started working like I wanted it to. Now just to fix the others in my program. :)

    I apologize if I offended anyone by cross-posting (at code-ranch). I didn't realize there was a code against that, and just wanted to get perspectives of folks that may visit different forums. I'll be forthright about doing so in the future.

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

    Default

    Quote Originally Posted by sonofshirt View Post
    I apologize if I offended anyone by cross-posting (at code-ranch). I didn't realize there was a code against that, and just wanted to get perspectives of folks that may visit different forums. I'll be forthright about doing so in the future.
    Thank you! :)

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Problem with updating empty JTable
    By byubi in forum AWT / Swing
    Replies: 1
    Last Post: 05-15-2010, 08:31 AM
  2. Problem with updating JTable
    By kwaspl in forum New To Java
    Replies: 2
    Last Post: 12-20-2009, 10:41 PM
  3. Replies: 0
    Last Post: 04-20-2009, 06:26 PM
  4. XML Node.getNodeValue Problem
    By mindscape777 in forum XML
    Replies: 1
    Last Post: 01-11-2009, 02:22 PM
  5. Replies: 29
    Last Post: 09-25-2008, 07:55 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
  •