Click here to Skip to main content
14,693,117 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Using informed and uninformed search algorithms to solve 8-puzzl

this is my problem


The problem that I am facing now with the above code is that whenever the program pops the stack it does not tally with the value that has been push earlier in the swap function method (Puzzle Class).e in java

What I have tried:

public class Main {

	public static void main(String[] args) {

        int startState[][] = {{6,2,5}, {1,8,0}, {7,4,3}}, goalState[][] = {{7,6,5}, {8,0,4}, {1,2,3}};

        System.out.println("Solving puzzle using DFS...");
        DFS dfs = new DFS(startState, goalState);
        dfs.search();
    }
	}
<pre>public class Node {
	 int state[][] = new int[3][3];
	    
	    public Node() {
	    }
	    
	    public Node(int state[][]) {
	        this.state = state;
	    }
	    
	    public int getNum(int posX, int posY) {
	        return state[posX][posY];
	    }
	    
	    public void setNum(int num, int x, int y) {
	        state[x][y] = num;
	    }
	    
	    public int getX(int num) {
	        int positionX = 0;

	        for (int x = 0; x < 3; x++) {
	            for (int y = 0; y < 3; y++) {
	                if (state[x][y] == num) {
	                    positionX = x;
	                    break;
	                }
	            }
	        }

	        return positionX;
	    }
	    
	    public int getY(int num) {
	        int positionY = 0;

	        for (int x = 0; x < 3; x++) {
	            for (int y = 0; y < 3; y++) {
	                if (state[x][y] == num) {
	                    positionY = y;
	                    break;
	                }
	            }
	        }

	        return positionY;
	    }
	    
	    public void getCoordinate(int coordinate[], int num) {
	        for (int x = 0; x < 3; x++) {
	            for (int y = 0; y < 3; y++) {
	                if (state[x][y] == num) {
	                    coordinate[0] = x;
	                    coordinate[1] = y;
	                    break;
	                }
	            }
	        }
	    }
	    
	    public void getState(int state[][]) {
	        state = this.state;
	    }
	    
	    public String toString() {
	        String output = "";

	        for (int x = 0; x < 3; x++) {
	            for (int y = 0; y < 3; y++) {
	                output += state[x][y];
	            }
	            output += "\n";
	        }

	        return output;
	    }
}
<pre>import java.util.Stack;
import java.util.ArrayList;

public class DFS {
    private int moveCounter = 0;
    private Stack<Node> fringe = new Stack<Node>();
    private Node startState, goalState, currentState = new Node();
    private ArrayList<Node> closed = new ArrayList<Node>();

    public DFS(int start[][], int goal[][]) {
        startState = new Node(start);
        goalState = new Node(goal);
        fringe.push(startState);
    }

    private void succesor(Node currentState) {
        Node newState = new Node();
        puzzle p = new puzzle();

        newState = p.moveUp(currentState);

        if (newState != null) {
            fringe.push(newState);
        }

        newState = p.moveRight(currentState);

        if (newState != null) {
            fringe.push(newState);
        }

        newState = p.moveDown(currentState);

        if (newState != null) {
            fringe.push(newState);
        }

        newState = p.moveLeft(currentState);

        if (newState != null) {
            fringe.push(newState);
        }
    }

    public void search() {
        boolean found = false, exist = false, checkElement = true;
        Node tmpNode = new Node();

        currentState = fringe.pop();
        closed.add(currentState);
        succesor(currentState);

        for (int x = 1; x < 2 ; x++) {
            while (!found) {
                try {
                    currentState = fringe.pop();

                    for (int i = 0; i < closed.size(); i++) {
                        tmpNode = closed.get(i);

                        if (currentState.toString().equals(tmpNode.toString())) {
                            exist = true;
                            break;
                        }                        
                    }

                    if (!exist) {
                        moveCounter++;

                        if (currentState.getX(x) == goalState.getX(x) && currentState.getY(x) == goalState.getY(x)) {
                            found = true;
                            break;
                        }
                        else {
                            closed.add(currentState);
                            succesor(currentState);
                        }
                    }
                }
                catch (Exception e) {
                    System.out.println("No solutions found!");
                    break;
                }
            }
        }

        System.out.println("Number of step(s): " + moveCounter);
    }
}
<pre>public class puzzle {
	   public void Puzzle() {
	    }
	    
	    private void swap(Node newState, int newPosition[]) {
	        int emptyTile[] = new int[2], tmpNum;                

	        newState.getCoordinate(emptyTile, 0);
	        tmpNum = newState.getNum(newPosition[0], newPosition[1]);
	        
	        newState.setNum(0, newPosition[0], newPosition[1]);
	        newState.setNum(tmpNum, emptyTile[0], emptyTile[1]);
	    }
	        
	    public Node moveUp(Node currentState) {
	        Node newState = new Node();
	        newState = null;
	        int newPosition[] = new int[2];

	        if (currentState.getX(0) - 1 >= 0) {
	            newState = currentState;
	            newPosition[0] = newState.getX(0) - 1;
	            newPosition[1] = newState.getY(0);

	            swap(newState, newPosition);
	        }

	        return newState;
	    }        
	        
	    public Node moveRight(Node currentState) {
	        Node newState = new Node();
	        newState = null;
	        int newPosition[] = new int[2];

	        if (currentState.getY(0) + 1 <= 2) {
	            newState = currentState;
	            newPosition[0] = newState.getX(0);
	            newPosition[1] = newState.getY(0) + 1;

	            swap(newState, newPosition);
	        }

	        return newState;
	    }        
	        
	    public Node moveDown(Node currentState) {
	        Node newState = new Node();
	        newState = null;
	        int newPosition[] = new int[2];

	        if (currentState.getX(0) + 1 <= 2) {
	            newState = currentState;
	            newPosition[0] = newState.getX(0) + 1;
	            newPosition[1] = newState.getY(0);

	            swap(newState, newPosition);
	        }

	        return newState;
	    }        
	        
	    public Node moveLeft(Node currentState) {
	        Node newState = new Node();
	        newState = null;
	        int newPosition[] = new int[2];

	        if (currentState.getY(0) - 1 >= 0) {
	            newState = currentState;
	            newPosition[0] = newState.getX(0);
	            newPosition[1] = newState.getY(0) - 1;

	            swap(newState, newPosition);
	        }

	        return newState;
	    }      
}
Posted
Updated 18-Mar-20 0:24am
v2
Comments
Richard MacCutchan 18-Mar-20 4:22am
   
And? Where exactly does the error occur, and what are the complete details?

1 solution

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900