try to handle resolution of alternate path

=> to check SOLVED, might be set SOVLED while not fully tested.
This commit is contained in:
philippe lhardy
2017-12-02 21:54:51 +01:00
parent 52d244c758
commit b5d27ed694
2 changed files with 107 additions and 20 deletions

View File

@@ -115,6 +115,7 @@ public class Display extends JFrame
maze.resetWallsProvider(model); maze.resetWallsProvider(model);
model.setMazeListener(maze); model.setMazeListener(maze);
model.reset();
// we are in GUI event thread, need to release it and do it outside. // we are in GUI event thread, need to release it and do it outside.
new Thread() { new Thread() {
@@ -151,6 +152,7 @@ public class Display extends JFrame
void resolve() void resolve()
{ {
model.reset();
model.resolve(model.getWidth() - 1, model.getHeight() - 1, maze); model.resolve(model.getWidth() - 1, model.getHeight() - 1, maze);
} }
@@ -446,6 +448,16 @@ public class Display extends JFrame
} }
}; };
savePngButton.addActionListener(savePngAction); savePngButton.addActionListener(savePngAction);
final JButton saveButton = new JButton(labels.getString("save") +" raw");
Action saveAction = new AbstractAction() {
public void actionPerformed(ActionEvent evt)
{
//
System.out.println("save");
save((MazeParamsFixed) params,model);
}
};
saveButton.addActionListener(saveAction);
final JButton quitButton = new JButton(labels.getString("quit")); final JButton quitButton = new JButton(labels.getString("quit"));
Action quitAction = new AbstractAction() { Action quitAction = new AbstractAction() {
@@ -462,6 +474,7 @@ public class Display extends JFrame
resizecontrol.add(slider); resizecontrol.add(slider);
resizecontrol.add(autoSlide); resizecontrol.add(autoSlide);
resizecontrol.add(savePngButton); resizecontrol.add(savePngButton);
resizecontrol.add(saveButton);
resizecontrol.add(quitButton); resizecontrol.add(quitButton);
add(controlDisplayPanel, BorderLayout.SOUTH); add(controlDisplayPanel, BorderLayout.SOUTH);
@@ -1076,6 +1089,7 @@ public class Display extends JFrame
} }
} }
public static void main(String pArgs[]) public static void main(String pArgs[])
{ {
LabyModel model = null; LabyModel model = null;

View File

@@ -86,6 +86,28 @@ public class LabyModel implements WallsProvider
MazeCreationListener listener = null; MazeCreationListener listener = null;
/**
TODO
private static MyLinkedList<P>
{
MyLinkedList<P> next;
P content
MyLinkedList<P>( P object)
{
content=object;
next=null;
}
MyLinkedList<P> addLast(P object)
{
next = new MyLinkedList(object);
return next;
}
}
*/
public LabyModel(int width, int height, int maxdepth, Random random) public LabyModel(int width, int height, int maxdepth, Random random)
{ {
this.width = width; this.width = width;
@@ -414,6 +436,16 @@ public class LabyModel implements WallsProvider
{ {
depth = 0; depth = 0;
computeOpenList(); computeOpenList();
// don't keep solved path
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
t[x][y] &= ~(SOLVED);
}
}
} }
public void fullReset() public void fullReset()
@@ -429,7 +461,8 @@ public class LabyModel implements WallsProvider
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {
// resetCell(x,y); // resetCell(x,y);
t[x][y] &= ~OPEN; t[x][y] &= ~( OPEN | SOLVED);
} }
} }
@@ -451,12 +484,12 @@ public class LabyModel implements WallsProvider
{ {
int x = p.getX(); int x = p.getX();
int y = p.getY(); int y = p.getY();
if ((t[x][y] & OPEN) != OPEN) if (isFlagSet(t[x][y],OPEN))
{ {
check = false; check = false;
t[x][y] |= OPEN; t[x][y] |= OPEN;
} }
if ((t[x][y] & CLOSED) == CLOSED) if (isFlagSet(t[x][y],CLOSED))
{ {
check = false; check = false;
t[x][y] &= ~CLOSED; t[x][y] &= ~CLOSED;
@@ -476,16 +509,15 @@ public class LabyModel implements WallsProvider
{ {
for (int x = 0; x < width; x++) for (int x = 0; x < width; x++)
{ {
if ((t[x][y] & CLOSED) == CLOSED) if (isFlagSet(t[x][y],CLOSED))
{ {
t[x][y] &= ~OPEN; t[x][y] &= ~OPEN;
} }
if (((t[x][y] & OPEN) == OPEN) || (t[x][y] == CLEAR)) if (isFlagSet(t[x][y],OPEN) || (t[x][y] == CLEAR))
{ {
openList.add(new Position(x, y)); openList.add(new Position(x, y));
} }
} }
} }
} }
@@ -613,6 +645,10 @@ public class LabyModel implements WallsProvider
short selectedDirection = 0; short selectedDirection = 0;
int newx = x; int newx = x;
int newy = y; int newy = y;
// list of alternate paths
LinkedList<LinkedList<DirectionPosition>> altpath = new LinkedList<>();
// list of positions from start to end // list of positions from start to end
LinkedList<DirectionPosition> backpath = new LinkedList<DirectionPosition>(); LinkedList<DirectionPosition> backpath = new LinkedList<DirectionPosition>();
// position that point to (x,y). // position that point to (x,y).
@@ -682,30 +718,67 @@ public class LabyModel implements WallsProvider
// from // from
// ie entry(0,0) exit(width-1,height-1) not yet // ie entry(0,0) exit(width-1,height-1) not yet
// implemented. // implemented.
if (rlistener != null) {
{ int ax = found.getPosition().getX();
rlistener.notifySearchError("Model Error : two parents " + found.toString() + " " int ay = found.getPosition().getY();
+ new Position(newx, newy).toString()); if (! isFlagSet(t[newx][newy], SOLVED ))
} {
return backpath; LinkedList<DirectionPosition> cp = (LinkedList<DirectionPosition>) backpath.clone();
DirectionPosition altfound = new DirectionPosition(selectedDirection,new Position(newx, newy));
cp.addFirst(altfound);
altpath.addLast(cp);
}
else
{
// this was already solved, might be a loop.
if (rlistener != null)
{
rlistener.notifySearchError("Loop : two parents " + found.toString() + " "
+ new Position(newx, newy).toString());
}
continue;
}
}
} }
selectedDirection=reversedirection; selectedDirection=reversedirection;
found = new DirectionPosition(selectedDirection,new Position(newx, newy)); if (isFlagSet(t[newx][newy], SOLVED ))
if (rlistener != null)
{ {
rlistener.notifySearch(found); // was already solved.
found = null;
}
else
{
found = new DirectionPosition(selectedDirection,new Position(newx, newy));
if (rlistener != null)
{
rlistener.notifySearch(found);
}
} }
// support multiple pathes // support multiple pathes
// break; // break;
} }
} }
} }
if (found == null)
{
if ( ! altpath.isEmpty() )
{
// new possible backpath
backpath = altpath.removeFirst();
found = backpath.removeFirst();
if (rlistener != null)
{
rlistener.notifySearchError("try alternate path " + found.toString());
rlistener.notifySearch(found);
}
}
}
} }
if (found == null) if (found == null)
{ {
rlistener.notifySearchError("No path found !"); rlistener.notifySearchError("No path found !");
break; break;
} }
// System.out.println(found); // System.out.println(found);
newx = found.getPosition().getX(); newx = found.getPosition().getX();
newy = found.getPosition().getY(); newy = found.getPosition().getY();