I have these two classes Maze and MazeCell.
I have a problem with the Maze constructor in Maze Class and do not know how to access it, or test it.
Does Maze N = new Maze(row, col) do anything. I know it's supposed to set it as the length of the maze, and width, but the M = new MazeCell [length][width] confuses me.
Could I get an explanation on that?
How can I use the print, I have tried N.print, but it won't work.
Here are the two classes
////////////////////////////////////// MAZE CLASS////////////////////////////
import java.io.*;
import java.util.*;
public class Maze
{
// The maze. ?
private MazeCell [][] M;
// M is an array of MazeCell.
// Constructor for a maze of size length rows by width columns.
public Maze (int length, int width)
{
M = new MazeCell [length] [width];
}
// Initialize a cell of the maze by setting the four sides
// to one of DOOR, WALL or EXIT. This method gets the object
// representing the cell in position (row, col) and calls
// the MazeCell constructor.
//This is just one small block like 0,0
public void initCell (int row, int col, MazeCell.WallType top,
MazeCell.WallType right,
MazeCell.WallType bottom,
MazeCell.WallType left)
{
M[row][col] = new MazeCell (top, right, bottom, left);
}
// Return the MazeCell object at position (row, col).
public MazeCell getCell (int row, int col)
{
return M[row][col];
}
// Print out the maze. Used for debugging purposes.
public void print()
{
// Draw top and sides of the cells of the maze. The
// method draws the rows one at a time, starting at
// the top of the maze.
for (int row = 0; row < M.length; row++)
{
System.out.println(renderTop(row));
System.out.println(renderSides(row));
}
// Draw bottom of row M.length - 1
String theBottom = "";
for (int col = 0; col < M[0].length; col++)
{
if (M[0][col].getBottom() == MazeCell.WallType.WALL)
theBottom += "._";
else if (M[0][col].getBottom() == MazeCell.WallType.EXIT)
theBottom += ".x";
else
theBottom += ". ";
}
theBottom += ".";
System.out.println (theBottom);
}
// A method to help the print method. This method constructs
// a string representing the top of row "row" and returns it
// to the caller.
private String renderTop (int row)
{
String theTop = ".";
for (int col = 0; col < M[row].length; col++)
{
if (M[row][col].getTop() == MazeCell.WallType.WALL)
theTop += "_.";
else if (M[row][col].getTop() == MazeCell.WallType.EXIT)
theTop += "x.";
else // Must be a door
theTop += " .";
}
return theTop;
}
// Another helper method. This method constructs the left and
// right sides of the cells in row "row" and returns this string
// to the caller.
private String renderSides (int row)
{
String theSide;
// Handle the left side of the row as a special
// case.
if (M[row][0].getLeft() == MazeCell.WallType.WALL)
theSide = "|";
else if (M[row][0].getLeft() == MazeCell.WallType.EXIT)
theSide = "x";
else
theSide = " "; // This shouldn't happen, since we
// can't have a door that leads
// outside of the maze.
// Now, draw the right side of every cell.
for (int col = 0; col < M[row].length; col++)
if (M[row][col].getRight() == MazeCell.WallType.WALL)
theSide += " |";
else if (M[row][col].getRight() == MazeCell.WallType.EXIT)
theSide += " x";
else
theSide += " ";
return theSide;
}
public static boolean solveMaze(Maze theMaze, int currentRow, int currentCol)
{
// MazeCell IF =
// theMaze = new Maze(currentRow,currentRow);
// IF.getTop();
// System.err.println(M.length);
// public static boolean solveMaze (Maze theMaze, int currentRow, int current col)
/* Base cases. Have I visited the room.
* Is they an exit.
Check if visisted; | if room[c][r] visited return true/false
Look for exit; | check for exit, look up,right,down, left;
Mark visited; | return true
return false; | Let the recursion begin ha ha ha ha ha ha;
Look for door; |
Go through door; |
solveMaze(row + 1, col); |
solveMaze(row, col + 1); |
solveMaze(row - 1, col); |
solveMaze(row, col -1); |
*/
return true;
}
public static void main(String [] args)
{
DataInputStream dis = null;
String record = null;
int recCount = 0;
BufferedReader in = new BufferedReader( new InputStreamReader(System.in));
Scanner stdin = new Scanner(System.in); //ALLOWS DATA INPUT
System.out.println("Enter file name ");
String input = stdin.nextLine(); //USER INPUTS DATA
try {
File f = new File(input);
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
while ( (record=dis.readLine()) != null )
{
recCount++;
// System.out.println(recCount + ": " + record);
String delims =" " ; // DEFINES CHARACTER BEING USED
StringTokenizer st = new StringTokenizer(record, delims);// ALLOWS THE OPERATORS
int row = Integer.parseInt(st.nextToken()); // whole changes string to int
int col = Integer.parseInt(st.nextToken()); // numerator
for(int w = 0; w < row * col; w++)
{// READS ALL DATA
int frow = Integer.parseInt(st.nextToken()); // denominator
int fcol = Integer.parseInt(st.nextToken());
char del, del1, del2,del3;
del = ( st.nextToken() ).charAt( 0 ); // changes string to character
del1 = ( st.nextToken() ).charAt( 0 ); // CHECKS CHARACTER ENTERED.
del2 = ( st.nextToken() ).charAt( 0 );
del3 = ( st.nextToken() ).charAt( 0 );
System.out.println(frow);
System.out.println(fcol);
System.out.print(del);
System.out.print(del1);
System.out.print(del2);
System.out.print(del3);
System.out.println(" ");
// System.err.println("YOU MADE PRIVATE M STATIC CHANGE IT BACK !");
Maze N = new Maze(row,col);
MazeCell q;
q = new MazeCell(MazeCell.char2Wall(del),MazeCell.char2Wall(del1),
MazeCell.char2Wall(del2),MazeCell.char2Wall(del3));
N.initCell(frow, fcol, q.char2Wall(del), q.char2Wall(del1), q.char2Wall(del2), q.char2Wall(del3));
if(q.getTop().equals(MazeCell.WallType.DOOR))
{
System.out.println("You found the door");
}
N.getCell(frow,fcol);
} //END OF FOR LOOP
}
}
catch (IOException e)
{
// catch io errors from FileInputStream or readLine()
System.out.println("Invalid filename !");//
}
}
}
//////////////////////END OF MAZE CLASS//////////////////////
////////////////////////START OF MAZECELL CLASS//////////////////////////////////////
import java.io.*;
import java.util.*;
public class Maze
{
// The maze. ?
private MazeCell [][] M;
// M is an array of MazeCell.
// Constructor for a maze of size length rows by width columns.
public Maze (int length, int width)
{
M = new MazeCell [length] [width];
}
// Initialize a cell of the maze by setting the four sides
// to one of DOOR, WALL or EXIT. This method gets the object
// representing the cell in position (row, col) and calls
// the MazeCell constructor.
//This is just one small block like 0,0
public void initCell (int row, int col, MazeCell.WallType top,
MazeCell.WallType right,
MazeCell.WallType bottom,
MazeCell.WallType left)
{
M[row][col] = new MazeCell (top, right, bottom, left);
}
// Return the MazeCell object at position (row, col).
public MazeCell getCell (int row, int col)
{
return M[row][col];
}
// Print out the maze. Used for debugging purposes.
public void print()
{
// Draw top and sides of the cells of the maze. The
// method draws the rows one at a time, starting at
// the top of the maze.
for (int row = 0; row < M.length; row++)
{
System.out.println(renderTop(row));
System.out.println(renderSides(row));
}
// Draw bottom of row M.length - 1
String theBottom = "";
for (int col = 0; col < M[0].length; col++)
{
if (M[0][col].getBottom() == MazeCell.WallType.WALL)
theBottom += "._";
else if (M[0][col].getBottom() == MazeCell.WallType.EXIT)
theBottom += ".x";
else
theBottom += ". ";
}
theBottom += ".";
System.out.println (theBottom);
}
// A method to help the print method. This method constructs
// a string representing the top of row "row" and returns it
// to the caller.
private String renderTop (int row)
{
String theTop = ".";
for (int col = 0; col < M[row].length; col++)
{
if (M[row][col].getTop() == MazeCell.WallType.WALL)
theTop += "_.";
else if (M[row][col].getTop() == MazeCell.WallType.EXIT)
theTop += "x.";
else // Must be a door
theTop += " .";
}
return theTop;
}
// Another helper method. This method constructs the left and
// right sides of the cells in row "row" and returns this string
// to the caller.
private String renderSides (int row)
{
String theSide;
// Handle the left side of the row as a special
// case.
if (M[row][0].getLeft() == MazeCell.WallType.WALL)
theSide = "|";
else if (M[row][0].getLeft() == MazeCell.WallType.EXIT)
theSide = "x";
else
theSide = " "; // This shouldn't happen, since we
// can't have a door that leads
// outside of the maze.
// Now, draw the right side of every cell.
for (int col = 0; col < M[row].length; col++)
if (M[row][col].getRight() == MazeCell.WallType.WALL)
theSide += " |";
else if (M[row][col].getRight() == MazeCell.WallType.EXIT)
theSide += " x";
else
theSide += " ";
return theSide;
}
public static boolean solveMaze(Maze theMaze, int currentRow, int currentCol)
{
// MazeCell IF =
// theMaze = new Maze(currentRow,currentRow);
// IF.getTop();
// System.err.println(M.length);
// public static boolean solveMaze (Maze theMaze, int currentRow, int current col)
/* Base cases. Have I visited the room.
* Is they an exit.
Check if visisted; | if room[c][r] visited return true/false
Look for exit; | check for exit, look up,right,down, left;
Mark visited; | return true
return false; | Let the recursion begin ha ha ha ha ha ha;
Look for door; |
Go through door; |
solveMaze(row + 1, col); |
solveMaze(row, col + 1); |
solveMaze(row - 1, col); |
solveMaze(row, col -1); |
*/
return true;
}
public static void main(String [] args)
{
DataInputStream dis = null;
String record = null;
int recCount = 0;
BufferedReader in = new BufferedReader( new InputStreamReader(System.in));
Scanner stdin = new Scanner(System.in); //ALLOWS DATA INPUT
System.out.println("Enter file name ");
String input = stdin.nextLine(); //USER INPUTS DATA
try {
File f = new File(input);
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
while ( (record=dis.readLine()) != null )
{
recCount++;
// System.out.println(recCount + ": " + record);
String delims =" " ; // DEFINES CHARACTER BEING USED
StringTokenizer st = new StringTokenizer(record, delims);// ALLOWS THE OPERATORS
int row = Integer.parseInt(st.nextToken()); // whole changes string to int
int col = Integer.parseInt(st.nextToken()); // numerator
for(int w = 0; w < row * col; w++)
{// READS ALL DATA
int frow = Integer.parseInt(st.nextToken()); // denominator
int fcol = Integer.parseInt(st.nextToken());
char del, del1, del2,del3;
del = ( st.nextToken() ).charAt( 0 ); // changes string to character
del1 = ( st.nextToken() ).charAt( 0 ); // CHECKS CHARACTER ENTERED.
del2 = ( st.nextToken() ).charAt( 0 );
del3 = ( st.nextToken() ).charAt( 0 );
System.out.println(frow);
System.out.println(fcol);
System.out.print(del);
System.out.print(del1);
System.out.print(del2);
System.out.print(del3);
System.out.println(" ");
// System.err.println("YOU MADE PRIVATE M STATIC CHANGE IT BACK !");
Maze N = new Maze(row,col);
MazeCell q;
q = new MazeCell(MazeCell.char2Wall(del),MazeCell.char2Wall(del1),
MazeCell.char2Wall(del2),MazeCell.char2Wall(del3));
N.initCell(frow, fcol, q.char2Wall(del), q.char2Wall(del1), q.char2Wall(del2), q.char2Wall(del3));
if(q.getTop().equals(MazeCell.WallType.DOOR))
{
System.out.println("You found the door");
}
N.getCell(frow,fcol);
} //END OF FOR LOOP
}
}
catch (IOException e)
{
// catch io errors from FileInputStream or readLine()
System.out.println("Invalid filename !");//
}
}
}
//////////////////////////////////////////////////////////////////////////////