show path with selected direction when resolved.
This commit is contained in:
40
java/org/artisanlogiciel/games/DirectionPosition.java
Normal file
40
java/org/artisanlogiciel/games/DirectionPosition.java
Normal 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 "?";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -553,48 +553,48 @@ public class Display extends JFrame
|
||||
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);
|
||||
int y = offsetY + (int) (pY * height);
|
||||
if ( dp != null )
|
||||
{
|
||||
Position dot = dp.getPosition();
|
||||
if ((dot.getX() >= pX) && (dot.getY() >= pY) && (dot.getX() < mX) && (dot.getY() < mY))
|
||||
{
|
||||
int x = offsetX + (int) (dot.getX() * width);
|
||||
int y = offsetY + (int) (dot.getY() * height);
|
||||
short path = dp.getDirection();
|
||||
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);
|
||||
if ( (path & LabyModel.POSITIVE) == LabyModel.POSITIVE )
|
||||
{
|
||||
g.drawLine(x, ym + (int) (height / 4), x + (int) width, ym);
|
||||
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 , x + (int) width, ym - (int) (height / 4));
|
||||
g.drawLine(x, ym, xm, ym);
|
||||
g.drawLine(x, ym, xm, ym - (int) (height / 4));
|
||||
}
|
||||
}
|
||||
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);
|
||||
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, y , xm + (int) (width /4), y + (int) (height));
|
||||
g.drawLine(xm, ym, xm , y);
|
||||
g.drawLine(xm + (int) (width /4), ym , xm , y);
|
||||
}
|
||||
}
|
||||
/*
|
||||
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)
|
||||
@@ -633,7 +633,7 @@ public class Display extends JFrame
|
||||
LabyModel map;
|
||||
final MazeCellParameters cp;
|
||||
Position current = null;
|
||||
LinkedList<Position> solvedPath = null;
|
||||
LinkedList<DirectionPosition> solvedPath = null;
|
||||
final Object lockChange = new Object();
|
||||
// current postion of human resolving
|
||||
int sX = 0;
|
||||
@@ -642,6 +642,9 @@ public class Display extends JFrame
|
||||
int gX = -1;
|
||||
int gY = -1;
|
||||
|
||||
// not set by default for debug, will show any path
|
||||
boolean showAll = false;
|
||||
|
||||
@Override
|
||||
public void mouseDragged(MouseEvent e) {
|
||||
// should find the cell ...
|
||||
@@ -654,7 +657,12 @@ public class Display extends JFrame
|
||||
|
||||
@Override
|
||||
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()
|
||||
@@ -846,9 +854,10 @@ public class Display extends JFrame
|
||||
}
|
||||
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,6 +877,9 @@ public class Display extends JFrame
|
||||
}
|
||||
}
|
||||
|
||||
if (showAll)
|
||||
{
|
||||
|
||||
pX = aX;
|
||||
pY = aY;
|
||||
|
||||
@@ -887,18 +899,20 @@ public class Display extends JFrame
|
||||
}
|
||||
if ( path != 0 )
|
||||
{
|
||||
cp.drawPath(g,pX,pY,path);
|
||||
DirectionPosition dp = new DirectionPosition(path,new Position(pX,pY));
|
||||
cp.drawPath(g,dp,pX,pY,pX+1,pY+1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public boolean notifySearch(Position pPosition)
|
||||
public boolean notifySearch(DirectionPosition pPosition)
|
||||
{
|
||||
synchronized (lockChange)
|
||||
{
|
||||
current = pPosition;
|
||||
current = pPosition.getPosition();
|
||||
}
|
||||
// should redraw ...
|
||||
invalidate();
|
||||
@@ -912,9 +926,9 @@ public class Display extends JFrame
|
||||
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");
|
||||
synchronized (lockChange)
|
||||
{
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -8,10 +8,10 @@ import java.util.LinkedList;
|
||||
public interface MazeResolutionListener
|
||||
{
|
||||
|
||||
public boolean notifySearch(Position pPosition);
|
||||
public boolean notifySearch(DirectionPosition pPosition);
|
||||
|
||||
public void notifySearchError(String error);
|
||||
|
||||
public void notifyCompletion(LinkedList<Position> solvedPath);
|
||||
public void notifyCompletion(LinkedList<DirectionPosition> solvedPath);
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user