LabyModel limit public constructors to

LabyModel(MazeParams params, Random random)
and
 LabyModel(String pFormat, InputStream pIn)

fix unchecked and (java9) deprecated Short(short)
This commit is contained in:
philippe lhardy
2018-06-14 22:49:39 +02:00
parent 2d3f7b01a0
commit fbe4a3e133
5 changed files with 128 additions and 127 deletions

View File

@@ -22,6 +22,7 @@
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}" includeantruntime="false" classpath="libs/${artgraphicslib}.jar">
<exclude name="org/artisanlogiciel/games/javafx/*"/>
<compilerarg value="-Xlint:deprecation,unchecked" />
</javac>
</target>

View File

@@ -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

View File

@@ -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<Position> openList = new LinkedList<Position>();
// list of entries and exits.
private final LinkedList<Position> entryExits = new LinkedList<Position>();
@@ -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<Position>) entryExits.clone());
return new LabyMap(brickMap, new LinkedList<Position>(entryExits));
}
}
@@ -445,7 +446,6 @@ public class LabyModel implements WallsProvider
{
// resetCell(x,y);
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<DirectionPosition> cp = (LinkedList<DirectionPosition>) backpath.clone();
LinkedList<DirectionPosition> cp = new LinkedList<DirectionPosition>(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));
}
}
}
@@ -1092,14 +1093,16 @@ public class LabyModel implements WallsProvider
{
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();
if ( (rwidth > 0) && (rheight > 0 ) )
{
width = rwidth;
height = rheight;
maxdepth = maxdepth;
random = null;
// SHOULD CHECK max width and max height...
// CLEAR == 0 and array is initialized with 0s
@@ -1111,6 +1114,11 @@ public class LabyModel implements WallsProvider
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 ).
}

View File

@@ -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<Position> exits = new LinkedList<Position>();
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<Position> exits = new LinkedList<Position>();
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());

View File

@@ -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;
}
}