one step beyong in resolution, still not finding all alternate path

add showAll button
reset maze size display at maze size change
This commit is contained in:
philippe lhardy
2017-12-04 22:34:34 +01:00
parent b5d27ed694
commit a43e1e5a68
2 changed files with 63 additions and 33 deletions

View File

@@ -422,6 +422,15 @@ public class Display extends JFrame
// control display panel contains controls for display
// zoom , autozoom.
final JPanel controlDisplayPanel = new JPanel(new BorderLayout());
final JCheckBox showAll = new JCheckBox("show all");
showAll.addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent e)
{
maze.setShowAll( showAll.isSelected());
}
});
final JSlider slider = new JSlider(2, 40);
slider.addChangeListener(new ChangeListener()
{
@@ -471,6 +480,7 @@ public class Display extends JFrame
quitButton.addActionListener(quitAction);
resizecontrol = new JPanel(new FlowLayout());
resizecontrol.add(showAll);
resizecontrol.add(slider);
resizecontrol.add(autoSlide);
resizecontrol.add(savePngButton);
@@ -541,6 +551,12 @@ public class Display extends JFrame
offsetY = y;
}
public void resetMazeWidthHeight(int mapw, int maph)
{
mapWidth = mapw;
mapHeight = maph;
}
public void adaptTo(double W, double H)
{
double w = ( W - offsetX ) / mapWidth;
@@ -750,7 +766,8 @@ public class Display extends JFrame
public void setShowAll(boolean showall )
{
this.showAll = showAll;
this.showAll = showall;
System.out.println(showAll ? "show all" : "X");
}
void checkExit()
@@ -835,9 +852,6 @@ public class Display extends JFrame
gY = map.getHeight() - 1;
}
/**
warning works only if new map has same dimensions than previous
*/
// public void resetWallsProvider(WallsProvider map)
public void resetWallsProvider(LabyModel map)
{
@@ -847,6 +861,7 @@ public class Display extends JFrame
drawingPath=null;
sX=0;
sY=0;
cp.resetMazeWidthHeight(map.getWidth(),map.getHeight());
}
public void setWallSize(int size)
@@ -970,9 +985,9 @@ public class Display extends JFrame
}
}
if (showAll)
if (this.showAll)
{
System.out.println("*");
pX = aX;
pY = aY;

View File

@@ -436,6 +436,11 @@ TODO
{
depth = 0;
computeOpenList();
resetResolving();
}
public void resetResolving()
{
// don't keep solved path
for (int y = 0; y < height; y++)
{
@@ -443,7 +448,6 @@ TODO
{
t[x][y] &= ~(SOLVED);
}
}
}
@@ -601,6 +605,16 @@ TODO
return ((check & flag) == flag);
}
public final short getCell(Position p)
{
return t[p.getX()][p.getY()];
}
public final void updateCell(Position p, short flags)
{
t[p.getX()][p.getY()] |= flags;
}
public final static short getReverseDirection(int index)
{
return AllDirections[(index + 2) % 4];
@@ -637,14 +651,16 @@ TODO
/**
* resolve this labrynth using internal representation
* initial (x,y) is exit, will return list of positions from start to end.
* initial (x,y) is exit, will return list of positions from start (0,0) to end (x,y)
**/
public LinkedList<DirectionPosition> resolve(int x, int y, MazeResolutionListener rlistener)
{
long safeguard = width * height;
short selectedDirection = 0;
int newx = x;
int newy = y;
int newx = 0;
int newy = 0;
resetResolving();
// list of alternate paths
LinkedList<LinkedList<DirectionPosition>> altpath = new LinkedList<>();
@@ -653,15 +669,12 @@ TODO
LinkedList<DirectionPosition> backpath = new LinkedList<DirectionPosition>();
// position that point to (x,y).
DirectionPosition found = new DirectionPosition(selectedDirection,new Position(x, y));
// entry
Position entry = new Position(0, 0);
while (!((newx == 0) && (newy == 0)))
while (! found.getPosition().equals(entry) )
{
if (( x == newx ) && ( y == newy ))
{
System.out.println("[ERROR] path contains same consecutive point" + new Position(newx, newy).toString());
}
x = newx;
y = newy;
Position last = found.getPosition();
backpath.addFirst(found);
found = null;
@@ -688,14 +701,14 @@ TODO
if (isFlagSet(direction,HORIZONTAL))
{
newx = x + delta;
newy = y;
newx = last.getX() + delta;
newy = last.getY();
pointingdirection |= HORIZONTAL;
}
else
{
newy = y + delta;
newx = x;
newy = last.getY() + delta;
newx = last.getX();
pointingdirection |= VERTICAL;
}
@@ -707,9 +720,11 @@ TODO
return backpath;
}
Position p = new Position(newx,newy);
if ((newx >= 0) && (newy >= 0) && (newx < width) && (newy < height))
{
if (isFlagSet(t[newx][newy],reversedirection))
System.out.println("check " + p);
if (isFlagSet(getCell(p),reversedirection))
{
if (found != null)
{
@@ -719,12 +734,10 @@ TODO
// ie entry(0,0) exit(width-1,height-1) not yet
// implemented.
{
int ax = found.getPosition().getX();
int ay = found.getPosition().getY();
if (! isFlagSet(t[newx][newy], SOLVED ))
if (! isFlagSet(getCell(p), SOLVED ))
{
LinkedList<DirectionPosition> cp = (LinkedList<DirectionPosition>) backpath.clone();
DirectionPosition altfound = new DirectionPosition(selectedDirection,new Position(newx, newy));
DirectionPosition altfound = new DirectionPosition(reversedirection,p);
cp.addFirst(altfound);
altpath.addLast(cp);
}
@@ -734,21 +747,21 @@ TODO
if (rlistener != null)
{
rlistener.notifySearchError("Loop : two parents " + found.toString() + " "
+ new Position(newx, newy).toString());
+ p.toString());
}
continue;
}
}
}
selectedDirection=reversedirection;
if (isFlagSet(t[newx][newy], SOLVED ))
if (isFlagSet(getCell(p), SOLVED ))
{
// was already solved.
found = null;
}
else
{
found = new DirectionPosition(selectedDirection,new Position(newx, newy));
found = new DirectionPosition(selectedDirection,p);
if (rlistener != null)
{
rlistener.notifySearch(found);
@@ -758,6 +771,10 @@ TODO
// break;
}
}
else
{
System.out.println("p outofbounds " + p);
}
}
if (found == null)
{
@@ -780,15 +797,13 @@ TODO
break;
}
// System.out.println(found);
newx = found.getPosition().getX();
newy = found.getPosition().getY();
if (isFlagSet(t[newx][newy], SOLVED ))
if (isFlagSet(getCell(found.getPosition()), SOLVED ))
{
System.out.println("[INFO] position already solved" + found.toString() + " *length:" + backpath.size());
}
else
{
t[newx][newy] |= SOLVED;
updateCell(found.getPosition(),SOLVED);
}