-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_algo.java
More file actions
109 lines (101 loc) · 2.82 KB
/
Copy pathsearch_algo.java
File metadata and controls
109 lines (101 loc) · 2.82 KB
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
108
109
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.LinkedHashMap;
/**
*
* @author Boaz Sharabi
*
* this abstract class represent search algorithm using tile states
*all of derived class have to implement the abstract method run()
*/
public abstract class search_algo {
tile goal;
tile start;
boolean withOpen;
boolean withTime;
String path="no path";
int count;
int cost;
long StartTime;
LinkedHashMap<String, tile> openlist= new LinkedHashMap<String, tile>();
public search_algo(tile start, tile goal, boolean withOpen, boolean withTime) {
this.start=start;
this.goal=goal;
this.withOpen=withOpen;
this.withTime=withTime;
}
/**
* run the algorithem
*/
public abstract void run();
protected String path(tile child) {
String path="";
tile current= child;
while(current.getParent()!=null&¤t.getParent().getParent()!=null) { // restore the solution path
path= "-"+current.getNumOp()+path;
current=current.getParent();
}
path=current.getNumOp()+path;
return path;
}
/**
* print the open list (all state that created by algorithm until now) to console
*/
public void PrintOpenList() {
StringBuilder string = new StringBuilder();
Iterator<tile> b=openlist.values().iterator();
tile current=null;
while(b.hasNext()) {
current=b.next();
string.append(current+"\n__________________________________________");
}
string.append("\n------------------------------------------------------------");
System.out.println(string);
}
boolean checkValid(tile t){
Board board= t.getBoard();
for(int i=0;i<board.mat.length;i++) {
for(int j=0;j<board.mat[0].length;j++) {
if(board.mat[i][j]!=-1&&board.color_cell.get(board.mat[i][j])==Color.BLACK) {
int x=((board.mat[i][j]-1)/(board.mat[0].length));
int y= ((board.mat[i][j]-1)%(board.mat[0].length));
if (i!=x||j!=y)
return false;
}
}
}
return true;
}
/**
* save the algorithm result to output file
*/
public void saveToFile() {
count=ColorTilePuzzle.count;
String fileName = "output.txt";
try
{
PrintWriter pw = new PrintWriter(new File(fileName));
StringBuilder sb = new StringBuilder();
sb.append(path);
sb.append("\n");
sb.append("Num: "+(count-1)); // decrease count by 1 because i generate goal tile at start for convenience of comparison
if(!path.equals("no path")) {
sb.append("\n");
sb.append("Cost: "+cost);
}
if(withTime) {
sb.append("\n");
sb.append(String.format("%.3f",(System.nanoTime()-StartTime)*Math.pow(10, -9))+" seconds");
}
pw.write(sb.toString());
pw.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
return;
}
}
}