From 619e9b9f19c1e0507cf25e3842d9122731eba91a Mon Sep 17 00:00:00 2001 From: philippe lhardy Date: Wed, 3 Nov 2021 08:30:30 +0100 Subject: [PATCH] jackson databind import - and some reowrk to use Position more than (x,y) parameters --- fetch_dependencies.sh | 27 ++++++++- .../org/artisanlogiciel/games/maze/Brick.java | 28 +++++++++ .../artisanlogiciel/games/maze/LabyModel.java | 27 ++++++--- .../artisanlogiciel/games/maze/Position.java | 13 ++++ .../games/maze/XYGridIterator.java | 6 +- .../games/maze/gui/Display.java | 14 ++--- .../games/maze/gui/MazeComponent.java | 60 +++++-------------- .../games/maze/model/LabyModelProvider.java | 18 +++++- .../games/maze/solve/DirectionPosition.java | 8 +-- 9 files changed, 126 insertions(+), 75 deletions(-) diff --git a/fetch_dependencies.sh b/fetch_dependencies.sh index 0eeff37..c72584c 100755 --- a/fetch_dependencies.sh +++ b/fetch_dependencies.sh @@ -1,5 +1,19 @@ #!/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 ]] then echo "[ERROR] expected a sharedrawweb project parent ( for exports )" >&2 @@ -10,7 +24,14 @@ then mkdir libs 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 - ln -s ../../sharedrawweb/dist/lib/artgaphics-0.1.0.jar libs/ -fi + ln -s ../../sharedrawweb/dist/lib/$lib_artgaphic libs/ +fi + +if [[ -n $use_jackson ]] +then + fetch_jackson_databind +fi diff --git a/java/org/artisanlogiciel/games/maze/Brick.java b/java/org/artisanlogiciel/games/maze/Brick.java index abdfbea..a6833ed 100644 --- a/java/org/artisanlogiciel/games/maze/Brick.java +++ b/java/org/artisanlogiciel/games/maze/Brick.java @@ -19,6 +19,34 @@ bcd is within 'H' or ' ' 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 DOWN = 2; public final static short RIGHT = 4; diff --git a/java/org/artisanlogiciel/games/maze/LabyModel.java b/java/org/artisanlogiciel/games/maze/LabyModel.java index a926c48..5ebcf13 100644 --- a/java/org/artisanlogiciel/games/maze/LabyModel.java +++ b/java/org/artisanlogiciel/games/maze/LabyModel.java @@ -59,6 +59,7 @@ lues, they are used as it is for // orders matters see getReverseDirection public static final short[] AllDirections = {LEFT, DOWN, RIGHT, UP}; + private int width; private int height; // 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 */ // FIXME FULLY BREAK RESOLVING... - public void setDirection(int x, int y, short path) { - if ((x >= 0) && (x < width) && (y >= 0) && (y < height)) { - t[x][y] = (short) (path | OPEN); + public void setDirection(Position cell, short path) { + if ( cell.within(0,0,width,height)) { + 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 */ // FIXME FULLY BREAK RESOLVING... - public void addDirection(int x, int y, short path) { - if ((x >= 0) && (x < width) && (y >= 0) && (y < height)) { - t[x][y] |= (short) (path | OPEN); + public void addDirection(Position cell, short path) { + if ( cell.within(0,0,width,height)) { + 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 ? **/ - 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 - 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) diff --git a/java/org/artisanlogiciel/games/maze/Position.java b/java/org/artisanlogiciel/games/maze/Position.java index 250ddeb..3c04730 100644 --- a/java/org/artisanlogiciel/games/maze/Position.java +++ b/java/org/artisanlogiciel/games/maze/Position.java @@ -3,6 +3,9 @@ package org.artisanlogiciel.games.maze; /** position of a cell with maze */ 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; public Position(int x, int y) @@ -38,6 +41,12 @@ public class Position return this; } + public Position doReverseTranslate(Position other) { + x -= other.x; + y -= other.y; + return this; + } + public void limitToMax(int mx, int my) { @@ -82,4 +91,8 @@ public class Position return false; } } + + public boolean within(int a, int b, int width, int height) { + return ((x > a) && (x < width) && (y > b) && (y < height)); + } } diff --git a/java/org/artisanlogiciel/games/maze/XYGridIterator.java b/java/org/artisanlogiciel/games/maze/XYGridIterator.java index 9115e1f..828444d 100644 --- a/java/org/artisanlogiciel/games/maze/XYGridIterator.java +++ b/java/org/artisanlogiciel/games/maze/XYGridIterator.java @@ -2,8 +2,6 @@ package org.artisanlogiciel.games.maze; public class XYGridIterator { - final static Position stepX = new Position(1,0); - final static Position stepY = new Position( 0,1); Position min; Position max; @@ -31,10 +29,10 @@ public class XYGridIterator { if ( next != null) { Position current = new Position(next); - next.doTranslate(stepX); + next.doTranslate(Position.stepX); if ( next.getX() >= max.getX()) { if (next.getY() < max.getY() - 1) { - next.setX(min.getX()).doTranslate(stepY); + next.setX(min.getX()).doTranslate(Position.stepY); } else { next = null; } diff --git a/java/org/artisanlogiciel/games/maze/gui/Display.java b/java/org/artisanlogiciel/games/maze/gui/Display.java index adfd0f0..80d8cc0 100644 --- a/java/org/artisanlogiciel/games/maze/gui/Display.java +++ b/java/org/artisanlogiciel/games/maze/gui/Display.java @@ -1,9 +1,6 @@ package org.artisanlogiciel.games.maze.gui; -import org.artisanlogiciel.games.maze.LabyModel; -import org.artisanlogiciel.games.maze.Maze; -import org.artisanlogiciel.games.maze.MazeParams; -import org.artisanlogiciel.games.maze.MazeParamsFixed; +import org.artisanlogiciel.games.maze.*; import org.artisanlogiciel.games.maze.model.WidthHeightProvider; import org.artisanlogiciel.games.maze.persist.MazePersistRaw; import org.artisanlogiciel.games.maze.solve.SolvingModel; @@ -195,21 +192,20 @@ implements StatusListener } void goNorth() { - maze.goNorth(); + maze.goTo(Brick.UP); } void goSouth() { - maze.goSouth(); + maze.goTo(Brick.DOWN); } void goEast() { - maze.goEast(); + maze.goTo(Brick.RIGHT); } void goWest() { - maze.goWest(); + maze.goTo(Brick.LEFT); } - void setWallSize(int size) { maze.setWallSize(size); } diff --git a/java/org/artisanlogiciel/games/maze/gui/MazeComponent.java b/java/org/artisanlogiciel/games/maze/gui/MazeComponent.java index 5eb63f4..c5eb172 100644 --- a/java/org/artisanlogiciel/games/maze/gui/MazeComponent.java +++ b/java/org/artisanlogiciel/games/maze/gui/MazeComponent.java @@ -33,12 +33,11 @@ public class MazeComponent LinkedList solvedPath = null; LinkedList drawingPath = null; final Object lockChange = new Object(); - // current postion of human resolving - int sX = 0; - int sY = 0; + // current position of human resolving + Position human = new Position(0,0); + // goal exit - int gX = -1; - int gY = -1; + Position goal = new Position(-1,-1); // use a background Xpm xpm = null; @@ -125,9 +124,9 @@ public class MazeComponent path = LabyModel.getDirection(last.getPosition(), newPosition); last.setDirection(path); if (add) { - map.addDirection(last.getPosition().getX(), last.getPosition().getY(), path); + map.addDirection(last.getPosition(), path); } else { - map.setDirection(last.getPosition().getX(), last.getPosition().getY(), path); + map.setDirection(last.getPosition(), path); } last = last.moveToAdjacentDirection(); drawingPath.addLast(last); @@ -151,7 +150,7 @@ public class MazeComponent } void checkExit() { - if ((sX == gX) && (sY == gY)) { + if (human.equals(goal)) { statusListener.addStatus("Exit found by human !"); } } @@ -163,34 +162,10 @@ public class MazeComponent checkExit(); } - void goNorth() { + void goTo(short direction) + { resetResolution(); - if (map.canMoveInDirection(sX, sY, Brick.UP)) { - 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; + if (map.moveInDirection(human, direction)) { proceed(); } } @@ -203,8 +178,7 @@ public class MazeComponent public MazeComponent(LabyModel map, WidthHeightProvider frame, StatusListener statusListener) { super(); this.map = map; - gX = map.getWidth() - 1; - gY = map.getHeight() - 1; + goal = new Position(map.getWidth() - 1,map.getHeight() - 1); resetCellRenderer(false,frame); this.statusListener = statusListener; addMouseMotionListener(this); @@ -222,8 +196,7 @@ public class MazeComponent solvedPath = null; // could be kept drawingPath = null; - sX = 0; - sY = 0; + human = new Position(0,0); cp.resetMazeWidthHeight(map); setPreferredSize(cp.getDimension()); } @@ -274,8 +247,7 @@ public class MazeComponent @Override protected void paintComponent(Graphics g) { super.paintComponent(g); - int x = 0; - int y = 0; + short walls = 0; short path = 0; @@ -328,14 +300,14 @@ public class MazeComponent cp.drawBackground(g, cell, walls); } - if ((sX == gX) && (sY == gY)) { + if (human.equals(goal)) { g.setColor(colorMap.goal); } else { g.setColor(colorMap.resolved_path); - cp.drawDot(g, new Position(gX, gY), min, max); + cp.drawDot(g, goal, min, max); 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 g.setColor(colorMap.wall); diff --git a/java/org/artisanlogiciel/games/maze/model/LabyModelProvider.java b/java/org/artisanlogiciel/games/maze/model/LabyModelProvider.java index 8b019ef..df94c25 100644 --- a/java/org/artisanlogiciel/games/maze/model/LabyModelProvider.java +++ b/java/org/artisanlogiciel/games/maze/model/LabyModelProvider.java @@ -1,6 +1,7 @@ package org.artisanlogiciel.games.maze.model; import org.artisanlogiciel.games.maze.MovesProvider; +import org.artisanlogiciel.games.maze.Position; import org.artisanlogiciel.games.maze.WallsProvider; public interface LabyModelProvider @@ -10,15 +11,26 @@ extends WallsProvider, /** * 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 */ - void setDirection(int x, int y, short path); + void setDirection(Position cell, short path); /** * 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 */ short getPath(int x, int y); diff --git a/java/org/artisanlogiciel/games/maze/solve/DirectionPosition.java b/java/org/artisanlogiciel/games/maze/solve/DirectionPosition.java index 6be3447..707d5a0 100644 --- a/java/org/artisanlogiciel/games/maze/solve/DirectionPosition.java +++ b/java/org/artisanlogiciel/games/maze/solve/DirectionPosition.java @@ -42,16 +42,16 @@ public class DirectionPosition { short pointingdirection = 0; Position p = null; if (LabyModel.isFlagSet(direction, LabyModel.RIGHT)) { - p = new Position(position.getX() + 1, position.getY()); + p = new Position(position).doTranslate(Position.stepX); pointingdirection |= 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; } 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; } 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; } else { p = position;