diff --git a/java/org/artisanlogiciel/games/DirectionPosition.java b/java/org/artisanlogiciel/games/DirectionPosition.java new file mode 100644 index 0000000..4a111c6 --- /dev/null +++ b/java/org/artisanlogiciel/games/DirectionPosition.java @@ -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 "?"; + } + + +} diff --git a/java/org/artisanlogiciel/games/Display.java b/java/org/artisanlogiciel/games/Display.java index d9571ee..e118c8a 100644 --- a/java/org/artisanlogiciel/games/Display.java +++ b/java/org/artisanlogiciel/games/Display.java @@ -60,7 +60,7 @@ public class Display extends JFrame boolean autoSize; MazeParams params = null; - + Display(LabyModel model,int W, int H, MazeParams params) { super("Maze display"); @@ -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); - int xm = x+ (int) (width / 2); - int ym = y+ (int) (height / 2); - if ( (path & LabyModel.HORIZONTAL) == LabyModel.HORIZONTAL) + if ( dp != null ) { - 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); - } - if ( (path & LabyModel.NEGATIVE) == LabyModel.NEGATIVE ) - { - g.drawLine(x, ym , x + (int) width, ym - (int) (height / 4)); - } + 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) + { + 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) @@ -633,7 +633,7 @@ public class Display extends JFrame LabyModel map; final MazeCellParameters cp; Position current = null; - LinkedList solvedPath = null; + LinkedList solvedPath = null; final Object lockChange = new Object(); // current postion of human resolving int sX = 0; @@ -641,6 +641,9 @@ public class Display extends JFrame // goal exit 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) { @@ -654,9 +657,14 @@ 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() { if ((sX == gX) && (sY == gY)) @@ -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,37 +877,42 @@ public class Display extends JFrame } } - pX = aX; - pY = aY; + if (showAll) + { + + pX = aX; + pY = aY; - // draw all path within clip bounds horiz first then lines - for (; pY < mY; pY++) - { - for (pX = 0; pX < mX; pX++) - { - path = map.getPath(pX,pY); - if ( ( path & LabyModel.SOLVED ) == LabyModel.SOLVED ) + // draw all path within clip bounds horiz first then lines + for (; pY < mY; pY++) { - 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) { - 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 solvedPath) + public void notifyCompletion(LinkedList solvedPath) { - LinkedList newPath = new LinkedList<>(solvedPath); + LinkedList newPath = new LinkedList<>(solvedPath); System.out.println("resolution completed"); synchronized (lockChange) { diff --git a/java/org/artisanlogiciel/games/LabyModel.java b/java/org/artisanlogiciel/games/LabyModel.java index 4472716..deabdc5 100644 --- a/java/org/artisanlogiciel/games/LabyModel.java +++ b/java/org/artisanlogiciel/games/LabyModel.java @@ -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 resolve(int x, int y, MazeResolutionListener rlistener) + public LinkedList 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 backpath = new LinkedList(); + LinkedList backpath = new LinkedList(); + // 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 { diff --git a/java/org/artisanlogiciel/games/MazeResolutionListener.java b/java/org/artisanlogiciel/games/MazeResolutionListener.java index 17058db..9c12371 100644 --- a/java/org/artisanlogiciel/games/MazeResolutionListener.java +++ b/java/org/artisanlogiciel/games/MazeResolutionListener.java @@ -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 solvedPath); + public void notifyCompletion(LinkedList solvedPath); }