Hey everybody,
i wanna use the array "intArray" in the class eightPuzzleAStarDemo() as initial state but i get a cannot use it.
Does anybody has a hint for me?
Is it because it is not in the same package?
Thank you

package aaa.mayerp.projectone;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Intro {
	public static void main(String[] args) throws IOException {

        String[] temp;


        InputStreamReader inputStream = new InputStreamReader(System.in);
        BufferedReader keyboard = new BufferedReader(inputStream);

        System.out.println("Enter eight numbers from 0 to 9");

        String initialstate = keyboard.readLine();
        String delimiter = ",";
        temp = initialstate.split(delimiter);
        int[] intArray = new int[temp.length];

        for (int i = 0; i < temp.length; i++) {

            for (int x = 0; x < temp.length; x++) {
                try {
                    intArray[x] = Integer.parseInt(temp[x]);
                } catch(NumberFormatException e) {
                    System.out.println("Invalid int encountered:- " + temp[x]);
               }
            }
        }

        System.out.println("Numbers array contents: ");
        for (int num: intArray) {
//            System.out.println("Number: [" + num + "]");
            System.out.println(num);
           
        }


    }
}
package aima.search.demos;

import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import aima.search.eightpuzzle.EightPuzzleBoard;
import aima.search.eightpuzzle.EightPuzzleGoalTest;
import aima.search.eightpuzzle.EightPuzzleSuccessorFunction;
import aima.search.eightpuzzle.ManhattanHeuristicFunction;
import aima.search.eightpuzzle.MisplacedTilleHeuristicFunction;
import aima.search.framework.GraphSearch;
import aima.search.framework.Problem;
import aima.search.framework.Search;
import aima.search.framework.SearchAgent;
import aima.search.informed.AStarSearch;
import aima.search.informed.GreedyBestFirstSearch;
import aima.search.informed.SimulatedAnnealingSearch;
import aima.search.uninformed.DepthLimitedSearch;
import aima.search.uninformed.IterativeDeepeningSearch;

/**
 * @author Ravi Mohan
 * 
 */

public class EightPuzzleDemo {
	static EightPuzzleBoard boardWithThreeMoveSolution = new EightPuzzleBoard(
			new int[] { 1, 2, 5, 3, 4, 0, 6, 7, 8 });;

	static EightPuzzleBoard random1 = new EightPuzzleBoard(new int[] { 1, 4, 2,
			7, 5, 8, 3, 0, 6 });

	static EightPuzzleBoard extreme = new EightPuzzleBoard(new int[] { 0, 8, 7,
			6, 5, 4, 3, 2, 1 });

	public static void main(String[] args) {
//		eightPuzzleDLSDemo();
//		eightPuzzleIDLSDemo();
//		eightPuzzleGreedyBestFirstDemo();
//		eightPuzzleGreedyBestFirstManhattanDemo();
		eightPuzzleAStarDemo();
//		eightPuzzleAStarManhattanDemo();
//		eightPuzzleSimulatedAnnealingDemo();
	}

