From fbe4a3e1335e45560757d1e69c3e522fc90f0050 Mon Sep 17 00:00:00 2001 From: philippe lhardy Date: Thu, 14 Jun 2018 22:49:39 +0200 Subject: [PATCH] LabyModel limit public constructors to LabyModel(MazeParams params, Random random) and LabyModel(String pFormat, InputStream pIn) fix unchecked and (java9) deprecated Short(short) --- build.xml | 1 + java/org/artisanlogiciel/games/Display.java | 65 ------------ java/org/artisanlogiciel/games/LabyModel.java | 98 ++++++++++--------- java/org/artisanlogiciel/games/Main.java | 22 +---- .../games/MazeParamsFixed.java | 69 +++++++++++++ 5 files changed, 128 insertions(+), 127 deletions(-) create mode 100644 java/org/artisanlogiciel/games/MazeParamsFixed.java diff --git a/build.xml b/build.xml index 8242244..f1b2f16 100644 --- a/build.xml +++ b/build.xml @@ -22,6 +22,7 @@ + diff --git a/java/org/artisanlogiciel/games/Display.java b/java/org/artisanlogiciel/games/Display.java index b83903d..c87b40b 100644 --- a/java/org/artisanlogiciel/games/Display.java +++ b/java/org/artisanlogiciel/games/Display.java @@ -429,71 +429,6 @@ public class Display extends JFrame } - private static class MazeParamsFixed implements MazeParams - { - int width; - int height; - int maxdepth; - File labdir; - String name; - - public MazeParamsFixed() - { - } - - public MazeParamsFixed(MazeParams params) - { - setParams(params.getSaveDir(),params.getWidth(),params.getHeight(),params.getMaxDepth()); - } - - public void setParams(File saveDir, int W, int H, int MD) - { - labdir = saveDir; - width=W; - height=H; - maxdepth=MD; - } - - public MazeParamsFixed(File saveDir, int W, int H, int MD) - { - name = null; - setParams(saveDir,W,H,MD); - } - - public int getWidth() - { - return width; - } - - public int getHeight() - { - return height; - } - - public int getMaxDepth() - { - return maxdepth; - } - - public void setName(String n) - { - name = n; - } - - public String getName() - { - if (name == null) - { - name = "lab" + width + "x" + height; - } - return name; - } - - public File getSaveDir() - { - return labdir; - } - } private class MazeControler extends JPanel diff --git a/java/org/artisanlogiciel/games/LabyModel.java b/java/org/artisanlogiciel/games/LabyModel.java index aec3358..3ace732 100644 --- a/java/org/artisanlogiciel/games/LabyModel.java +++ b/java/org/artisanlogiciel/games/LabyModel.java @@ -33,9 +33,11 @@ public class LabyModel implements WallsProvider public final static short DIRECTION = 4; // could we get rid of that to // free one bit for other purpose // ? + // can be both POSITIVE and NEGATIVE, it means that you can move in positive direction and in negative direction. public final static short POSITIVE = 8; public final static short NEGATIVE = 16; - + + // can it be both OPEN and CLOSED ? private final static short OPEN = 32; // can be reused once generation is // completed private final static short CLOSED = 64; // can be reused once generation is @@ -44,25 +46,23 @@ public class LabyModel implements WallsProvider public final static short DOWN = Brick.DOWN << FLAGLENGTH | DIRECTION | VERTICAL | POSITIVE; public final static short RIGHT = Brick.RIGHT << FLAGLENGTH | DIRECTION | HORIZONTAL | POSITIVE; public final static short UP = Brick.UP << FLAGLENGTH | DIRECTION | VERTICAL | NEGATIVE; - public final static short ENTRY = Brick.ENTRY << FLAGLENGTH; // flag when a - // wall should - // be open to - // access - // this. - private final static short GOAL = Brick.GOAL << FLAGLENGTH; // flag when a - // wall should - // be open to - // access this. - public final static short SOLVED = 64 << FLAGLENGTH; // flag when solution - // is on this path. - private final static short FREE = 128 << FLAGLENGTH; // free flag + + // flag when a wall should be open to access this for entry + public final static short ENTRY = Brick.ENTRY << FLAGLENGTH; + // flag when a wall should be open to access this for exit + private final static short GOAL = Brick.GOAL << FLAGLENGTH; + // flag when solution is on this path. + public final static short SOLVED = 64 << FLAGLENGTH; + // free flag + private final static short FREE = 128 << FLAGLENGTH; // remains 2 free bits ( keep one out for sign ) + // orders matters see getReverseDirection private static final short[] AllDirections = { LEFT, DOWN, RIGHT, UP }; private int width; private int height; - // wall flags + status flags for each position (x,y) + // wall flags Brick.(LEFT,DOWN,RIGHT,UP,ENTRY,GOAL) + status flags for each position (x,y) private short[][] t; private int depth = 0; /** @@ -71,11 +71,15 @@ public class LabyModel implements WallsProvider * that to shorter maxdepth is the harder the labyrinth is ... **/ private int maxdepth = 0; - private int deepest = 0; // longest path found + // longest path found + private int deepest = 0; + // each move is a linearwork step private int linearwork = 0; + private Position deepestEnd = null; boolean maxreached = false; java.util.Random random; + // list of positions not fully walked private final LinkedList openList = new LinkedList(); // list of entries and exits. private final LinkedList entryExits = new LinkedList(); @@ -87,7 +91,7 @@ public class LabyModel implements WallsProvider MazeCreationListener listener = null; - public LabyModel(int width, int height, int maxdepth, Random random) + private LabyModel(int width, int height, int maxdepth, Random random) { this.width = width; this.height = height; @@ -99,14 +103,12 @@ public class LabyModel implements WallsProvider public LabyModel(MazeParams params, Random random) { - this.width = params.getWidth(); - this.height = params.getHeight(); - this.maxdepth = params.getMaxDepth(); - this.random = random; - // CLEAR == 0 and array is initialized with 0s - t = new short[width][height]; + this(params.getWidth(),params.getHeight(),params.getMaxDepth(),random); } + /** + construct LabyModel from an InputStream, yet only "raw" is supported + **/ public LabyModel(String pFormat, InputStream pIn) throws IOException { parseInputStream(pFormat, pIn); @@ -123,7 +125,6 @@ public class LabyModel implements WallsProvider if ((x >= 0) && (x < width) && (y >= 0) && (y < height)) { // t[x][y] |= DIRECTION | VERTICAL | POSITIVE | HORIZONTAL | NEGATIVE | LEFT | RIGHT | UP | DOWN; - t[x][y] |= DIRECTION | VERTICAL | POSITIVE | HORIZONTAL | NEGATIVE; t[x][y] |= LEFT | RIGHT | UP | DOWN; } } @@ -392,7 +393,7 @@ public class LabyModel implements WallsProvider brickMap[x][y] = new Brick(out2XY(x, y), outHorizWall2XY(x, y), getWalls(x, y)); } } - return new LabyMap(brickMap, (LinkedList) entryExits.clone()); + return new LabyMap(brickMap, new LinkedList(entryExits)); } } @@ -444,8 +445,7 @@ public class LabyModel implements WallsProvider for (int x = 0; x < width; x++) { // resetCell(x,y); - t[x][y] &= ~( OPEN | SOLVED); - + t[x][y] &= ~( OPEN | SOLVED); } } @@ -485,6 +485,7 @@ public class LabyModel implements WallsProvider return check; } + // coherency : if CLOSED then not OPEN public void computeOpenList() { openList.clear(); @@ -505,7 +506,7 @@ public class LabyModel implements WallsProvider } /** - generate a maze with an entry set at (x,y) + generate a maze with an entry set at (x,y) with a maxdepth **/ public void generateWithEntry(int x, int y) { @@ -696,7 +697,7 @@ public class LabyModel implements WallsProvider // internal GUARD. if (! isFlagSet(reversedirection,pointingdirection)) { - System.out.println("Internal ERROR. Please check AllDirections order " + System.out.println("[FATAL] Internal ERROR. Please check AllDirections order " + (reversedirection & pointingdirection) + " " + pointingdirection); return backpath; } @@ -718,7 +719,7 @@ public class LabyModel implements WallsProvider { if (! isFlagSet(getCell(p), SOLVED )) { - LinkedList cp = (LinkedList) backpath.clone(); + LinkedList cp = new LinkedList(backpath); DirectionPosition altfound = new DirectionPosition(reversedirection,p); cp.addFirst(altfound); altpath.addLast(cp); @@ -884,7 +885,7 @@ public class LabyModel implements WallsProvider { if ((t[newx][newy]) == CLEAR) { - freeDirection.add(new Short(direction)); + freeDirection.add(Short.valueOf(direction)); } } } @@ -1087,30 +1088,37 @@ public class LabyModel implements WallsProvider { throw new IOException("Use correct constructor."); } - + private void parseInputStream(String pFormat, InputStream pIn) throws IOException { if ((pFormat == null) || (pFormat.equals("raw"))) { + // maxdepth is unset then unmodified byte[] header = new byte[4]; DataInputStream in = new DataInputStream(pIn); in.read(header); int rwidth = in.readInt(); int rheight = in.readInt(); - width = rwidth; - height = rheight; - maxdepth = maxdepth; - random = null; - // SHOULD CHECK max width and max height... - // CLEAR == 0 and array is initialized with 0s - t = new short[width][height]; - for (int y = 0; y < height; y++) - { - for (int x = 0; x < width; x++) - { - t[x][y] = in.readShort(); - } - } + if ( (rwidth > 0) && (rheight > 0 ) ) + { + width = rwidth; + height = rheight; + random = null; + // SHOULD CHECK max width and max height... + // CLEAR == 0 and array is initialized with 0s + t = new short[width][height]; + for (int y = 0; y < height; y++) + { + for (int x = 0; x < width; x++) + { + t[x][y] = in.readShort(); + } + } + } + else + { + throw new IOException("Invalid header for width and height"); + } // should be at end of stream ? Not necessary can stream multiple // labs ( or tiling ). } diff --git a/java/org/artisanlogiciel/games/Main.java b/java/org/artisanlogiciel/games/Main.java index 0aa7afd..91864d5 100644 --- a/java/org/artisanlogiciel/games/Main.java +++ b/java/org/artisanlogiciel/games/Main.java @@ -14,25 +14,13 @@ public class Main return editor; } - public WallsProvider generate(int width, int height, int maxdepth, MazeResolutionListener listener) + public LabyMap generate2(MazeParamEditor params) { - LabyModel model = new LabyModel(width, height, maxdepth, new java.util.Random()); + LabyModel model = new LabyModel(params, new java.util.Random(1024L)); model.generateWithEntry(0, 0); - LinkedList exits = new LinkedList(); - model.addEntryOrExit(-1, 0); - model.addEntryOrExit(width, height - 1); - if (!model.check()) - { - System.out.println("Check failed"); - } - model.resolve(width - 1, height - 1, listener); - return model; - } - public LabyMap generate2(int width, int height, int maxdepth) - { - LabyModel model = new LabyModel(width, height, maxdepth, new java.util.Random(1024L)); - model.generateWithEntry(0, 0); + final int width = params.getWidth(); + final int height = params.getHeight(); LinkedList exits = new LinkedList(); model.addEntryOrExit(-1, 0); model.addEntryOrExit(width, height - 1); @@ -51,7 +39,7 @@ public class Main { Main m = new Main(); MazeParamEditor editor = m.editor(); - LabyMap map = m.generate2(editor.width, editor.height, editor.maxdepth); + LabyMap map = m.generate2(editor); System.out.println(map.toShortString()); System.out.println(map.toString()); System.out.println(Brick.getDirLine()); diff --git a/java/org/artisanlogiciel/games/MazeParamsFixed.java b/java/org/artisanlogiciel/games/MazeParamsFixed.java new file mode 100644 index 0000000..0e40257 --- /dev/null +++ b/java/org/artisanlogiciel/games/MazeParamsFixed.java @@ -0,0 +1,69 @@ +package org.artisanlogiciel.games; + +import java.io.File; + +public class MazeParamsFixed implements MazeParams +{ + int width; + int height; + int maxdepth; + File labdir; + String name; + + public MazeParamsFixed() + { + } + + public MazeParamsFixed(MazeParams params) + { + setParams(params.getSaveDir(),params.getWidth(),params.getHeight(),params.getMaxDepth()); + } + + public void setParams(File saveDir, int W, int H, int MD) + { + labdir = saveDir; + width=W; + height=H; + maxdepth=MD; + } + + public MazeParamsFixed(File saveDir, int W, int H, int MD) + { + name = null; + setParams(saveDir,W,H,MD); + } + + public int getWidth() + { + return width; + } + + public int getHeight() + { + return height; + } + + public int getMaxDepth() + { + return maxdepth; + } + + public void setName(String n) + { + name = n; + } + + public String getName() + { + if (name == null) + { + name = "lab" + width + "x" + height; + } + return name; + } + + public File getSaveDir() + { + return labdir; + } +}