- Nov 6, 2004
- 324
- 0
- 0
Schoolwork alert! Just looking for explanation, not a solution to the entire thing.
Hi, I am writing a java program. The gist of the following code snippet is a reference to a Cell object that points (ptr) to another Cell object, measures the number of walls in the cell (value numWall) and if it is greater than one will loop. The Cells are arranged in an ArrayList 'in' which means they are part of a maze. They are in order by their Cell id. findReachable Cell returns an integer corresponding to the next reachable cell in the maze not blocked by walls. Anyway, in the following code, I get an infinite loop if I delete the three System.out.println statements, which I didn't really want to include, but I'm willing to if it makes the damn thing work. This is the last part and I have tried various things. What am I overlooking. I am trying to use java references somewhat like a pointer here in C++, but I must be doing it wrong. Yes I know Java doesn't explicitly have pointers. Anyway, thanks for an explanation.
Hi, I am writing a java program. The gist of the following code snippet is a reference to a Cell object that points (ptr) to another Cell object, measures the number of walls in the cell (value numWall) and if it is greater than one will loop. The Cells are arranged in an ArrayList 'in' which means they are part of a maze. They are in order by their Cell id. findReachable Cell returns an integer corresponding to the next reachable cell in the maze not blocked by walls. Anyway, in the following code, I get an infinite loop if I delete the three System.out.println statements, which I didn't really want to include, but I'm willing to if it makes the damn thing work. This is the last part and I have tried various things. What am I overlooking. I am trying to use java references somewhat like a pointer here in C++, but I must be doing it wrong. Yes I know Java doesn't explicitly have pointers. Anyway, thanks for an explanation.
Code:
public void solveMaze(Cell maze[][])
{
Set<Cell> deadEndSet = new TreeSet<Cell>();
Collections.sort(in);
for (Cell i : in)
{
if (i.countCellWalls(i.getWalls()) == 3)
deadEndSet.add(i);
}
for (Cell de : deadEndSet)
{
Cell ptr = in.get(de.getID());
/*
Point to a dead end
*/
int numWall;
do
{
if (pathSet.contains(ptr))
pathSet.remove(ptr);
System.out.println("Ptr points to cell " + ptr.getID());
ptr = in.get(findReachableCell(ptr));
System.out.println("Ptr now points to " + ptr.getID());
numWall = ptr.countCellWalls(ptr.getWalls());
System.out.println("New value of numWall is
+ numWall);
}while (numWall > 1);
}
// ---------------------------------------------------------
// Copying maze solution to ArrayList. Maze is solved.
for (Cell s : pathSet)
{
// if (in.get(s.getID()).getSymbol() != 'S');
in.get(s.getID()).setPath(true);
}
}// end solveMaze()
Last edited: