-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMazeViewer.java
140 lines (116 loc) · 4.32 KB
/
MazeViewer.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//Name:Yangping Zheng
//USC loginid:yangpinz
//CS 455 PA3
//Fall 2015
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JFrame;
/**
* MazeViewer class
*
* Program to read in and display a maze and a path through the maze. At user
* command displays a path through the maze if there is one.
*
* How to call it from the command line:
*
* java MazeViewer mazeFile
*
* where mazeFile is a text file of the maze. The format is the number of rows
* and number of columns, followed by one line per row. Each maze location is
* either a wall (1) or free (0). Here is an example of contents of a file for
* a 3x4 maze:
*
* 3 4
* 0111
* 0000
* 1110
* 0010
*
* The top left is the maze entrance, and the bottom right is the maze exit.
*
*/
public class MazeViewer {
private static final int COLNUM_OF_MAZE = 0;//indicate the maze information
private static final int ROWNUM_OF_MAZE = 1;
public static void main(String[] args) {
String fileName = "";
try {
if (args.length < 1) {
System.out.println("ERROR: missing file name command line argument");
}
else {
fileName = args[0];
boolean[][] mazeData = readMazeFile(fileName);
// for(int i = 0;i<mazeData.length;i++) {
// for(int j = 0;j<mazeData[0].length;j++) {
// System.out.print(mazeData[i][j] + " ");
// }
// System.out.println();
// }
// System.out.println(mazeData.length);
JFrame frame = new MazeFrame(mazeData);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
catch (FileNotFoundException exc) {
System.out.println("File not found: " + fileName);
}
catch (IOException exc) {
exc.printStackTrace();
}
catch (NumberFormatException exc) {
exc.printStackTrace();
}
}
/**
readMazeFile reads in and returns a maze from the file whose name is
String given. The file format is shown in the MazeViewer class comments.
@param fileName
the name of a file to read from
@returns
the array with maze contents. false at a location means there is no wall
(0 in the file) and true means there is a wall (1 in the file).
The first dimension is which row, and the second is which column. E.g. if the file
started with 3 10, it would mean the array returned would have
3 rows, and 10 columns.
@throws FileNotFoundException
if there's no such file (subclass of IOException)
@throws IOException
(hook given in case you want to do more error-checking.
that would also involve changing main to catch other exceptions)
*/
private static boolean[][] readMazeFile(String fileName) throws IOException {
String rd = ""; //to save the content of each line of maze data
BufferedReader in = null; //set a new reference of a Buffer
boolean[][] rtn = null; //set a 2D array to save the maze data we read from the file
try {
in = new BufferedReader(new FileReader(fileName)); //BufferReader is to read in the file and transfer to a buffer
rd=in.readLine(); //read a line each time
String[] rowComStr = rd.split(" "); //to parse the 2 numbers in the first row of maze data
int rowNum = Integer.parseInt(rowComStr[COLNUM_OF_MAZE]);
int colNum = Integer.parseInt(rowComStr[ROWNUM_OF_MAZE]);
rtn = new boolean[rowNum][colNum]; //define the 2D array we are going to use
int rowCount = 0;
while(((rd=in.readLine())!=null)&&rowCount<rtn.length) { //iterate over each line maze data
for(int i = 0;i<colNum;i++) {
rtn[rowCount][i]=parseBool(rd.charAt(i)-'0');
}
rowCount++;
}
in.close();
} catch (NumberFormatException e) {
System.out.println("Unexpected content in file!"); //catch the exception from parseInt()
throw e;
}
return rtn;
}
/*
* parse the '1' and '0' into true and false
* */
private static boolean parseBool(int data) {
return (data==0)?false:true;
}
}