-
Notifications
You must be signed in to change notification settings - Fork 0
/
MazeViewer.java
91 lines (72 loc) · 2.5 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
// Name:
// USC loginid:
// CS 455 PA3
// Spring 2017
import java.io.FileNotFoundException;
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, followed by the start location,
* and ending with the exit location. Each maze location is
* either a wall (1) or free (0). Here is an example of contents of a file for
* a 3x4 maze, with start location as the top left, and exit location as the bottom right
* (we count locations from 0, similar to Java arrays):
*
* 3 4
* 0111
* 0000
* 1110
* 0 0
* 2 3
*
*/
public class MazeViewer {
private static final char WALL_CHAR = '1';
private static final char FREE_CHAR = '0';
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];
JFrame frame = readMazeFile(fileName);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
catch (FileNotFoundException exc) {
System.out.println("File not found: " + fileName);
}
catch (IOException exc) {
exc.printStackTrace();
}
}
/**
readMazeFile reads in maze from the file whose name is given and
returns a MazeFrame created from it.
@param fileName
the name of a file to read from (file format shown in class comments, above)
@returns a MazeFrame containing the data from the file.
@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 MazeFrame readMazeFile(String fileName) throws IOException {
// DUMMY CODE TO GET IT TO COMPILE
return new MazeFrame(new boolean[1][1], new MazeCoord(0, 0), new MazeCoord(0, 0));
}
}