show path with selected direction when resolved.

This commit is contained in:
philippe lhardy
2017-12-01 21:42:02 +01:00
parent 401c7e15d0
commit eeb1172f88
4 changed files with 147 additions and 85 deletions

View File

@@ -544,29 +544,36 @@ public class LabyModel implements WallsProvider
return false;
}
public final static boolean isFlagSet(short check, short flag)
{
return ((check & flag) == flag);
}
/**
* resolve this labrynth using internal representation
* initial (x,y) is exit, will return list of positions from start to end.
**/
public LinkedList<Position> resolve(int x, int y, MazeResolutionListener rlistener)
public LinkedList<DirectionPosition> resolve(int x, int y, MazeResolutionListener rlistener)
{
long safeguard = width * height;
short selectedDirection = 0;
int newx = x;
int newy = y;
// list of positions from start to end
LinkedList<Position> backpath = new LinkedList<Position>();
LinkedList<DirectionPosition> backpath = new LinkedList<DirectionPosition>();
// position that point to (x,y).
DirectionPosition found = new DirectionPosition(selectedDirection,new Position(x, y));
while (!((newx == 0) && (newy == 0)))
{
// position that point to (x,y).
Position found = null;
if (( x == newx ) && ( y == newy ))
{
System.out.println("[ERROR] path contains same consecutive point" + new Position(newx, newy).toString());
}
x = newx;
y = newy;
backpath.addFirst(new Position(x, y));
backpath.addFirst(found);
found = null;
// should find from all adjacent cells (directions) one that point to this.
{
@@ -580,7 +587,7 @@ public class LabyModel implements WallsProvider
short reversedirection = AllDirections[(didx + 2) % 4];
short pointingdirection = DIRECTION;
if ((direction & POSITIVE) == POSITIVE)
if (isFlagSet(direction,POSITIVE))
{
delta = 1;
pointingdirection |= NEGATIVE;
@@ -591,7 +598,7 @@ public class LabyModel implements WallsProvider
pointingdirection |= POSITIVE;
}
if ((direction & HORIZONTAL) == HORIZONTAL)
if (isFlagSet(direction,HORIZONTAL))
{
newx = x + delta;
newy = y;
@@ -605,7 +612,7 @@ public class LabyModel implements WallsProvider
}
// internal GUARD.
if ((reversedirection & pointingdirection) != pointingdirection)
if (! isFlagSet(reversedirection,pointingdirection))
{
System.out.println("Internal ERROR. Please check AllDirections order "
+ (reversedirection & pointingdirection) + " " + pointingdirection);
@@ -614,7 +621,7 @@ public class LabyModel implements WallsProvider
if ((newx >= 0) && (newy >= 0) && (newx < width) && (newy < height))
{
if ((t[newx][newy] & reversedirection) == reversedirection)
if (isFlagSet(t[newx][newy],reversedirection))
{
if (found != null)
{
@@ -630,7 +637,8 @@ public class LabyModel implements WallsProvider
}
return backpath;
}
found = new Position(newx, newy);
selectedDirection=reversedirection;
found = new DirectionPosition(selectedDirection,new Position(newx, newy));
if (rlistener != null)
{
rlistener.notifySearch(found);
@@ -646,11 +654,11 @@ public class LabyModel implements WallsProvider
break;
}
// System.out.println(found);
newx = found.getX();
newy = found.getY();
if ( ( t[newx][newy] & SOLVED ) == SOLVED )
newx = found.getPosition().getX();
newy = found.getPosition().getY();
if (isFlagSet(t[newx][newy], SOLVED ))
{
System.out.println("[INFO] position already solved" + new Position(newx, newy).toString() + " *length:" + backpath.size());
System.out.println("[INFO] position already solved" + found.toString() + " *length:" + backpath.size());
}
else
{