forked from djhonyy/8-Puzzle-Solver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblemSolver.java
107 lines (98 loc) · 2.46 KB
/
ProblemSolver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
public class ProblemSolver
{
/*
* We expect arguments in the form:
*
* ./ProblemSolver <-d> dfs/bfs/aso/asm <initial state> <optional parameter>
*
* Example: ./ProblemSolver dfs 0 1 2 3 4 5 6 7 8
*
* See Readme for more information.
*/
public static void main(String[] args)
{
// Numbers to be adjusted if the debug toggle is present, as components
// of args will be in different locations if it is.
int searchTypeDebug = 0;
int eightPuzzleDebug = 1;
boolean debug = false;
// Print out correct usage and end the program if there aren't any
// parameters
if (args.length < 1)
{
printUsage();
}
// Check for debug toggle
if (args[0].equals("-d"))
{
searchTypeDebug = 1;
eightPuzzleDebug = 2;
debug = true;
System.out.println("Search Type passed in: "
+ args[searchTypeDebug].toLowerCase());
}
String searchType = args[searchTypeDebug].toLowerCase();
if (args.length > 2) // We will run with 8puzzle
{
int[] startingStateBoard = dispatchEightPuzzle(args,
eightPuzzleDebug);
if (searchType.equals("dfs")) // Use DFSearch.java
{
DFSearch.search(startingStateBoard, debug);
}
else if (searchType.equals("bfs")) // Use BFSearch.java
{
BFSearch.search(startingStateBoard, debug);
}
// Use AStarSearch.java with number out of place
else if (searchType.equals("aso"))
{
AStarSearch.search(startingStateBoard, debug, 'o');
}
// Use AStarSearch.java with Manhattan Distance
else if (searchType.equals("asm"))
{
AStarSearch.search(startingStateBoard, debug, 'm');
}
// An invalid searchType has been passed in. Print correct usage and
// end the program.
else
{
printUsage();
}
}
else
// We will run with fwgc
{
if (searchType.equals("dfs")) // Use DFSearch.java
{
DFSearch.search(debug);
}
else if (searchType.equals("bfs")) // Use BFSearch.java
{
BFSearch.search(debug);
}
else
{
printUsage();
}
}
}
// Helper method to print the correct usage and end the program
private static void printUsage()
{
System.out.println("Usage: ./Main <searchType> [Initial Puzzle State]");
System.exit(-1);
}
// Helper method to build our initial 8puzzle state passed in through args
private static int[] dispatchEightPuzzle(String[] a, int d)
{
int[] initState = new int[9];
// i -> loop counter
for (int i = d; i < a.length; i++)
{
initState[i - d] = Integer.parseInt(a[i]);
}
return initState;
}
}