From 81441d7f17bbc9a56c786169a4eab4fb8d7cd179 Mon Sep 17 00:00:00 2001 From: philippe lhardy Date: Wed, 9 Dec 2020 23:02:29 +0100 Subject: [PATCH] Add reverse that allow to switch interior/exterior of a labyrinth - invert create a new labyrinth walls are path of previous. - grow then new labrynth is bigger, else shrink. --- .../games/maze/BrickCharRepr.java | 2 + .../artisanlogiciel/games/maze/LabyModel.java | 79 ++++++++++++++++++- .../games/maze/MovesProvider.java | 11 +++ .../games/maze/gui/Display.java | 21 +++++ lang/LabelsBundle.properties | 3 +- lang/LabelsBundle_fr.properties | 3 +- 6 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 java/org/artisanlogiciel/games/maze/BrickCharRepr.java create mode 100644 java/org/artisanlogiciel/games/maze/MovesProvider.java diff --git a/java/org/artisanlogiciel/games/maze/BrickCharRepr.java b/java/org/artisanlogiciel/games/maze/BrickCharRepr.java new file mode 100644 index 0000000..6debe22 --- /dev/null +++ b/java/org/artisanlogiciel/games/maze/BrickCharRepr.java @@ -0,0 +1,2 @@ +package org.artisanlogiciel.games.maze;public class BrickCharRepr { +} diff --git a/java/org/artisanlogiciel/games/maze/LabyModel.java b/java/org/artisanlogiciel/games/maze/LabyModel.java index 1bb4ec3..76435d7 100644 --- a/java/org/artisanlogiciel/games/maze/LabyModel.java +++ b/java/org/artisanlogiciel/games/maze/LabyModel.java @@ -11,7 +11,8 @@ import java.util.Random; * openList if all moves from its position have not been resolved a node is * tagged CLOSED when fully processed **/ -public class LabyModel implements WallsProvider { +public class LabyModel + implements WallsProvider, MovesProvider { /** * WARNING don't change those values, they are used as it is for @@ -704,6 +705,10 @@ public class LabyModel implements WallsProvider { return path; } + public short getMoves(int x, int y) + { + return getPath(x,y); + } /** * is there a wall in that direction ? **/ @@ -764,4 +769,76 @@ public class LabyModel implements WallsProvider { return onewaywall ? ( ! hasWallInDirection(x,y, (short) (direction << FLAGLENGTH)) ) : ((getWalls(x, y) & direction ) == 0); } + public boolean reverse(boolean grow) + { + // use current moves as walls ... and rebuild new direction from it. + + int deltasize = grow ? 1 : -1; + int newwidth = getWidth() + deltasize; + int newheight = getHeight() + deltasize; + int delta = grow ? 1 : 0; + + if ( ( newwidth < 1) || (newheight < 1 ) ) + { + System.err.println("Can't reverse, too small"); + return false; + } + short newt[][] = new short[newwidth][newheight]; + short[] rdmw = { Brick.RIGHT, Brick.DOWN }; + short[] lumw = { Brick.UP, Brick.LEFT }; + short[] ldmw = { Brick.LEFT, Brick.DOWN }; + short[] urmw = { Brick.UP, Brick.RIGHT }; + + short[] allmoves = { LEFT,UP,RIGHT, DOWN }; + for (int x = 0; x < getWidth() -1 ; x ++) + { + for (int y = 0; y < getHeight() -1 ; y ++) + { + short walls = 0; + short ulwalls = (short) (t[x][y] >> FLAGLENGTH); + short rdwalls = (short) (t[x+1][y+1] >> FLAGLENGTH); + short ldwalls = (short) (t[x][y+1] >> FLAGLENGTH); + short ruwalls = (short) (t[x+1][y] >> FLAGLENGTH); + + // invert moves to walls + for (int i = 0 ; i < 2; i ++) + { + if ((ulwalls & rdmw[i] ) != 0) { + walls |= lumw[i]; + } + if ((rdwalls & lumw[i] ) != 0 ) { + walls |= rdmw[i]; + } + if ((ldwalls & urmw[i] ) != 0) { + walls |= ldmw[i]; + } + if ((ruwalls & ldmw[i] ) != 0 ) { + walls |= urmw[i]; + } + } + // convert walls to moves, walls are actualy where there is no path : XOR to invert. + short moves = (short) ((walls ^ ( Brick.RIGHT | Brick.LEFT | Brick.UP | Brick.DOWN )) << FLAGLENGTH); + short finalmoves = 0; + if ( moves == 0 ) + { + finalmoves = 0; + } + else { + for (short m : allmoves) { + if ((moves & m) != 0) { + finalmoves |= m; + } + } + } + newt[x+delta][y+delta] = finalmoves; + } + } + width = newwidth; + height = newheight; + t = newt; + + return true; + } + + } diff --git a/java/org/artisanlogiciel/games/maze/MovesProvider.java b/java/org/artisanlogiciel/games/maze/MovesProvider.java new file mode 100644 index 0000000..1a1bb78 --- /dev/null +++ b/java/org/artisanlogiciel/games/maze/MovesProvider.java @@ -0,0 +1,11 @@ +package org.artisanlogiciel.games.maze; + +/** + * Get model represented by possible moves + */ +public interface MovesProvider { + + /** return possible moves from this position */ + short getMoves(int x, int y); + +} diff --git a/java/org/artisanlogiciel/games/maze/gui/Display.java b/java/org/artisanlogiciel/games/maze/gui/Display.java index e8b6fcd..d6881cd 100644 --- a/java/org/artisanlogiciel/games/maze/gui/Display.java +++ b/java/org/artisanlogiciel/games/maze/gui/Display.java @@ -45,6 +45,7 @@ implements StatusListener LabyModel model; boolean autoSize; boolean statusEnable = true; + boolean mGrow = false; MazeParams params = null; JTextField statusField = null; @@ -150,6 +151,14 @@ implements StatusListener solving.resolve(solving.getWidth() - 1, solving.getHeight() - 1, maze); } + boolean reverse(boolean grow) { + boolean done = model.reverse(grow); + if ( done ) { + refresh(); + } + return done; + } + void goNorth() { maze.goNorth(); } @@ -513,6 +522,18 @@ implements StatusListener resolveQuitBar.add(resolveButton); + JButton reverseButton = new JButton(labels.getString("reverse")); + reverseButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent evt) { + // + addStatus("Reversing"); + mGrow ^= reverse(mGrow); + } + }); + + resolveQuitBar.add(reverseButton); + + final JButton quitButton = new JButton(labels.getString("quit")); Action quitAction = new AbstractAction() { public void actionPerformed(ActionEvent evt) { diff --git a/lang/LabelsBundle.properties b/lang/LabelsBundle.properties index de03b04..eba5ace 100644 --- a/lang/LabelsBundle.properties +++ b/lang/LabelsBundle.properties @@ -11,4 +11,5 @@ save = Save quit = Quit title = A Maz ing seed = seed -load = Load \ No newline at end of file +load = Load +reverse = Reverse \ No newline at end of file diff --git a/lang/LabelsBundle_fr.properties b/lang/LabelsBundle_fr.properties index cd279bf..e1a44f0 100644 --- a/lang/LabelsBundle_fr.properties +++ b/lang/LabelsBundle_fr.properties @@ -11,4 +11,5 @@ save = Sauver quit = Quitter title = La Bireinte seed = graine -load = Charger \ No newline at end of file +load = Charger +reverse = Inverse \ No newline at end of file