jackson databind import
- and some reowrk to use Position more than (x,y) parameters
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -33,12 +33,11 @@ public class MazeComponent
|
||||
LinkedList<DirectionPosition> solvedPath = null;
|
||||
LinkedList<DirectionPosition> 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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user