From b5d27ed694a00be271504da0eae1efbc9d48ac3c Mon Sep 17 00:00:00 2001 From: philippe lhardy Date: Sat, 2 Dec 2017 21:54:51 +0100 Subject: [PATCH] try to handle resolution of alternate path => to check SOLVED, might be set SOVLED while not fully tested. --- java/org/artisanlogiciel/games/Display.java | 14 +++ java/org/artisanlogiciel/games/LabyModel.java | 113 ++++++++++++++---- 2 files changed, 107 insertions(+), 20 deletions(-) diff --git a/java/org/artisanlogiciel/games/Display.java b/java/org/artisanlogiciel/games/Display.java index f09b75e..f4badb3 100644 --- a/java/org/artisanlogiciel/games/Display.java +++ b/java/org/artisanlogiciel/games/Display.java @@ -115,6 +115,7 @@ public class Display extends JFrame maze.resetWallsProvider(model); model.setMazeListener(maze); + model.reset(); // we are in GUI event thread, need to release it and do it outside. new Thread() { @@ -151,6 +152,7 @@ public class Display extends JFrame void resolve() { + model.reset(); model.resolve(model.getWidth() - 1, model.getHeight() - 1, maze); } @@ -446,6 +448,16 @@ public class Display extends JFrame } }; savePngButton.addActionListener(savePngAction); + final JButton saveButton = new JButton(labels.getString("save") +" raw"); + Action saveAction = new AbstractAction() { + public void actionPerformed(ActionEvent evt) + { + // + System.out.println("save"); + save((MazeParamsFixed) params,model); + } + }; + saveButton.addActionListener(saveAction); final JButton quitButton = new JButton(labels.getString("quit")); Action quitAction = new AbstractAction() { @@ -462,6 +474,7 @@ public class Display extends JFrame resizecontrol.add(slider); resizecontrol.add(autoSlide); resizecontrol.add(savePngButton); + resizecontrol.add(saveButton); resizecontrol.add(quitButton); add(controlDisplayPanel, BorderLayout.SOUTH); @@ -1076,6 +1089,7 @@ public class Display extends JFrame } } + public static void main(String pArgs[]) { LabyModel model = null; diff --git a/java/org/artisanlogiciel/games/LabyModel.java b/java/org/artisanlogiciel/games/LabyModel.java index a4364f3..8bf8b7d 100644 --- a/java/org/artisanlogiciel/games/LabyModel.java +++ b/java/org/artisanlogiciel/games/LabyModel.java @@ -86,6 +86,28 @@ public class LabyModel implements WallsProvider MazeCreationListener listener = null; + + /** +TODO + private static MyLinkedList

+ { + MyLinkedList

next; + P content + + MyLinkedList

( P object) + { + content=object; + next=null; + } + + MyLinkedList

addLast(P object) + { + next = new MyLinkedList(object); + return next; + } + } + */ + public LabyModel(int width, int height, int maxdepth, Random random) { this.width = width; @@ -414,6 +436,16 @@ public class LabyModel implements WallsProvider { depth = 0; computeOpenList(); + // don't keep solved path + for (int y = 0; y < height; y++) + { + for (int x = 0; x < width; x++) + { + t[x][y] &= ~(SOLVED); + } + + } + } public void fullReset() @@ -429,7 +461,8 @@ public class LabyModel implements WallsProvider for (int x = 0; x < width; x++) { // resetCell(x,y); - t[x][y] &= ~OPEN; + t[x][y] &= ~( OPEN | SOLVED); + } } @@ -451,12 +484,12 @@ public class LabyModel implements WallsProvider { int x = p.getX(); int y = p.getY(); - if ((t[x][y] & OPEN) != OPEN) + if (isFlagSet(t[x][y],OPEN)) { check = false; t[x][y] |= OPEN; } - if ((t[x][y] & CLOSED) == CLOSED) + if (isFlagSet(t[x][y],CLOSED)) { check = false; t[x][y] &= ~CLOSED; @@ -476,16 +509,15 @@ public class LabyModel implements WallsProvider { for (int x = 0; x < width; x++) { - if ((t[x][y] & CLOSED) == CLOSED) + if (isFlagSet(t[x][y],CLOSED)) { t[x][y] &= ~OPEN; } - if (((t[x][y] & OPEN) == OPEN) || (t[x][y] == CLEAR)) + if (isFlagSet(t[x][y],OPEN) || (t[x][y] == CLEAR)) { openList.add(new Position(x, y)); } } - } } @@ -613,6 +645,10 @@ public class LabyModel implements WallsProvider short selectedDirection = 0; int newx = x; int newy = y; + + // list of alternate paths + LinkedList> altpath = new LinkedList<>(); + // list of positions from start to end LinkedList backpath = new LinkedList(); // position that point to (x,y). @@ -682,30 +718,67 @@ public class LabyModel implements WallsProvider // from // ie entry(0,0) exit(width-1,height-1) not yet // implemented. - if (rlistener != null) - { - rlistener.notifySearchError("Model Error : two parents " + found.toString() + " " - + new Position(newx, newy).toString()); - } - return backpath; + { + int ax = found.getPosition().getX(); + int ay = found.getPosition().getY(); + if (! isFlagSet(t[newx][newy], SOLVED )) + { + LinkedList cp = (LinkedList) backpath.clone(); + DirectionPosition altfound = new DirectionPosition(selectedDirection,new Position(newx, newy)); + cp.addFirst(altfound); + altpath.addLast(cp); + } + else + { + // this was already solved, might be a loop. + if (rlistener != null) + { + rlistener.notifySearchError("Loop : two parents " + found.toString() + " " + + new Position(newx, newy).toString()); + } + continue; + } + } } selectedDirection=reversedirection; - found = new DirectionPosition(selectedDirection,new Position(newx, newy)); - if (rlistener != null) + if (isFlagSet(t[newx][newy], SOLVED )) { - rlistener.notifySearch(found); + // was already solved. + found = null; + } + else + { + found = new DirectionPosition(selectedDirection,new Position(newx, newy)); + if (rlistener != null) + { + rlistener.notifySearch(found); + } } // support multiple pathes // break; } } } + if (found == null) + { + if ( ! altpath.isEmpty() ) + { + // new possible backpath + backpath = altpath.removeFirst(); + found = backpath.removeFirst(); + if (rlistener != null) + { + rlistener.notifySearchError("try alternate path " + found.toString()); + rlistener.notifySearch(found); + } + } + } } - if (found == null) - { - rlistener.notifySearchError("No path found !"); - break; - } + if (found == null) + { + rlistener.notifySearchError("No path found !"); + break; + } // System.out.println(found); newx = found.getPosition().getX(); newy = found.getPosition().getY();