-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
82 lines (74 loc) · 2.54 KB
/
Main.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
import java.util.*;
public class Main {
public static void main(String[] args) throws InterruptedException{
Generate gen = new Generate();
Scanner input = new Scanner(System.in);
Board board;
Boolean done = false;
Astar alg = new Astar();
System.out.println("Run tests? (y, n)");
String dec = input.nextLine();
if (dec.toLowerCase().equals("y")) {
Test test = new Test();
} else {
do {
System.out.println("Enter your own puzzle or randomly generate one? (own, gen)");
dec = input.nextLine();
if (dec.toLowerCase().equals("own")) {
board = getUserInput();
} else {
do {
board = gen.genBoard();
} while (!gen.validateBoard(board));
System.out.println(board.getFirst());
System.out.println(board.getSecond());
System.out.println(board.getThird());
}
System.out.println("Manhatten Distance Heuristic");
long startTime = System.nanoTime();
int manhattanNodes = alg.astarManhattan(board);
long endTime = System.nanoTime();
System.out.println("Nodes generated: " + manhattanNodes);
System.out.println("Time taken: " + (endTime - startTime) / 1000000 + "ms");
displaySol(alg.getGoal());
System.out.println("Misplaced Tile Heuristic");
startTime = System.nanoTime();
int misplacedNodes = alg.astarMisplaced(board);
endTime = System.nanoTime();
System.out.println("Nodes generated: " + misplacedNodes);
System.out.println("Time taken: " + (endTime - startTime) / 1000000 + "ms");
displaySol(alg.getGoal());
System.out.println("Done? (y, n)");
dec = input.nextLine();
if (dec.toLowerCase().equals("y")) done = true;
} while (!done);
}
}
private static Board getUserInput() {
Generate gen = new Generate();
Scanner input = new Scanner(System.in);
System.out.println("Please enter the 3 lines of the board, no spaces");
String userInput = "";
userInput += input.nextLine();
userInput += input.nextLine();
userInput += input.nextLine();
Board board = new Board(userInput);
while (!gen.validateBoard(board)) {
System.out.println("Board not valid: Please enter the 3 lines of the board, no spaces");
userInput = input.nextLine();
userInput += input.nextLine();
userInput += input.nextLine();
board = new Board(userInput);
}
return board;
}
private static void displaySol(Board board) {
if (board.getParent() != null) {
displaySol(board.getParent());
}
System.out.println("----");
System.out.println(board.getFirst());
System.out.println(board.getSecond());
System.out.println(board.getThird());
}
}