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

View File

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