jackson databind import

- and some reowrk to use Position more than (x,y) parameters
This commit is contained in:
philippe lhardy
2021-11-03 08:30:30 +01:00
parent 02fda1fc2e
commit 619e9b9f19
9 changed files with 126 additions and 75 deletions

View File

@@ -1,5 +1,19 @@
#!/bin/bash #!/bin/bash
fetch_jackson_databind()
{
libversion_jackson_databind=2.12.0
lib_jackson_databind=jackson-databind-${libversion_jackson_databind}.jar
if [[ ! -e libs/$lib_jackson_databind ]]
then
jacksonmaven=https://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-databind/${libversion_jackson_databind}/$lib_jackson_databind
pushd libs
wget $jacksonmaven
popd
fi
}
if [[ ! -d ../sharedrawweb ]] if [[ ! -d ../sharedrawweb ]]
then then
echo "[ERROR] expected a sharedrawweb project parent ( for exports )" >&2 echo "[ERROR] expected a sharedrawweb project parent ( for exports )" >&2
@@ -10,7 +24,14 @@ then
mkdir libs mkdir libs
fi fi
if [[ ! -e libs/artgaphics-0.1.0.jar ]] libversion_artgaphic=0.2.0
lib_artgaphic=artgaphics-${libversion_artgaphic}.jar
if [[ ! -e libs/$lib_artgaphic ]]
then then
ln -s ../../sharedrawweb/dist/lib/artgaphics-0.1.0.jar libs/ ln -s ../../sharedrawweb/dist/lib/$lib_artgaphic libs/
fi fi
if [[ -n $use_jackson ]]
then
fetch_jackson_databind
fi

View File

@@ -19,6 +19,34 @@ bcd is within 'H' or ' '
public class Brick public class Brick
{ {
// order matters index is same as bits order
public static final Position[] AllMoves = {
new Position(0,0).doReverseTranslate(Position.stepX),
new Position(Position.stepY),
new Position(Position.stepX),
new Position(0,0).doReverseTranslate(Position.stepY),
// last is no move
new Position(0,0)
};
// flag is in Brick settings
static int getDirectionIndex(short flag)
{
// short a = (short) (flag >> FLAGLENGTH);
for (int index = 0; index < AllMoves.length - 1; index ++) {
if (isFlagSet((short) (1 << index), flag)) {
return index;
}
}
// return last index which is no move.
return AllMoves.length - 1;
}
public static Position getDirectionMove(short direction) {
return AllMoves[getDirectionIndex(direction)];
}
public final static short LEFT = 1; public final static short LEFT = 1;
public final static short DOWN = 2; public final static short DOWN = 2;
public final static short RIGHT = 4; public final static short RIGHT = 4;

View File

@@ -59,6 +59,7 @@ lues, they are used as it is for
// orders matters see getReverseDirection // orders matters see getReverseDirection
public static final short[] AllDirections = {LEFT, DOWN, RIGHT, UP}; public static final short[] AllDirections = {LEFT, DOWN, RIGHT, UP};
private int width; private int width;
private int height; private int height;
// wall flags Brick.(LEFT,DOWN,RIGHT,UP,ENTRY,GOAL) + status flags for each position (x,y) // wall flags Brick.(LEFT,DOWN,RIGHT,UP,ENTRY,GOAL) + status flags for each position (x,y)
@@ -136,9 +137,9 @@ lues, they are used as it is for
/* set direction existing onee are lost */ /* set direction existing onee are lost */
// FIXME FULLY BREAK RESOLVING... // FIXME FULLY BREAK RESOLVING...
public void setDirection(int x, int y, short path) { public void setDirection(Position cell, short path) {
if ((x >= 0) && (x < width) && (y >= 0) && (y < height)) { if ( cell.within(0,0,width,height)) {
t[x][y] = (short) (path | OPEN); t[cell.getX()][cell.getY()] = (short) (path | OPEN);
} }
} }
@@ -146,9 +147,9 @@ lues, they are used as it is for
* add a new direction, exiting ones are kept * add a new direction, exiting ones are kept
*/ */
// FIXME FULLY BREAK RESOLVING... // FIXME FULLY BREAK RESOLVING...
public void addDirection(int x, int y, short path) { public void addDirection(Position cell, short path) {
if ((x >= 0) && (x < width) && (y >= 0) && (y < height)) { if ( cell.within(0,0,width,height)) {
t[x][y] |= (short) (path | OPEN); t[cell.getX()][cell.getY()] |= (short) (path | OPEN);
} }
} }
@@ -789,9 +790,19 @@ lues, they are used as it is for
/** /**
* is there no wall in that direction ? * is there no wall in that direction ?
**/ **/
public boolean canMoveInDirection(int x, int y, short direction) { public boolean canMoveInDirection(Position where, short direction) {
// one way wall will create walll if it not possible to step back to current position from next position // one way wall will create walll if it not possible to step back to current position from next position
return onewaywall ? ( ! hasWallInDirection(x,y, (short) (direction << FLAGLENGTH)) ) : ((getWalls(x, y) & direction ) == 0); return onewaywall ? ( ! hasWallInDirection(where.getX(),where.getY(), (short) (direction << FLAGLENGTH)) ) : ((getWalls(where) & direction ) == 0);
}
public boolean moveInDirection(Position position, short direction) {
boolean canMove = canMoveInDirection(position, direction);
if ( canMove )
{
position.doTranslate(Brick.getDirectionMove(direction));
}
return canMove;
} }
public boolean reverse(boolean grow) public boolean reverse(boolean grow)

View File

@@ -3,6 +3,9 @@ package org.artisanlogiciel.games.maze;
/** position of a cell with maze */ /** position of a cell with maze */
public class Position public class Position
{ {
public final static Position stepX = new Position(1,0);
public final static Position stepY = new Position( 0,1);
private int x, y; private int x, y;
public Position(int x, int y) public Position(int x, int y)
@@ -38,6 +41,12 @@ public class Position
return this; return this;
} }
public Position doReverseTranslate(Position other) {
x -= other.x;
y -= other.y;
return this;
}
public void limitToMax(int mx, int my) public void limitToMax(int mx, int my)
{ {
@@ -82,4 +91,8 @@ public class Position
return false; return false;
} }
} }
public boolean within(int a, int b, int width, int height) {
return ((x > a) && (x < width) && (y > b) && (y < height));
}
} }

