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

@@ -0,0 +1,40 @@
package org.artisanlogiciel.games;
/**
DirectionPosition
record direction and position ( direction as defined in LabyModel ).
**/
public class DirectionPosition
{
private short direction;
private Position position;
// (direction,position)
DirectionPosition(short d, Position p)
{
direction=d;
position=p;
}
short getDirection()
{
return direction;
}
Position getPosition()
{
return position;
}
public String toString()
{
if (position != null)
{
return position.toString();
}
return "?";
}
}

View File

@@ -60,7 +60,7 @@ public class Display extends JFrame
boolean autoSize; boolean autoSize;
MazeParams params = null; MazeParams params = null;
Display(LabyModel model,int W, int H, MazeParams params) Display(LabyModel model,int W, int H, MazeParams params)
{ {
super("Maze display"); super("Maze display");
@@ -553,48 +553,48 @@ public class Display extends JFrame
g.drawLine(x, y, x, y + (int) height); g.drawLine(x, y, x, y + (int) height);
} }
public void drawPath(Graphics g, int pX, int pY, short path) public void drawPath(Graphics g, DirectionPosition dp, int pX, int pY, int mX, int mY)
{ {
int x = offsetX + (int) (pX * width); if ( dp != null )
int y = offsetY + (int) (pY * height);
int xm = x+ (int) (width / 2);
int ym = y+ (int) (height / 2);
if ( (path & LabyModel.HORIZONTAL) == LabyModel.HORIZONTAL)
{ {
g.drawLine(x, ym, x + (int) width, ym); Position dot = dp.getPosition();
if ( (path & LabyModel.POSITIVE) == LabyModel.POSITIVE ) if ((dot.getX() >= pX) && (dot.getY() >= pY) && (dot.getX() < mX) && (dot.getY() < mY))
{ {
g.drawLine(x, ym + (int) (height / 4), x + (int) width, ym); int x = offsetX + (int) (dot.getX() * width);
} int y = offsetY + (int) (dot.getY() * height);
if ( (path & LabyModel.NEGATIVE) == LabyModel.NEGATIVE ) short path = dp.getDirection();
{ int xm = x+ (int) (width / 2);
g.drawLine(x, ym , x + (int) width, ym - (int) (height / 4)); int ym = y+ (int) (height / 2);
} if ( (path & LabyModel.HORIZONTAL) == LabyModel.HORIZONTAL)
{
if ( (path & LabyModel.POSITIVE) == LabyModel.POSITIVE )
{
g.drawLine(xm, ym, x + (int) width, ym);
g.drawLine(xm, ym + (int) (height / 4), x + (int) width, ym);
}
// LEFT /_
if ( (path & LabyModel.NEGATIVE) == LabyModel.NEGATIVE )
{
g.drawLine(x, ym, xm, ym);
g.drawLine(x, ym, xm, ym - (int) (height / 4));
}
}
if ((path & LabyModel.VERTICAL) == LabyModel.VERTICAL)
{
if ( (path & LabyModel.POSITIVE) == LabyModel.POSITIVE )
{
g.drawLine(xm, ym, xm , y + (int) height);
g.drawLine(xm - (int) (width / 4), ym , xm, y + (int) height);
}
// UP |\
if ( (path & LabyModel.NEGATIVE) == LabyModel.NEGATIVE )
{
g.drawLine(xm, ym, xm , y);
g.drawLine(xm + (int) (width /4), ym , xm , y);
}
}
}
} }
if ((path & LabyModel.VERTICAL) == LabyModel.VERTICAL)
{
g.drawLine(xm, y, xm , y + (int) height);
if ( (path & LabyModel.POSITIVE) == LabyModel.POSITIVE )
{
g.drawLine(xm - (int) (width / 4), y , xm, y + (int) height);
}
if ( (path & LabyModel.NEGATIVE) == LabyModel.NEGATIVE )
{
g.drawLine(xm, y , xm + (int) (width /4), y + (int) (height));
}
}
/*
if ((path & LabyModel.HORIZONTAL) ==LabyModel.HORIZONTAL)
{
y=y+ (int) (height / 2);
g.drawLine(x + (int) width, y, x + (int) width, y + (int) height);
}
if ( (pX == 0 ) && ((path & LabyModel.VERTICAL) == LabyModel.VERTICAL))
{
x=x+ (int) (width / 2);
g.drawLine(x, y, x, y + (int) height);
}
*/
} }
public void drawDot(Graphics g, Position dot, int pX, int pY, int mX, int mY) public void drawDot(Graphics g, Position dot, int pX, int pY, int mX, int mY)
@@ -633,7 +633,7 @@ public class Display extends JFrame
LabyModel map; LabyModel map;
final MazeCellParameters cp; final MazeCellParameters cp;
Position current = null; Position current = null;
LinkedList<Position> solvedPath = null; LinkedList<DirectionPosition> solvedPath = null;
final Object lockChange = new Object(); final Object lockChange = new Object();
// current postion of human resolving // current postion of human resolving
int sX = 0; int sX = 0;
@@ -641,6 +641,9 @@ public class Display extends JFrame
// goal exit // goal exit
int gX = -1; int gX = -1;
int gY = -1; int gY = -1;
// not set by default for debug, will show any path
boolean showAll = false;
@Override @Override
public void mouseDragged(MouseEvent e) { public void mouseDragged(MouseEvent e) {
@@ -654,9 +657,14 @@ public class Display extends JFrame
@Override @Override
public void mouseMoved(MouseEvent e) { public void mouseMoved(MouseEvent e) {
System.out.println("Mouse moved (" + e.getX() + ',' + e.getY() + ')'); // System.out.println("Mouse moved (" + e.getX() + ',' + e.getY() + ')');
} }
public void setShowAll(boolean showall )
{
showAll = showAll;
}
void checkExit() void checkExit()
{ {
if ((sX == gX) && (sY == gY)) if ((sX == gX) && (sY == gY))
@@ -846,9 +854,10 @@ public class Display extends JFrame
} }
if (solvedPath != null) if (solvedPath != null)
{ {
for (Position resolved : solvedPath) for (DirectionPosition resolved : solvedPath)
{ {
cp.drawDot(g, resolved, pX, pY, mX, mY); // cp.drawDot(g, resolved.getPosition(), pX, pY, mX, mY);
cp.drawPath(g,resolved, pX, pY, mX, mY);
} }
} }
} }
@@ -868,37 +877,42 @@ public class Display extends JFrame
} }
} }
pX = aX; if (showAll)
pY = aY; {
pX = aX;
pY = aY;
// draw all path within clip bounds horiz first then lines // draw all path within clip bounds horiz first then lines
for (; pY < mY; pY++) for (; pY < mY; pY++)
{
for (pX = 0; pX < mX; pX++)
{
path = map.getPath(pX,pY);
if ( ( path & LabyModel.SOLVED ) == LabyModel.SOLVED )
{ {
g.setColor(Color.green); for (pX = 0; pX < mX; pX++)
{
path = map.getPath(pX,pY);
if ( ( path & LabyModel.SOLVED ) == LabyModel.SOLVED )
{
g.setColor(Color.green);
}
else
{
g.setColor(Color.blue);
}
if ( path != 0 )
{
DirectionPosition dp = new DirectionPosition(path,new Position(pX,pY));
cp.drawPath(g,dp,pX,pY,pX+1,pY+1);
}
}
} }
else }
{ }
g.setColor(Color.blue);
}
if ( path != 0 )
{
cp.drawPath(g,pX,pY,path);
}
}
}
}
public boolean notifySearch(Position pPosition) public boolean notifySearch(DirectionPosition pPosition)
{ {
synchronized (lockChange) synchronized (lockChange)
{ {
current = pPosition; current = pPosition.getPosition();
} }
// should redraw ... // should redraw ...
invalidate(); invalidate();
@@ -912,9 +926,9 @@ public class Display extends JFrame
System.err.println(error); System.err.println(error);
} }
public void notifyCompletion(LinkedList<Position> solvedPath) public void notifyCompletion(LinkedList<DirectionPosition> solvedPath)
{ {
LinkedList<Position> newPath = new LinkedList<>(solvedPath); LinkedList<DirectionPosition> newPath = new LinkedList<>(solvedPath);
System.out.println("resolution completed"); System.out.println("resolution completed");
synchronized (lockChange) synchronized (lockChange)
{ {

View File

@@ -544,29 +544,36 @@ public class LabyModel implements WallsProvider
return false; return false;
} }
public final static boolean isFlagSet(short check, short flag)
{
return ((check & flag) == flag);
}
/** /**
* 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 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; long safeguard = width * height;
short selectedDirection = 0;
int newx = x; int newx = x;
int newy = y; int newy = y;
// list of positions from start to end // 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))) while (!((newx == 0) && (newy == 0)))
{ {
// position that point to (x,y).
Position found = null;
if (( x == newx ) && ( y == newy )) if (( x == newx ) && ( y == newy ))
{ {
System.out.println("[ERROR] path contains same consecutive point" + new Position(newx, newy).toString()); System.out.println("[ERROR] path contains same consecutive point" + new Position(newx, newy).toString());
} }
x = newx; x = newx;
y = newy; 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. // 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 reversedirection = AllDirections[(didx + 2) % 4];
short pointingdirection = DIRECTION; short pointingdirection = DIRECTION;
if ((direction & POSITIVE) == POSITIVE) if (isFlagSet(direction,POSITIVE))
{ {
delta = 1; delta = 1;
pointingdirection |= NEGATIVE; pointingdirection |= NEGATIVE;
@@ -591,7 +598,7 @@ public class LabyModel implements WallsProvider
pointingdirection |= POSITIVE; pointingdirection |= POSITIVE;
} }
if ((direction & HORIZONTAL) == HORIZONTAL) if (isFlagSet(direction,HORIZONTAL))
{ {
newx = x + delta; newx = x + delta;
newy = y; newy = y;
@@ -605,7 +612,7 @@ public class LabyModel implements WallsProvider
} }
// internal GUARD. // internal GUARD.
if ((reversedirection & pointingdirection) != pointingdirection) if (! isFlagSet(reversedirection,pointingdirection))
{ {
System.out.println("Internal ERROR. Please check AllDirections order " System.out.println("Internal ERROR. Please check AllDirections order "
+ (reversedirection & pointingdirection) + " " + pointingdirection); + (reversedirection & pointingdirection) + " " + pointingdirection);
@@ -614,7 +621,7 @@ public class LabyModel implements WallsProvider
if ((newx >= 0) && (newy >= 0) && (newx < width) && (newy < height)) if ((newx >= 0) && (newy >= 0) && (newx < width) && (newy < height))
{ {
if ((t[newx][newy] & reversedirection) == reversedirection) if (isFlagSet(t[newx][newy],reversedirection))
{ {
if (found != null) if (found != null)
{ {
@@ -630,7 +637,8 @@ public class LabyModel implements WallsProvider
} }
return backpath; return backpath;
} }
found = new Position(newx, newy); selectedDirection=reversedirection;
found = new DirectionPosition(selectedDirection,new Position(newx, newy));
if (rlistener != null) if (rlistener != null)
{ {
rlistener.notifySearch(found); rlistener.notifySearch(found);
@@ -646,11 +654,11 @@ public class LabyModel implements WallsProvider
break; break;
} }
// System.out.println(found); // System.out.println(found);
newx = found.getX(); newx = found.getPosition().getX();
newy = found.getY(); newy = found.getPosition().getY();
if ( ( t[newx][newy] & SOLVED ) == SOLVED ) 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 else
{ {

View File

@@ -8,10 +8,10 @@ import java.util.LinkedList;
public interface MazeResolutionListener public interface MazeResolutionListener
{ {
public boolean notifySearch(Position pPosition); public boolean notifySearch(DirectionPosition pPosition);
public void notifySearchError(String error); public void notifySearchError(String error);
public void notifyCompletion(LinkedList<Position> solvedPath); public void notifyCompletion(LinkedList<DirectionPosition> solvedPath);
} }