-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMazeViewer.java
More file actions
143 lines (114 loc) · 4.57 KB
/
MazeViewer.java
File metadata and controls
143 lines (114 loc) · 4.57 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
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
140
141
142
143
// Name: Joshua Schnall
// USC loginid: jschnall@usc.edu
// CS 455 PA3
// Spring 2016
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JFrame;
import java.util.Scanner;
import java.io.File;
/**
* 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 int WALL_CHAR = 1; //changed to integer constant
private static final int FREE_CHAR = 0; //changed to integer constant
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); //print error if file not found
}
catch (IOException exc) {
System.out.println("ERROR: file is not proper format for MazeViewer"); // print error if improper file format
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 {
File file = new File(fileName); //get file
Scanner in = new Scanner(file);
//check for correct file format.... first number is integer
if(!in.hasNextInt()){
throw new IOException();
}
int x = in.nextInt(); //get x,y dimensions from file
//check for correct file format.... second number is integer
if(!in.hasNextInt()){
throw new IOException();
}
int y = in.nextInt();
boolean[][] frameSize = new boolean[x][y]; //scans in maze size and creates boolean array of that size
in.useDelimiter(""); //reset delimiter to single char
for(int i = 0; i< frameSize.length; i++){
in.nextLine();
for(int j = 0; j< frameSize[0].length; j++)
//check for correct file format....all numbers being read in are integers and appropriate number of integers
if(!in.hasNextInt()){
throw new IOException();
}
else if(in.nextInt() == WALL_CHAR){
frameSize[i][j] = true; //sets any 1 spots which are walls to true in boolean array
}
else{
frameSize[i][j] = false; // sets zeros spots to false in boolean array
}
}
in.useDelimiter("\\p{javaWhitespace}+"); // reset delimiter to default
//check for correct file format.... second to last set of numbers are integers
if(!in.hasNextInt()){
throw new IOException();
}
MazeCoord start = new MazeCoord(in.nextInt(), in.nextInt());
in.nextLine();
//check for correct file format.... last set of numbers are integers
if(!in.hasNextInt()){
throw new IOException();
}
MazeCoord finish = new MazeCoord(in.nextInt(), in.nextInt()); // get the start and finish coordinates
return new MazeFrame(frameSize, start, finish);
}
}