	private static void eightPuzzleDLSDemo() {
		System.out.println("\nEightPuzzleDemo recursive DLS -->");
		try {
			Problem problem = new Problem(random1,
					new EightPuzzleSuccessorFunction(),
					new EightPuzzleGoalTest());
			Search search = new DepthLimitedSearch(9);
			SearchAgent agent = new SearchAgent(problem, search);
			printActions(agent.getActions());
			printInstrumentation(agent.getInstrumentation());
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private static void eightPuzzleIDLSDemo() {
		System.out.println("\nEightPuzzleDemo Iterative DLS -->");
		try {
			Problem problem = new Problem(random1,
					new EightPuzzleSuccessorFunction(),
					new EightPuzzleGoalTest());
			Search search = new IterativeDeepeningSearch();
			SearchAgent agent = new SearchAgent(problem, search);
			printActions(agent.getActions());
			printInstrumentation(agent.getInstrumentation());
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private static void eightPuzzleGreedyBestFirstDemo() {
		System.out
				.println("\nEightPuzzleDemo Greedy Best First Search (MisplacedTileHeursitic)-->");
		try {
			Problem problem = new Problem(boardWithThreeMoveSolution,
					new EightPuzzleSuccessorFunction(),
					new EightPuzzleGoalTest(),
					new MisplacedTilleHeuristicFunction());
			Search search = new GreedyBestFirstSearch(new GraphSearch());
			SearchAgent agent = new SearchAgent(problem, search);
			printActions(agent.getActions());
			printInstrumentation(agent.getInstrumentation());
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private static void eightPuzzleGreedyBestFirstManhattanDemo() {
		System.out
				.println("\nEightPuzzleDemo Greedy Best First Search (ManhattanHeursitic)-->");
		try {
			Problem problem = new Problem(boardWithThreeMoveSolution,
					new EightPuzzleSuccessorFunction(),
					new EightPuzzleGoalTest(), new ManhattanHeuristicFunction());
			Search search = new GreedyBestFirstSearch(new GraphSearch());
			SearchAgent agent = new SearchAgent(problem, search);
			printActions(agent.getActions());
			printInstrumentation(agent.getInstrumentation());
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private static void eightPuzzleAStarDemo() {
		System.out
				.println("\nEightPuzzleDemo AStar Search (MisplacedTileHeursitic)-->");
		try {
			Problem problem = new Problem(intArray,
					new EightPuzzleSuccessorFunction(),
					new EightPuzzleGoalTest(),
					new MisplacedTilleHeuristicFunction());
			Search search = new AStarSearch(new GraphSearch());
			SearchAgent agent = new SearchAgent(problem, search);
			printActions(agent.getActions());
			printInstrumentation(agent.getInstrumentation());
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private static void eightPuzzleSimulatedAnnealingDemo() {
		System.out.println("\nEightPuzzleDemo Simulated Annealing  Search -->");
		try {
			Problem problem = new Problem(random1,
					new EightPuzzleSuccessorFunction(),
					new EightPuzzleGoalTest(), new ManhattanHeuristicFunction());
			SimulatedAnnealingSearch search = new SimulatedAnnealingSearch();
			SearchAgent agent = new SearchAgent(problem, search);
			printActions(agent.getActions());
			System.out.println("Search Outcome=" + search.getOutcome());
			System.out.println("Final State=\n" + search.getLastSearchState());
			printInstrumentation(agent.getInstrumentation());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void eightPuzzleAStarManhattanDemo() {
		System.out
				.println("\nEightPuzzleDemo AStar Search (ManhattanHeursitic)-->");
		try {
			Problem problem = new Problem(random1,
					new EightPuzzleSuccessorFunction(),
					new EightPuzzleGoalTest(), new ManhattanHeuristicFunction());
			Search search = new AStarSearch(new GraphSearch());
			SearchAgent agent = new SearchAgent(problem, search);
			printActions(agent.getActions());
			printInstrumentation(agent.getInstrumentation());
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private static void printInstrumentation(Properties properties) {
		Iterator keys = properties.keySet().iterator();
		while (keys.hasNext()) {
			String key = (String) keys.next();
			String property = properties.getProperty(key);
			System.out.println(key + " : " + property);
		}

	}

	private static void printActions(List actions) {
		for (int i = 0; i < actions.size(); i++) {
			String action = (String) actions.get(i);
			System.out.println(action);
		}
	}

}

Class EightPuzzleDemo is only an example of use. Make all what you need.

Well,
but I want to have an console conversation, so in my view, there are some additional things I've to add

Place this code in your class Info.
Add needed imports
From your main invoke method eightPuzzleAStarDemo with proper parameter.

static void eightPuzzleAStarDemo(EightPuzzleBoard random1) {// little change
        System.out.println("\nEightPuzzleDemo AStar Search (MisplacedTileHeursitic)-->");
        try {
            Problem problem = new Problem(random1, EightPuzzleFunctionFactory.getActionsFunction(), EightPuzzleFunctionFactory.getResultFunction(), new EightPuzzleGoalTest());
            Search search = new AStarSearch(new GraphSearch(),
                    new MisplacedTilleHeuristicFunction());
            SearchAgent agent = new SearchAgent(problem, search);
            printActions(agent.getActions());
            printInstrumentation(agent.getInstrumentation());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private static void printInstrumentation(Properties properties) {
        Iterator<Object> keys = properties.keySet().iterator();
        while (keys.hasNext()) {
            String key = (String) keys.next();
            String property = properties.getProperty(key);
            System.out.println(key + " : " + property);
        }

    }

    private static void printActions(List<Action> actions) {
        for (int i = 0; i < actions.size(); i++) {
            String action = actions.get(i).toString();
            System.out.println(action);
        }
    }

How to construct random1?
Look at example:

EightPuzzleBoard random1 = new EightPuzzleBoard(new int[]{1, 4, 2, 7, 5, 8, 3, 0, 6});

where you need only put your

int[] intArray

where you need only put your

hey, that doesn't work
I want to have a conversation in the console like:
"but now your arraylist (which should be intArray at the end)
and if I try to do that, I got an error message.
so using the different arrays from the class EightPuzzleDemo is not the problem.
The problem is how to use the intArray from Class input.

Use your

intArray

as a parameter of constructor of class

EightPuzzleBoard

From keybord input same values as in example:

1, 4, 2, 7, 5, 8, 3, 0, 6

Use your as a parameter of constructor of class
From keybord input same values as in example:

mh,
what I did now is the following:
in class EightPuzzleDemo:

static EightPuzzleBoard Input = new EightPuzzleBoard(new int[]{ intArray});
private static void eightPuzzleAStarDemo(EightPuzzleBoard intArray) {
		System.out
				.println("\nEightPuzzleDemo AStar Search (MisplacedTileHeursitic)-->");
		try {
			Problem problem = new Problem(intArray,
					new EightPuzzleSuccessorFunction(),
					new EightPuzzleGoalTest(),
					new MisplacedTilleHeuristicFunction());
			Search search = new AStarSearch(new GraphSearch());
			SearchAgent agent = new SearchAgent(problem, search);
			printActions(agent.getActions());
			printInstrumentation(agent.getInstrumentation());
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

Class EightPuzzleBoard

package aima.search.eightpuzzle;

import java.util.ArrayList;
import java.util.List;

import aima.basic.XYLocation;

/**
 * @author Ravi Mohan
 * 
 */

public class EightPuzzleBoard {

	public static String LEFT = "Left";

	public static String RIGHT = "Right";

	public static String UP = "Up";

	public static String DOWN = "Down";

	public int[] getBoard() {
		return board;
	}

	int[] board;

	public EightPuzzleBoard() {
		board = new int[] { 5, 4, 0, 6, 1, 8, 7, 3, 2 };

	}

	public EightPuzzleBoard(int[] intArray) {
		board = intArray;
	}

	private int[] xycoordinatesFromAbsoluteCoordinate(int x) {
		int[] retVal = null;
		switch (x) {
		case 0:
			retVal = new int[] { 0, 0 };
			break;
		case 1:
			retVal = new int[] { 0, 1 };
			break;
		case 2:
			retVal = new int[] { 0, 2 };
			break;
		case 3:
			retVal = new int[] { 1, 0 };
			break;
		case 4:
			retVal = new int[] { 1, 1 };
			break;
		case 5:
			retVal = new int[] { 1, 2 };
			break;
		case 6:
			retVal = new int[] { 2, 0 };
			break;
		case 7:
			retVal = new int[] { 2, 1 };
			break;
		case 8:
			retVal = new int[] { 2, 2 };
			break;

		}
		return retVal;
	}

	private int absoluteCoordinatesFromXYCoordinates(int x, int y) {
		return x * 3 + y;
	}

	private int getValueAt(int x, int y) {
		// refactor this use either case or a div/mod soln
		return board[absoluteCoordinatesFromXYCoordinates(x, y)];
	}

	private int getGapPosition() {

		return getPositionOf(0);
	}

	private int getPositionOf(int val) {
		int retVal = -1;
		for (int i = 0; i < 9; i++) {
			if (board[i] == val) {
				retVal = i;
			}
		}
		return retVal;
	}

	public XYLocation getLocationOf(int val) {
		int abspos = getPositionOf(val);
		int xpos = xycoordinatesFromAbsoluteCoordinate(abspos)[0];
		int ypos = xycoordinatesFromAbsoluteCoordinate(abspos)[1];
		return new XYLocation(xpos, ypos);
	}

	private void setValue(int xPos, int yPos, int val) {
		int abscoord = absoluteCoordinatesFromXYCoordinates(xPos, yPos);
		board[abscoord] = val;

	}

	public int getValueAt(XYLocation loc) {
		return getValueAt(loc.getXCoOrdinate(), loc.getYCoOrdinate());
	}

	public void moveGapRight() {
		int gapPosition = getGapPosition();
		int xpos = xycoordinatesFromAbsoluteCoordinate(gapPosition)[0];
		int ypos = xycoordinatesFromAbsoluteCoordinate(gapPosition)[1];
		if (!(ypos == 2)) {
			int valueOnRight = getValueAt(xpos, ypos + 1);
			setValue(xpos, ypos, valueOnRight);
			setValue(xpos, ypos + 1, 0);
		}

	}

	public void moveGapLeft() {
		int gapPosition = getGapPosition();
		int xpos = xycoordinatesFromAbsoluteCoordinate(gapPosition)[0];
		int ypos = xycoordinatesFromAbsoluteCoordinate(getGapPosition())[1];
		if (!(ypos == 0)) {
			int valueOnLeft = getValueAt(xpos, ypos - 1);
			setValue(xpos, ypos, valueOnLeft);
			setValue(xpos, ypos - 1, 0);
		}

	}

	public void moveGapDown() {
		int gapPosition = getGapPosition();
		int xpos = xycoordinatesFromAbsoluteCoordinate(gapPosition)[0];
		int ypos = xycoordinatesFromAbsoluteCoordinate(gapPosition)[1];
		if (!(xpos == 2)) {
			int valueOnBottom = getValueAt(xpos + 1, ypos);
			setValue(xpos, ypos, valueOnBottom);
			setValue(xpos + 1, ypos, 0);
		}

	}

	public void moveGapUp() {
		int gapPosition = getGapPosition();
		int xpos = xycoordinatesFromAbsoluteCoordinate(gapPosition)[0];
		int ypos = xycoordinatesFromAbsoluteCoordinate(gapPosition)[1];
		if (!(xpos == 0)) {
			int valueOnTop = getValueAt(xpos - 1, ypos);
			setValue(xpos, ypos, valueOnTop);
			setValue(xpos - 1, ypos, 0);
		}

	}

	@Override
	public boolean equals(Object o) {

		if (this == o) {
			return true;
		}
		if ((o == null) || (this.getClass() != o.getClass())) {
			return false;
		}
		EightPuzzleBoard intArray = (EightPuzzleBoard) o;

		for (int i = 0; i < 8; i++) {
			if (this.getPositionOf(i) != intArray.getPositionOf(i)) {
				return false;
			}
		}
		return true;
	}

	@Override
	public int hashCode() {
		int result = 17;
		for (int i = 0; i < 8; i++) {
			int position = this.getPositionOf(i);
			result = 37 * result + position;
		}
		return result;
	}

	public List<XYLocation> getPositions() {
		ArrayList<XYLocation> retVal = new ArrayList<XYLocation>();
		for (int i = 0; i < 9; i++) {
			int[] res = xycoordinatesFromAbsoluteCoordinate(getPositionOf(i));
			XYLocation loc = new XYLocation(res[0], res[1]);
			retVal.add(loc);

		}
		return retVal;
	}

	public void setBoard(List<XYLocation> locs) {

		int count = 0;

		for (int i = 0; i < locs.size(); i++) {
			XYLocation loc = locs.get(i);
			this.setValue(loc.getXCoOrdinate(), loc.getYCoOrdinate(), count);
			count = count + 1;
		}
	}

	public boolean canMoveGap(String where) {
		boolean retVal = true;
		int absPos = getPositionOf(0);
		if (where.equals(LEFT)) {
			if ((absPos == 0) || (absPos == 3) || (absPos == 6)) {
				retVal = false;
			}
		}
		if (where.equals(RIGHT)) {
			if ((absPos == 2) || (absPos == 5) || (absPos == 8)) {
				retVal = false;
			}
		}
		if (where.equals(UP)) {
			if ((absPos == 0) || (absPos == 1) || (absPos == 2)) {
				retVal = false;
			}
		}
		if (where.equals(DOWN)) {
			if ((absPos == 6) || (absPos == 7) || (absPos == 8)) {
				retVal = false;
			}
		}

		return retVal;
	}

	@Override
	public String toString() {
		String retVal = board[0] + " " + board[1] + " " + board[2] + "\n"
				+ board[3] + " " + board[4] + " " + board[5] + " " + "\n"
				+ board[6] + " " + board[7] + " " + board[8];
		return retVal;
	}

}

but in class EightPuzzleDemo I have a bug at this line:

static EightPuzzleBoard Input = new EightPuzzleBoard(new int[]{ intArray});

what did I wrong?

Look at this.

import aima.core.agent.Action;
import aima.core.environment.eightpuzzle.EightPuzzleBoard;
import aima.core.environment.eightpuzzle.EightPuzzleFunctionFactory;
import aima.core.environment.eightpuzzle.EightPuzzleGoalTest;
import aima.core.environment.eightpuzzle.MisplacedTilleHeuristicFunction;
import aima.core.search.framework.GraphSearch;
import aima.core.search.framework.Problem;
import aima.core.search.framework.Search;
import aima.core.search.framework.SearchAgent;
import aima.core.search.informed.AStarSearch;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

public class Intro {

    public static void main(String[] args) throws IOException {

        InputStreamReader inputStream = new InputStreamReader(System.in);
        BufferedReader keyboard = new BufferedReader(inputStream);
        System.out.println("Enter eight numbers from 0 to 9 with ,as delimiter");
        String initialstate = keyboard.readLine();
        String delimiter = ",";
        String[] temp = initialstate.split(delimiter);
        int[] intArray = new int[temp.length];
        for (int i = 0; i < temp.length; i++) {
            for (int x = 0; x < temp.length; x++) {
                try {
                    intArray[x] = Integer.parseInt(temp[x]);
                } catch (NumberFormatException e) {
                    System.out.println("Invalid int encountered:- " + temp[x]);
                }
            }
        }

        System.out.println("Numbers array contents: ");
        for (int num : intArray) {
            System.out.println(num);

        }
        ////////////
        //EightPuzzleBoard random1 = new EightPuzzleBoard(new int[]{1, 4, 2, 7, 5, 8, 3, 0, 6});

        //create a new instance of class EightPuzzleBoard random1 with intArray as a parameter
        EightPuzzleBoard random1 = new EightPuzzleBoard(intArray);

        // invoke static method eightPuzzleAStarDemo
        eightPuzzleAStarDemo(random1);
    }

    static void eightPuzzleAStarDemo(EightPuzzleBoard random1) {
        System.out.println("\nEightPuzzleDemo AStar Search (MisplacedTileHeursitic)-->");
        try {
            Problem problem = new Problem(random1, EightPuzzleFunctionFactory.getActionsFunction(), EightPuzzleFunctionFactory.getResultFunction(), new EightPuzzleGoalTest());
            Search search = new AStarSearch(new GraphSearch(),
                    new MisplacedTilleHeuristicFunction());
            SearchAgent agent = new SearchAgent(problem, search);
            printActions(agent.getActions());
            printInstrumentation(agent.getInstrumentation());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private static void printInstrumentation(Properties properties) {
        Iterator<Object> keys = properties.keySet().iterator();
        while (keys.hasNext()) {
            String key = (String) keys.next();
            String property = properties.getProperty(key);
            System.out.println(key + " : " + property);
        }

    }

    private static void printActions(List<Action> actions) {
        for (int i = 0; i < actions.size(); i++) {
            String action = actions.get(i).toString();
            System.out.println(action);
        }
    }
}

study lines 45,48,51,54,57
I hope, now all is clear

Edited 6 Years Ago by quuba: add

This question has already been answered. Start a new discussion instead.