View File

@@ -2,8 +2,6 @@ package org.artisanlogiciel.games.maze;
public class XYGridIterator { public class XYGridIterator {
final static Position stepX = new Position(1,0);
final static Position stepY = new Position( 0,1);
Position min; Position min;
Position max; Position max;
@@ -31,10 +29,10 @@ public class XYGridIterator {
if ( next != null) if ( next != null)
{ {
Position current = new Position(next); Position current = new Position(next);
next.doTranslate(stepX); next.doTranslate(Position.stepX);
if ( next.getX() >= max.getX()) { if ( next.getX() >= max.getX()) {
if (next.getY() < max.getY() - 1) { if (next.getY() < max.getY() - 1) {
next.setX(min.getX()).doTranslate(stepY); next.setX(min.getX()).doTranslate(Position.stepY);
} else { } else {
next = null; next = null;
} }

View File

@@ -1,9 +1,6 @@
package org.artisanlogiciel.games.maze.gui; package org.artisanlogiciel.games.maze.gui;
import org.artisanlogiciel.games.maze.LabyModel; import org.artisanlogiciel.games.maze.*;
import org.artisanlogiciel.games.maze.Maze;
import org.artisanlogiciel.games.maze.MazeParams;
import org.artisanlogiciel.games.maze.MazeParamsFixed;
import org.artisanlogiciel.games.maze.model.WidthHeightProvider; import org.artisanlogiciel.games.maze.model.WidthHeightProvider;
import org.artisanlogiciel.games.maze.persist.MazePersistRaw; import org.artisanlogiciel.games.maze.persist.MazePersistRaw;
import org.artisanlogiciel.games.maze.solve.SolvingModel; import org.artisanlogiciel.games.maze.solve.SolvingModel;
@@ -195,21 +192,20 @@ implements StatusListener
} }
void goNorth() { void goNorth() {
maze.goNorth(); maze.goTo(Brick.UP);
} }
void goSouth() { void goSouth() {
maze.goSouth(); maze.goTo(Brick.DOWN);
} }
void goEast() { void goEast() {
maze.goEast(); maze.goTo(Brick.RIGHT);
} }
void goWest() { void goWest() {
maze.goWest(); maze.goTo(Brick.LEFT);
} }
void setWallSize(int size) { void setWallSize(int size) {
maze.setWallSize(size); maze.setWallSize(size);
} }

View File

@@ -33,12 +33,11 @@ public class MazeComponent
LinkedList<DirectionPosition> solvedPath = null; LinkedList<DirectionPosition> solvedPath = null;
LinkedList<DirectionPosition> drawingPath = null; LinkedList<DirectionPosition> drawingPath = null;
final Object lockChange = new Object(); final Object lockChange = new Object();
// current postion of human resolving // current position of human resolving
int sX = 0; Position human = new Position(0,0);
int sY = 0;
// goal exit // goal exit
int gX = -1; Position goal = new Position(-1,-1);
int gY = -1;
// use a background // use a background
Xpm xpm = null; Xpm xpm = null;
@@ -125,9 +124,9 @@ public class MazeComponent
path = LabyModel.getDirection(last.getPosition(), newPosition); path = LabyModel.getDirection(last.getPosition(), newPosition);
last.setDirection(path); last.setDirection(path);
if (add) { if (add) {
map.addDirection(last.getPosition().getX(), last.getPosition().getY(), path); map.addDirection(last.getPosition(), path);
} else { } else {
map.setDirection(last.getPosition().getX(), last.getPosition().getY(), path); map.setDirection(last.getPosition(), path);
} }
last = last.moveToAdjacentDirection(); last = last.moveToAdjacentDirection();
drawingPath.addLast(last); drawingPath.addLast(last);
@@ -151,7 +150,7 @@ public class MazeComponent
} }
void checkExit() { void checkExit() {
if ((sX == gX) && (sY == gY)) { if (human.equals(goal)) {
statusListener.addStatus("Exit found by human !"); statusListener.addStatus("Exit found by human !");
} }
} }
@@ -163,34 +162,10 @@ public class MazeComponent
checkExit(); checkExit();
} }
void goNorth() { void goTo(short direction)
{
resetResolution(); resetResolution();
if (map.canMoveInDirection(sX, sY, Brick.UP)) { if (map.moveInDirection(human, direction)) {
sY = sY - 1;
proceed();
}
}
void goSouth() {
resetResolution();
if (map.canMoveInDirection(sX, sY, Brick.DOWN)) {
sY = sY + 1;
proceed();
}
}
void goEast() {
resetResolution();
if (map.canMoveInDirection(sX, sY, Brick.RIGHT)) {
sX = sX + 1;
proceed();
}
}
void goWest() {
resetResolution();
if (map.canMoveInDirection(sX, sY, Brick.LEFT)) {
sX = sX - 1;
proceed(); proceed();
} }
} }
@@ -203,8 +178,7 @@ public class MazeComponent
public MazeComponent(LabyModel map, WidthHeightProvider frame, StatusListener statusListener) { public MazeComponent(LabyModel map, WidthHeightProvider frame, StatusListener statusListener) {
super(); super();
this.map = map; this.map = map;
gX = map.getWidth() - 1; goal = new Position(map.getWidth() - 1,map.getHeight() - 1);
gY = map.getHeight() - 1;
resetCellRenderer(false,frame); resetCellRenderer(false,frame);
this.statusListener = statusListener; this.statusListener = statusListener;
addMouseMotionListener(this); addMouseMotionListener(this);
@@ -222,8 +196,7 @@ public class MazeComponent
solvedPath = null; solvedPath = null;
// could be kept // could be kept
drawingPath = null; drawingPath = null;
sX = 0; human = new Position(0,0);
sY = 0;
cp.resetMazeWidthHeight(map); cp.resetMazeWidthHeight(map);
setPreferredSize(cp.getDimension()); setPreferredSize(cp.getDimension());
} }
@@ -274,8 +247,7 @@ public class MazeComponent
@Override @Override
protected void paintComponent(Graphics g) { protected void paintComponent(Graphics g) {
super.paintComponent(g); super.paintComponent(g);
int x = 0;
int y = 0;
short walls = 0; short walls = 0;
short path = 0; short path = 0;
@@ -328,14 +300,14 @@ public class MazeComponent
cp.drawBackground(g, cell, walls); cp.drawBackground(g, cell, walls);
} }
if ((sX == gX) && (sY == gY)) { if (human.equals(goal)) {
g.setColor(colorMap.goal); g.setColor(colorMap.goal);
} else { } else {
g.setColor(colorMap.resolved_path); g.setColor(colorMap.resolved_path);
cp.drawDot(g, new Position(gX, gY), min, max); cp.drawDot(g, goal, min, max);
g.setColor(colorMap.path); g.setColor(colorMap.path);
} }
cp.drawDot(g, new Position(sX, sY), min, max); cp.drawDot(g, human, min, max);
// draw all walls within clip bounds horiz first then lines // draw all walls within clip bounds horiz first then lines
g.setColor(colorMap.wall); g.setColor(colorMap.wall);

View File

@@ -1,6 +1,7 @@
package org.artisanlogiciel.games.maze.model; package org.artisanlogiciel.games.maze.model;
import org.artisanlogiciel.games.maze.MovesProvider; import org.artisanlogiciel.games.maze.MovesProvider;
import org.artisanlogiciel.games.maze.Position;
import org.artisanlogiciel.games.maze.WallsProvider; import org.artisanlogiciel.games.maze.WallsProvider;
public interface LabyModelProvider public interface LabyModelProvider
@@ -10,15 +11,26 @@ extends WallsProvider,
/** /**
* add a new direction(s) exiting ones are kept * add a new direction(s) exiting ones are kept
*/ */
void addDirection(int x, int y, short path); void addDirection(Position cell, short path);
/* set direction(s) existing ones are lost */ /* set direction(s) existing ones are lost */
void setDirection(int x, int y, short path); void setDirection(Position cell, short path);
/** /**
* is there no wall in that direction ? * is there no wall in that direction ?
* WARNING direction is in Brick ( ex Brick.UP )
**/ **/
boolean canMoveInDirection(int x, int y, short direction); boolean canMoveInDirection(Position where, short direction);
/**
* return if position position was change
* WARNING direction is in Brick ( ex Brick.UP )
*
* @param inoutposition
* @param direction
* @return
*/
boolean moveInDirection(Position inoutposition, short direction);
/** like getMoves but include resolved information */ /** like getMoves but include resolved information */
short getPath(int x, int y); short getPath(int x, int y);

View File

@@ -42,16 +42,16 @@ public class DirectionPosition {
short pointingdirection = 0; short pointingdirection = 0;
Position p = null; Position p = null;
if (LabyModel.isFlagSet(direction, LabyModel.RIGHT)) { if (LabyModel.isFlagSet(direction, LabyModel.RIGHT)) {
p = new Position(position.getX() + 1, position.getY()); p = new Position(position).doTranslate(Position.stepX);
pointingdirection |= LabyModel.LEFT; pointingdirection |= LabyModel.LEFT;
} else if (LabyModel.isFlagSet(direction, LabyModel.LEFT)) { } else if (LabyModel.isFlagSet(direction, LabyModel.LEFT)) {
p = new Position(position.getX() - 1, position.getY()); p = new Position(position).doReverseTranslate(Position.stepX);
pointingdirection |= LabyModel.RIGHT; pointingdirection |= LabyModel.RIGHT;
} else if (LabyModel.isFlagSet(direction, LabyModel.UP)) { } else if (LabyModel.isFlagSet(direction, LabyModel.UP)) {
p = new Position(position.getX(), position.getY() - 1); p = new Position(position).doReverseTranslate(Position.stepY);
pointingdirection |= LabyModel.DOWN; pointingdirection |= LabyModel.DOWN;
} else if (LabyModel.isFlagSet(direction, LabyModel.DOWN)) { } else if (LabyModel.isFlagSet(direction, LabyModel.DOWN)) {
p = new Position(position.getX(), position.getY() + 1); p = new Position(position).doTranslate(Position.stepY);
pointingdirection |= LabyModel.UP; pointingdirection |= LabyModel.UP;
} else { } else {
p = position; p = position;