From a43e1e5a68e0d1e90ce304722aad16fd5e966116 Mon Sep 17 00:00:00 2001 From: philippe lhardy Date: Mon, 4 Dec 2017 22:34:34 +0100 Subject: [PATCH] one step beyong in resolution, still not finding all alternate path add showAll button reset maze size display at maze size change --- java/org/artisanlogiciel/games/Display.java | 27 ++++++-- java/org/artisanlogiciel/games/LabyModel.java | 69 +++++++++++-------- 2 files changed, 63 insertions(+), 33 deletions(-) diff --git a/java/org/artisanlogiciel/games/Display.java b/java/org/artisanlogiciel/games/Display.java index f4badb3..2f652fd 100644 --- a/java/org/artisanlogiciel/games/Display.java +++ b/java/org/artisanlogiciel/games/Display.java @@ -422,6 +422,15 @@ public class Display extends JFrame // control display panel contains controls for display // zoom , autozoom. final JPanel controlDisplayPanel = new JPanel(new BorderLayout()); + final JCheckBox showAll = new JCheckBox("show all"); + showAll.addChangeListener(new ChangeListener() + { + public void stateChanged(ChangeEvent e) + { + maze.setShowAll( showAll.isSelected()); + } + }); + final JSlider slider = new JSlider(2, 40); slider.addChangeListener(new ChangeListener() { @@ -471,6 +480,7 @@ public class Display extends JFrame quitButton.addActionListener(quitAction); resizecontrol = new JPanel(new FlowLayout()); + resizecontrol.add(showAll); resizecontrol.add(slider); resizecontrol.add(autoSlide); resizecontrol.add(savePngButton); @@ -541,6 +551,12 @@ public class Display extends JFrame offsetY = y; } + public void resetMazeWidthHeight(int mapw, int maph) + { + mapWidth = mapw; + mapHeight = maph; + } + public void adaptTo(double W, double H) { double w = ( W - offsetX ) / mapWidth; @@ -750,7 +766,8 @@ public class Display extends JFrame public void setShowAll(boolean showall ) { - this.showAll = showAll; + this.showAll = showall; + System.out.println(showAll ? "show all" : "X"); } void checkExit() @@ -835,9 +852,6 @@ public class Display extends JFrame gY = map.getHeight() - 1; } - /** - warning works only if new map has same dimensions than previous - */ // public void resetWallsProvider(WallsProvider map) public void resetWallsProvider(LabyModel map) { @@ -847,6 +861,7 @@ public class Display extends JFrame drawingPath=null; sX=0; sY=0; + cp.resetMazeWidthHeight(map.getWidth(),map.getHeight()); } public void setWallSize(int size) @@ -970,9 +985,9 @@ public class Display extends JFrame } } - if (showAll) + if (this.showAll) { - + System.out.println("*"); pX = aX; pY = aY; diff --git a/java/org/artisanlogiciel/games/LabyModel.java b/java/org/artisanlogiciel/games/LabyModel.java index 8bf8b7d..27dff48 100644 --- a/java/org/artisanlogiciel/games/LabyModel.java +++ b/java/org/artisanlogiciel/games/LabyModel.java @@ -436,6 +436,11 @@ TODO { depth = 0; computeOpenList(); + resetResolving(); + } + + public void resetResolving() + { // don't keep solved path for (int y = 0; y < height; y++) { @@ -443,7 +448,6 @@ TODO { t[x][y] &= ~(SOLVED); } - } } @@ -601,6 +605,16 @@ TODO return ((check & flag) == flag); } + public final short getCell(Position p) + { + return t[p.getX()][p.getY()]; + } + + public final void updateCell(Position p, short flags) + { + t[p.getX()][p.getY()] |= flags; + } + public final static short getReverseDirection(int index) { return AllDirections[(index + 2) % 4]; @@ -637,14 +651,16 @@ TODO /** * 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 (0,0) to end (x,y) **/ public LinkedList resolve(int x, int y, MazeResolutionListener rlistener) { long safeguard = width * height; short selectedDirection = 0; - int newx = x; - int newy = y; + int newx = 0; + int newy = 0; + + resetResolving(); // list of alternate paths LinkedList> altpath = new LinkedList<>(); @@ -653,15 +669,12 @@ TODO LinkedList backpath = new LinkedList(); // position that point to (x,y). DirectionPosition found = new DirectionPosition(selectedDirection,new Position(x, y)); + // entry + Position entry = new Position(0, 0); - while (!((newx == 0) && (newy == 0))) + while (! found.getPosition().equals(entry) ) { - if (( x == newx ) && ( y == newy )) - { - System.out.println("[ERROR] path contains same consecutive point" + new Position(newx, newy).toString()); - } - x = newx; - y = newy; + Position last = found.getPosition(); backpath.addFirst(found); found = null; @@ -688,14 +701,14 @@ TODO if (isFlagSet(direction,HORIZONTAL)) { - newx = x + delta; - newy = y; + newx = last.getX() + delta; + newy = last.getY(); pointingdirection |= HORIZONTAL; } else { - newy = y + delta; - newx = x; + newy = last.getY() + delta; + newx = last.getX(); pointingdirection |= VERTICAL; } @@ -707,9 +720,11 @@ TODO return backpath; } + Position p = new Position(newx,newy); if ((newx >= 0) && (newy >= 0) && (newx < width) && (newy < height)) { - if (isFlagSet(t[newx][newy],reversedirection)) + System.out.println("check " + p); + if (isFlagSet(getCell(p),reversedirection)) { if (found != null) { @@ -719,12 +734,10 @@ TODO // ie entry(0,0) exit(width-1,height-1) not yet // implemented. { - int ax = found.getPosition().getX(); - int ay = found.getPosition().getY(); - if (! isFlagSet(t[newx][newy], SOLVED )) + if (! isFlagSet(getCell(p), SOLVED )) { LinkedList cp = (LinkedList) backpath.clone(); - DirectionPosition altfound = new DirectionPosition(selectedDirection,new Position(newx, newy)); + DirectionPosition altfound = new DirectionPosition(reversedirection,p); cp.addFirst(altfound); altpath.addLast(cp); } @@ -734,21 +747,21 @@ TODO if (rlistener != null) { rlistener.notifySearchError("Loop : two parents " + found.toString() + " " - + new Position(newx, newy).toString()); + + p.toString()); } continue; } } } selectedDirection=reversedirection; - if (isFlagSet(t[newx][newy], SOLVED )) + if (isFlagSet(getCell(p), SOLVED )) { // was already solved. found = null; } else { - found = new DirectionPosition(selectedDirection,new Position(newx, newy)); + found = new DirectionPosition(selectedDirection,p); if (rlistener != null) { rlistener.notifySearch(found); @@ -758,6 +771,10 @@ TODO // break; } } + else + { + System.out.println("p outofbounds " + p); + } } if (found == null) { @@ -780,15 +797,13 @@ TODO break; } // System.out.println(found); - newx = found.getPosition().getX(); - newy = found.getPosition().getY(); - if (isFlagSet(t[newx][newy], SOLVED )) + if (isFlagSet(getCell(found.getPosition()), SOLVED )) { System.out.println("[INFO] position already solved" + found.toString() + " *length:" + backpath.size()); } else { - t[newx][newy] |= SOLVED; + updateCell(found.getPosition(),SOLVED); }