diff --git a/java/org/artisanlogiciel/games/DirectionPosition.java b/java/org/artisanlogiciel/games/DirectionPosition.java index 4a111c6..47351e1 100644 --- a/java/org/artisanlogiciel/games/DirectionPosition.java +++ b/java/org/artisanlogiciel/games/DirectionPosition.java @@ -22,6 +22,11 @@ public class DirectionPosition return direction; } + void setDirection(short d) + { + direction = d; + } + Position getPosition() { return position; diff --git a/java/org/artisanlogiciel/games/Display.java b/java/org/artisanlogiciel/games/Display.java index 8822ade..6074000 100644 --- a/java/org/artisanlogiciel/games/Display.java +++ b/java/org/artisanlogiciel/games/Display.java @@ -649,8 +649,10 @@ public class Display extends JFrame @Override public void mouseDragged(MouseEvent e) { // should find the cell ... + DirectionPosition last = null; int pX = (int) ((double) (e.getX() - cp.getOffsetX()) / cp.getWidth()); int pY = (int) ((double) (e.getY() - cp.getOffsetY()) / cp.getHeight()); + short path = 0; Position newPosition = new Position(pX,pY); if (drawingPath == null ) { @@ -658,17 +660,21 @@ public class Display extends JFrame } else { - DirectionPosition last = drawingPath.getLast(); + // setShowAll(true); + last = drawingPath.getLast(); if ( last.getPosition().equals(newPosition)) { // no move. return; } - // short path=computeMove(last,newPosition); + path=LabyModel.getDirection(last.getPosition(),newPosition); + last.setDirection(path); + map.setDirection(last.getPosition().getX(),last.getPosition().getY(),path); } + last = new DirectionPosition((short) 0,newPosition); System.out.println("Mouse dragged Cell " + newPosition); - drawingPath.addLast(new DirectionPosition((short) 0,newPosition)); - map.noWalls(pX,pY); + drawingPath.addLast(last); + // map.noWalls(pX,pY); changed(null,null,map); } @@ -679,7 +685,7 @@ public class Display extends JFrame public void setShowAll(boolean showall ) { - showAll = showAll; + this.showAll = showAll; } void checkExit() diff --git a/java/org/artisanlogiciel/games/LabyModel.java b/java/org/artisanlogiciel/games/LabyModel.java index 65b8b96..291b4a7 100644 --- a/java/org/artisanlogiciel/games/LabyModel.java +++ b/java/org/artisanlogiciel/games/LabyModel.java @@ -127,6 +127,26 @@ public class LabyModel implements WallsProvider } } + /* set direction existing onee are lost */ + // FIXME FULLY BREAK RESOLVING... + public void setDirection(int x, int y, short path) + { + if ((x > 0) && (x < width) && (y > 0) && (y < height)) + { + t[x][y]= path; + } + } + + /** add a new direction, exiting ones are kept */ + // FIXME FULLY BREAK RESOLVING... + public void addDirection(int x, int y, short path) + { + if ((x > 0) && (x < width) && (y > 0) && (y < height)) + { + t[x][y]|= path; + } + } + // entry and exit can be outside the model boundaries a one x or one y out. public boolean addEntryOrExit(int x, int y) { @@ -548,6 +568,40 @@ public class LabyModel implements WallsProvider { return ((check & flag) == flag); } + + public final static short getReverseDirection(int index) + { + return AllDirections[(index + 2) % 4]; + } + + /** return direction to use to be closer to 'to' from 'from' */ + public final static short getDirection(Position from, Position to) + { + short pointingdirection = DIRECTION; + if ( from.equals(to) ) + { + return pointingdirection; + } + if ( from.getX() < to.getX() ) + { + pointingdirection |= RIGHT; + } + else + if ( from.getX() > to.getX() ) + { + pointingdirection |= LEFT; + } + if ( from.getY() < to.getY() ) + { + pointingdirection |= DOWN; + } + else + if ( from.getY() > to.getY() ) + { + pointingdirection |= UP; + } + return pointingdirection; + } /** * resolve this labrynth using internal representation @@ -575,16 +629,14 @@ public class LabyModel implements WallsProvider 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. { - ArrayList freeDirection = new ArrayList(); - // didx is index of four cell adjacent to this (x,y) for (int didx = 0; didx < 4; didx++) { int delta = 0; short direction = AllDirections[didx]; - short reversedirection = AllDirections[(didx + 2) % 4]; + short reversedirection = getReverseDirection(didx); short pointingdirection = DIRECTION; if (isFlagSet(direction,POSITIVE)) @@ -902,7 +954,7 @@ public class LabyModel implements WallsProvider if ((newx >= 0) && (newy >= 0) && (newx < width) && (newy < height)) { - return ((t[newx][newy] & reversedirection) != reversedirection); + return !isFlagSet(t[newx][newy],(short)reversedirection); } else {