Reputation: 882
I have to make a program that takes in an 8 puzzle as an array, checks to see if it is solvable (and catches any input errors), and then uses A* search to solve the puzzle if it is solvable, displaying the sequence of moves to solve the 8 puzzle (basically how the array is reorganized, and this all has to be in one .java file (most I have seen are in multiple source files, which I would have preferred, but that isn't what is being accepted currently. I have gotten most of it done, except for using a file for input to create the puzzle array (I have set it up to take in keyboard input to create it), the A* search algorithm itself, and outputting to file. The parts I have done are pretty much all the validation methods (checking to make sure the puzzle is a true 8 puzzle, uses combination of if statements and try-catch blocks), the checking for solvability method, puzzle creation, and I think that is most of every thing.
The part I am having trouble with is the algorithm itself. I understand it, but not how to implement it all in one file because I don't know how to make a Node object with a State object(I think State is supposed to be an object, but I am not sure) work in the same file as another class object (that class object being puzzle). I have quite a bit done already, but the method that this whole thing is about (I know how to implement this as separate .java files, but not like this, partly due to Java not being my strongest data structure language (my knowledge of java data structure creation is inferior to that using C++). Anyway, here is what I have so far so you can know the context in which I'm working:
import java.io.File;
import java.io.IOException;
import java.util.Queue;
import java.util.Scanner;
public class Puzzle {
public static void main(String[] args) throws IOException
{
int collumnInput = 0;
int rowInput = 0;
if (args.length == 0)
{
System.out.println("Give 8-puzzle as 3x3 matrix (a space between digits, hit Enter after each line: " );
Scanner scan = new Scanner (System.in);
int[][] puzzleArray = new int[3][3];
for (collumnInput=0; collumnInput < 3; collumnInput++)
{
for (rowInput = 0; rowInput < 3; rowInput++)
{
try
{
puzzleArray[collumnInput][rowInput] = scan.nextInt();
if ((puzzleArray[collumnInput][rowInput] > 8) || (puzzleArray[collumnInput][rowInput] < 0))
{
System.out.println("Invalid 8-puzzle entered!");
System.exit(0);
}
}
catch (java.util.InputMismatchException exception)
{
System.out.println("Invalid 8-puzzle entered!");
System.exit(0);
}
}
}
scan.close();
for(int column = 0; column < 3; column++)
{
for(int row = 0; row < 3; row++)
{
System.out.print(puzzleArray[column][row] + " "); //Outputs the array in a 3x3 grid.
}
System.out.println();
}
boolean []check = new boolean[9];
for (int collumnCheck = 0; collumnCheck < collumnInput; collumnCheck++){
for (int rowCheck = 0; rowCheck < rowInput; rowCheck++)
{
if (check[puzzleArray[collumnCheck][rowCheck]]){
System.out.println("Invalid 8-puzzle entered!");
System.exit(0);
}else
{
check[ puzzleArray[collumnCheck][rowCheck]] = true;
}
}
}
int[] oneDArray = convertToOneD(puzzleArray);
boolean possible=isSolvable(oneDArray);
if (possible == true)
System.out.println("Is solvable");
else
System.out.println("Not solvable");
}
/*else if (args.length == 1)
{
}
else if (args.length == 2)
{
}
else
{
System.out.println("Invalid number of arguments, halting execution.");
}
*/
}
public static boolean isSolvable(int [] p)
{
int i, j, n, inversions = 0;
n = p.length;
for(i = 0; i < n - 1; i++)
for(j = i+1; j < n; j++)
if(p[i] > p[j]) {
//System.out.println("("+p[i]+", "+p[j]+")");
inversions++;
}
//System.out.println("Number of inversions are: "+inversions);
return !((inversions > 0) && (inversions % 2 == 0));
}
public static int[] convertToOneD(int[][] theArray)
{
int[] singleD = new int[9];
int k=0;
for (int i=0; i < 3; i++)
{
for (int j=0; j < 3; j++)
{
singleD[k] = theArray[i][j];
k++;
}
}
for (k = 0; k < 9; k++)
{
System.out.print(singleD[k]+" ");
}
return singleD;
}
/*public static void AStarSearch(int[]puzzleArray)
{
}*/
}
Upvotes: 0
Views: 342
Reputation: 20520
Put your other classes inside your Puzzle
class as inner static classes, like this:
public class Puzzle {
public static class Node {
public Node() {
//constructor
}
//other methods of Node
}
public static class State {
public State() {
//constructor
}
//other methods of State
}
public static void main(String[] args) {
Node n = new Node();
State s = new State();
//whatever
}
}
You can then use them pretty much as if they were defined in other files. The only difference is that you can't use them from outside the Puzzle
class unless they have enough visibility (fine in this case because I've declared them as public
) and unless you refer to them as Puzzle.Node
or import them.
If they were private
, then you could use them inside Puzzle
, but not elsewhere.
Upvotes: 1