-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileParser.java
76 lines (69 loc) · 2.39 KB
/
FileParser.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
import java.io.*;
import java.util.Arrays;
import java.util.Queue;
import java.util.Vector;
public class FileParser {
BufferedReader br;
/* Init Buffer */
public FileParser(String path){
File file = new File(path);
try {
br = new BufferedReader(new FileReader(file));
} catch (Exception e) {
System.err.println("No input file");
}
}
/* Parse file as asked */
public int[][] parse() throws IOException {
String line;
Vector<Integer> black, red;
int[][] mat;
/* First Settings */
Algorithms.alg = br.readLine();
String wtime = br.readLine();
if(wtime.equalsIgnoreCase("with time"))
Algorithms.with_time = true;
else
Algorithms.with_time = false;
String openList = br.readLine();
if(openList.equalsIgnoreCase("no open"))
Algorithms.with_open_list = false;
else
Algorithms.with_open_list = true;
/* Mat,GOAL init */
String[] nm = br.readLine().split("x"); //mat [N][M]
int rows = Integer.parseInt(nm[0]), colm = Integer.parseInt(nm[1]);
mat = new int[rows][colm];
Node.setGOAL(rows,colm);
/* Colors */
line = br.readLine(); //black
Node.BLACK = parseColors(line.substring(6));
line = br.readLine(); //red
Node.RED = parseColors(line.substring(4));
/* Matrix */
while(br.ready()) {
for(int i=0; i<mat.length;i++){
String[] lineNumbers = br.readLine().split(",");
int j = 0;
for (String s : lineNumbers) {
mat[i][j++] = s.equals("_") ? 0 : Integer.parseInt(s);
}
}
}
return mat;
}
/* parse colored blocks vector */
private Vector<Integer> parseColors(String str){
if (str == null || str.isEmpty() || str == ""){
return new Vector<>();
}
else {
String[] sr = str.substring(1).split(",");
Vector<Integer> res = new Vector<>();
for (String s : sr) {
res.add(Integer.parseInt(s));
}
return res;
}
}
}