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} --> <!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}" includeantruntime="false" classpath="libs/${artgraphicslib}.jar"> <javac srcdir="${src}" destdir="${build}" includeantruntime="false" classpath="libs/${artgraphicslib}.jar">
<exclude name="org/artisanlogiciel/games/javafx/*"/> <exclude name="org/artisanlogiciel/games/javafx/*"/>
<compilerarg value="-Xlint:deprecation,unchecked" />
</javac> </javac>
</target> </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 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 public final static short DIRECTION = 4; // could we get rid of that to
// free one bit for other purpose // 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 POSITIVE = 8;
public final static short NEGATIVE = 16; 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 private final static short OPEN = 32; // can be reused once generation is
// completed // completed
private final static short CLOSED = 64; // can be reused once generation is 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 DOWN = Brick.DOWN << FLAGLENGTH | DIRECTION | VERTICAL | POSITIVE;
public final static short RIGHT = Brick.RIGHT << FLAGLENGTH | DIRECTION | HORIZONTAL | 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 UP = Brick.UP << FLAGLENGTH | DIRECTION | VERTICAL | NEGATIVE;
public final static short ENTRY = Brick.ENTRY << FLAGLENGTH; // flag when a
// wall should // flag when a wall should be open to access this for entry
// be open to public final static short ENTRY = Brick.ENTRY << FLAGLENGTH;
// access // flag when a wall should be open to access this for exit
// this. private final static short GOAL = Brick.GOAL << FLAGLENGTH;
private final static short GOAL = Brick.GOAL << FLAGLENGTH; // flag when a // flag when solution is on this path.
// wall should public final static short SOLVED = 64 << FLAGLENGTH;
// be open to // free flag
// access this. private final static short FREE = 128 << FLAGLENGTH;
public final static short SOLVED = 64 << FLAGLENGTH; // flag when solution
// is on this path.
private final static short FREE = 128 << FLAGLENGTH; // free flag
// remains 2 free bits ( keep one out for sign ) // remains 2 free bits ( keep one out for sign )
// orders matters see getReverseDirection
private static final short[] AllDirections = { LEFT, DOWN, RIGHT, UP }; private static final short[] AllDirections = { LEFT, DOWN, RIGHT, UP };
private int width; private int width;
private int height; 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 short[][] t;
private int depth = 0; private int depth = 0;
/** /**
@@ -71,11 +71,15 @@ public class LabyModel implements WallsProvider
* that to shorter maxdepth is the harder the labyrinth is ... * that to shorter maxdepth is the harder the labyrinth is ...
**/ **/
private int maxdepth = 0; 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 int linearwork = 0;
private Position deepestEnd = null; private Position deepestEnd = null;
boolean maxreached = false; boolean maxreached = false;
java.util.Random random; java.util.Random random;
// list of positions not fully walked
private final LinkedList<Position> openList = new LinkedList<Position>(); private final LinkedList<Position> openList = new LinkedList<Position>();
// list of entries and exits. // list of entries and exits.
private final LinkedList<Position> entryExits = new LinkedList<Position>(); private final LinkedList<Position> entryExits = new LinkedList<Position>();
@@ -87,7 +91,7 @@ public class LabyModel implements WallsProvider
MazeCreationListener listener = null; 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.width = width;
this.height = height; this.height = height;
@@ -99,14 +103,12 @@ public class LabyModel implements WallsProvider
public LabyModel(MazeParams params, Random random) public LabyModel(MazeParams params, Random random)
{ {
this.width = params.getWidth(); this(params.getWidth(),params.getHeight(),params.getMaxDepth(),random);
this.height = params.getHeight();
this.maxdepth = params.getMaxDepth();
this.random = random;
// CLEAR == 0 and array is initialized with 0s
t = new short[width][height];
} }
/**
construct LabyModel from an InputStream, yet only "raw" is supported
**/
public LabyModel(String pFormat, InputStream pIn) throws IOException public LabyModel(String pFormat, InputStream pIn) throws IOException
{ {
parseInputStream(pFormat, pIn); parseInputStream(pFormat, pIn);
@@ -123,7 +125,6 @@ public class LabyModel implements WallsProvider
if ((x >= 0) && (x < width) && (y >= 0) && (y < height)) 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 | LEFT | RIGHT | UP | DOWN;
t[x][y] |= DIRECTION | VERTICAL | POSITIVE | HORIZONTAL | NEGATIVE;
t[x][y] |= LEFT | RIGHT | UP | DOWN; 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)); 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); // resetCell(x,y);
t[x][y] &= ~( OPEN | SOLVED); t[x][y] &= ~( OPEN | SOLVED);
} }
} }
@@ -485,6 +485,7 @@ public class LabyModel implements WallsProvider
return check; return check;
} }
// coherency : if CLOSED then not OPEN
public void computeOpenList() public void computeOpenList()
{ {
openList.clear(); 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) public void generateWithEntry(int x, int y)
{ {
@@ -696,7 +697,7 @@ public class LabyModel implements WallsProvider
// internal GUARD. // internal GUARD.
if (! isFlagSet(reversedirection,pointingdirection)) 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); + (reversedirection & pointingdirection) + " " + pointingdirection);
return backpath; return backpath;
} }
@@ -718,7 +719,7 @@ public class LabyModel implements WallsProvider
{ {
if (! isFlagSet(getCell(p), SOLVED )) 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); DirectionPosition altfound = new DirectionPosition(reversedirection,p);
cp.addFirst(altfound); cp.addFirst(altfound);
altpath.addLast(cp); altpath.addLast(cp);
@@ -884,7 +885,7 @@ public class LabyModel implements WallsProvider
{ {
if ((t[newx][newy]) == CLEAR) 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"))) if ((pFormat == null) || (pFormat.equals("raw")))
{ {
// maxdepth is unset then unmodified
byte[] header = new byte[4]; byte[] header = new byte[4];
DataInputStream in = new DataInputStream(pIn); DataInputStream in = new DataInputStream(pIn);
in.read(header); in.read(header);
int rwidth = in.readInt(); int rwidth = in.readInt();
int rheight = in.readInt(); int rheight = in.readInt();
if ( (rwidth > 0) && (rheight > 0 ) )
{
width = rwidth; width = rwidth;
height = rheight; height = rheight;
maxdepth = maxdepth;
random = null; random = null;
// SHOULD CHECK max width and max height... // SHOULD CHECK max width and max height...
// CLEAR == 0 and array is initialized with 0s // CLEAR == 0 and array is initialized with 0s
@@ -1111,6 +1114,11 @@ public class LabyModel implements WallsProvider
t[x][y] = in.readShort(); 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 // should be at end of stream ? Not necessary can stream multiple
// labs ( or tiling ). // labs ( or tiling ).
} }

View File

@@ -14,25 +14,13 @@ public class Main
return editor; 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); 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) final int width = params.getWidth();
{ final int height = params.getHeight();
LabyModel model = new LabyModel(width, height, maxdepth, new java.util.Random(1024L));
model.generateWithEntry(0, 0);
LinkedList<Position> exits = new LinkedList<Position>(); LinkedList<Position> exits = new LinkedList<Position>();
model.addEntryOrExit(-1, 0); model.addEntryOrExit(-1, 0);
model.addEntryOrExit(width, height - 1); model.addEntryOrExit(width, height - 1);
@@ -51,7 +39,7 @@ public class Main
{ {
Main m = new Main(); Main m = new Main();
MazeParamEditor editor = m.editor(); 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.toShortString());
System.out.println(map.toString()); System.out.println(map.toString());
System.out.println(Brick.getDirLine()); 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;
}
}