Hello need help reading in the maze from the textual representation into the _ro
ID: 3572762 • Letter: H
Question
Hello need help reading in the maze from the textual representation into the _ropen and _copen arrays. Read in contents of a maze printed with ASCII graphics from the file.
The rows and columns are already set. Example (assuming rows = 3, columns = 4)
public class Maze {
private final int _rows, _columns;
private boolean[][] _ropen, _copen;
private boolean[][] _marked;
/*
* _ropen[i,j] is true if one can get from cell [i,j] to cell [i+1,j]
* _copen[i,j] is true is one can get from cell [i,j] to cell[i,j+1];
*/
public Maze(int rows, int columns) {
_rows = rows;
_columns = columns;
_ropen = new boolean[rows-1][columns];
_copen = new boolean[rows][columns-1];
_marked = new boolean[rows][columns];
}
public int rows() { return _rows; }
public int columns() { return _columns; }
public void read(BufferedReader r) throws IOException {
}
public void clear() {
for (int i = 0; i < _rows; ++i) {
for (int j=0; j < _columns; ++j) {
_marked[i][j] = false;
if (i+1 != _rows) _ropen[i][j] = false;
if (j+1 != _columns) _copen[i][j] = false;
}
}
}
private JComponent panel;
public JComponent getPanel() {
if (panel == null) panel = new Panel();
return panel;
}
@SuppressWarnings("serial")
public static class ParseException extends IOException {
public ParseException(String s) { super(s); }
}
public class Cell {
public final int row, column;
public Cell(int i, int j) {
if (i < 0 || i >= _rows) throw new IllegalArgumentException("row " + i + " out of range [0," + _rows + ")");
if (j < 0 || j >= _columns) throw new IllegalArgumentException("column " + j + " out of range [0," + _columns + ")");
this.row = i;
this.column = j;
}
public String toString() {
return "[" + row + "," + column + "]"; // useful for debugging
}
}
Explanation / Answer
public void read(BufferedReader r) throws IOException {
try{
String currentLine;
int col = 0;
while(currentLine = r.readLine()) != null){
if (currentLine.charAt(0)=='|'){
_ropen[0][col] = false;
}
else{
_ropen[0][col] = true;
}
if (currentLine.charAt(1)=='|'){
_ropen[1][col] = false;
}
else{
_ropen[1][col] = true;
}
if (currentLine.charAt(2)=='|'){
_ropen[2][col] = false;
}
else{
_ropen[2][col] = true;
}
if (currentLine.charAt(3)=='|'){
_ropen[3][col] = false;
}
else{
_ropen[3][col] = true;
}
col++;
}
int row = 0;
while(currentLine = r.readLine()) != null){
if (currentLine.charAt(0)=='-'){
_ropen[row][0] = false;
}
else{
_ropen[row][0] = true;
}
if (currentLine.charAt(1)=='-'){
_ropen[row][1] = false;
}
else{
_ropen[row][1] = true;
}
if (currentLine.charAt(2)=='-'){
_ropen[row][2] = false;
}
else{
_ropen[row][2] = true;
}
row++;
}
r.close();
}
catch(IOException e){
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.