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);
model.setMazeListener(maze);
model.reset();
// we are in GUI event thread, need to release it and do it outside.
new Thread() {
@@ -151,6 +152,7 @@ public class Display extends JFrame
void resolve()
{
model.reset();
model.resolve(model.getWidth() - 1, model.getHeight() - 1, maze);
}
@@ -446,6 +448,16 @@ public class Display extends JFrame
}
};
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"));
Action quitAction = new AbstractAction() {
@@ -462,6 +474,7 @@ public class Display extends JFrame
resizecontrol.add(slider);
resizecontrol.add(autoSlide);
resizecontrol.add(savePngButton);
resizecontrol.add(saveButton);
resizecontrol.add(quitButton);
add(controlDisplayPanel, BorderLayout.SOUTH);
@@ -1076,6 +1089,7 @@ public class Display extends JFrame
}
}
public static void main(String pArgs[])
{
LabyModel model = null;

View File

@@ -86,6 +86,28 @@ public class LabyModel implements WallsProvider
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)
{
this.width = width;
@@ -414,6 +436,16 @@ public class LabyModel implements WallsProvider
{
depth = 0;
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()
@@ -429,7 +461,8 @@ public class LabyModel implements WallsProvider
for (int x = 0; x < width; x++)
{
// 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 y = p.getY();
if ((t[x][y] & OPEN) != OPEN)
if (isFlagSet(t[x][y],OPEN))
{
check = false;
t[x][y] |= OPEN;
}
if ((t[x][y] & CLOSED) == CLOSED)
if (isFlagSet(t[x][y],CLOSED))
{
check = false;
t[x][y] &= ~CLOSED;
@@ -476,16 +509,15 @@ public class LabyModel implements WallsProvider
{
for (int x = 0; x < width; x++)
{
if ((t[x][y] & CLOSED) == CLOSED)
if (isFlagSet(t[x][y],CLOSED))
{
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));
}
}
}
}
@@ -613,6 +645,10 @@ public class LabyModel implements WallsProvider
short selectedDirection = 0;
int newx = x;
int newy = y;
// list of alternate paths
LinkedList<LinkedList<DirectionPosition>> altpath = new LinkedList<>();
// list of positions from start to end
LinkedList<DirectionPosition> backpath = new LinkedList<DirectionPosition>();
// position that point to (x,y).
@@ -682,24 +718,61 @@ public class LabyModel implements WallsProvider
// from
// 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 ))
{
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("Model Error : two parents " + found.toString() + " "
rlistener.notifySearchError("Loop : two parents " + found.toString() + " "
+ new Position(newx, newy).toString());
}
return backpath;
continue;
}
}
}
selectedDirection=reversedirection;
if (isFlagSet(t[newx][newy], SOLVED ))
{
// was already solved.
found = null;
}
else
{
found = new DirectionPosition(selectedDirection,new Position(newx, newy));
if (rlistener != null)
{
rlistener.notifySearch(found);
}
}
// support multiple pathes
// 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)
{