refactoring, deploy Position and XYGridIterator

- try to use more Position instead of (x,y)
  - create PositionWithDpeth for its specific usage in path finding
- create a XYGridITerator that walk cells from grid X then Y.
This commit is contained in:
philippe lhardy
2020-12-28 19:12:35 +01:00
parent 0c886f0cd1
commit 02fda1fc2e
15 changed files with 311 additions and 162 deletions

View File

@@ -27,6 +27,11 @@ public class LabyMap implements WallsProvider
return getAt(x, y).getWalls();
}
public short getWalls(Position cell)
{
return getAt(cell.getX(), cell.getY()).getWalls();
}
public int getWidth()
{
return tileMap[0].length;

View File

@@ -79,7 +79,7 @@ lues, they are used as it is for
boolean maxreached = false;
java.util.Random random;
// list of positions not fully walked
private final LinkedList<Position> openList = new LinkedList<Position>();
private final LinkedList<PositionWithDepth> openList = new LinkedList<>();
// list of entries and exits.
private final LinkedList<Position> entryExits = new LinkedList<Position>();
private final Object coherentLock = new Object(); // before getting the lock
@@ -390,7 +390,7 @@ lues, they are used as it is for
return this.height;
}
public LinkedList<Position> getOpenList() {
public LinkedList<PositionWithDepth> getOpenList() {
return openList;
}
@@ -464,7 +464,7 @@ lues, they are used as it is for
t[x][y] &= ~OPEN;
}
if (isFlagSet(t[x][y], OPEN) || (t[x][y] == CLEAR)) {
openList.add(new Position(x, y));
openList.add(new PositionWithDepth(x, y,0));
}
}
}
@@ -477,7 +477,7 @@ lues, they are used as it is for
**/
public void generateWithEntry(int x, int y) {
openList.add(new Position(x, y));
openList.add(new PositionWithDepth(x, y,0));
while (!openList.isEmpty()) {
// this is where magic happens
step();
@@ -493,7 +493,7 @@ lues, they are used as it is for
// should not continue in depth first...
if (!openList.isEmpty()) {
// insert head as next position to pick up.
Position current = openList.removeFirst();
PositionWithDepth current = openList.removeFirst();
openList.add(current);
}
}
@@ -526,7 +526,7 @@ lues, they are used as it is for
* @param p Position
* @return true if newly added , false if already open.
**/
private boolean open(boolean first, Position p) {
private boolean open(boolean first, PositionWithDepth p) {
int x = p.getX();
int y = p.getY();
if ((t[x][y] & OPEN) != OPEN) {
@@ -537,6 +537,7 @@ lues, they are used as it is for
return false;
}
// WARNING Brick uses argument in other order... ( bad ... )
public final static boolean isFlagSet(short check, short flag) {
return Brick.isFlagSet(flag,check);
}
@@ -588,7 +589,7 @@ lues, they are used as it is for
**/
public boolean step() {
boolean complete = false;
Position current = null;
PositionWithDepth current = null;
synchronized (coherentLock) {
@@ -668,7 +669,7 @@ lues, they are used as it is for
newx = x;
}
Position target = new Position(newx, newy, current.getDepth() + 1);
PositionWithDepth target = new PositionWithDepth(newx, newy, current.getDepth() + 1);
open(false, target);
if ((t[x][y] & DIRECTION) == DIRECTION) {
t[x][y] |= direction;
@@ -707,6 +708,10 @@ lues, they are used as it is for
return complete;
}
public short getWalls(Position cell) {
return getWalls(cell.getX(), cell.getY());
}
public short getWalls(int x, int y) {
short walls = 0;
for (short direction : AllDirections) {

View File

@@ -4,22 +4,20 @@ package org.artisanlogiciel.games.maze;
public class Position
{
private int x, y;
private int depth;
public Position(int x, int y, int depth)
{
this.x = x;
this.y = y;
this.depth = depth;
}
public Position(int x, int y)
{
this.x = x;
this.y = y;
this.depth = 0;
}
public Position(Position other)
{
this.x = other.x;
this.y = other.y;
}
public int getX()
{
return this.x;
@@ -30,29 +28,58 @@ public class Position
return this.y;
}
public int getDepth()
{
return depth;
public Position translate(Position other) {
return new Position(x + other.x, y + other.y);
}
public Position doTranslate(Position other) {
x += other.x;
y += other.y;
return this;
}
public void limitToMax(int mx, int my)
{
x = Math.min(x,mx);
y = Math.min(y,my);
}
public void limitToMin(int mx, int my)
{
x = Math.max(x,mx);
y = Math.max(y,my);
}
public Position setX(int x)
{
this.x = x;
return this;
}
public Position setY(int y)
{
this.y = y;
return this;
}
public String toString()
{
return "(" + x + "," + y + ")" + "/" + depth;
return "(" + x + "," + y + ")";
}
public boolean equals(Object other)
{
// disregards depth
if (other instanceof Position )
{
Position p = (Position) other;
return (p.getX() == x) && (p.getY() == y);
}
else
{
return false;
}
// disregards depth
if (other instanceof Position )
{
Position p = (Position) other;
return (p.getX() == x) && (p.getY() == y);
}
else
{
return false;
}
}
}

View File

@@ -0,0 +1,24 @@
package org.artisanlogiciel.games.maze;
public class PositionWithDepth
extends Position
{
private int depth;
public PositionWithDepth(int x, int y, int depth)
{
super(x,y);
this.depth = depth;
}
public int getDepth()
{
return depth;
}
public String toString()
{
return "(" + getX() + "," + getY() + ")" + "/" + depth;
}
}

View File

@@ -22,4 +22,6 @@ public interface WallsProvider
* t
**/
short getWalls(int x, int y);
short getWalls(Position cell);
}

View File

@@ -0,0 +1,46 @@
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;
Position next;
public XYGridIterator(Position min, Position max) {
this.min = min;
this.max = max;
reset();
}
public void reset()
{
this.next = new Position(min);
}
public boolean hasNext()
{
return next != null;
}
public Position next()
{
if ( next != null)
{
Position current = new Position(next);
next.doTranslate(stepX);
if ( next.getX() >= max.getX()) {
if (next.getY() < max.getY() - 1) {
next.setX(min.getX()).doTranslate(stepY);
} else {
next = null;
}
}
return current;
}
return null;
}
}

View File

@@ -0,0 +1,19 @@
package org.artisanlogiciel.games.maze.gui;
import org.artisanlogiciel.games.maze.model.WidthHeightProvider;
import javax.swing.*;
public abstract class CellGridComponent
extends JComponent {
MazeCellRenderer cp;
MazeCellRenderer createCellRenderer(boolean hexagon, WidthHeightProvider model, WidthHeightProvider frame)
{
MazeCellRenderer cellRenderer = hexagon ?
new HexagonCellRenderer(model, frame, 0, 0)
: new MazeCellRenderer(model, frame, 0, 0);
return cellRenderer;
}
}

View File

@@ -66,6 +66,11 @@ implements StatusListener
maze.resetCellRenderer(hexagon, frameSize);
}
public void setBackground(boolean background) {
addStatus(background ? "view background" : "hide background");
maze.setViewBackground(background);
}
private class MazeFrame extends JFrame
{
MazeFrame(LabyModel model, WidthHeightProvider frame, MazeParams params) {

View File

@@ -1,6 +1,7 @@
package org.artisanlogiciel.games.maze.gui;
import org.artisanlogiciel.games.maze.Brick;
import org.artisanlogiciel.games.maze.Position;
import org.artisanlogiciel.games.maze.model.WidthHeightProvider;
import java.awt.*;
@@ -20,15 +21,15 @@ extends MazeCellRenderer
super(model, frame, x, y);
}
void drawLine(Graphics g, int refx, int refy, int x, int y, int a, int b) {
void drawLine(Graphics g, Position ref, int x, int y, int a, int b) {
g.drawLine(
(int) ((refx + x) * width / SUBCELL), (int) ((refy + y) * height / SUBCELL),
(int) ((refx + a) * width / SUBCELL),(int) ((refy + b) * height / SUBCELL));
(int) ((ref.getX() + x) * width / SUBCELL), (int) ((ref.getY() + y) * height / SUBCELL),
(int) ((ref.getX() + a) * width / SUBCELL),(int) ((ref.getY() + b) * height / SUBCELL));
}
@Override
public void drawBackground(Graphics g, int pX, int pY, short walls) {
UV uv= new UV(pX,pY);
public void drawBackground(Graphics g, Position cell, short walls) {
UV uv= new UV(cell);
int x = offsetX + (int) (uv.getX() * width / SUBCELL);
int y = offsetY + (int) (uv.getY() * height / SUBCELL);
Color savecolor = g.getColor();
@@ -39,47 +40,39 @@ extends MazeCellRenderer
}
class UV
extends Position
{
int u;
int v;
UV(int u, int v)
UV(Position p)
{
this.u=u;
this.v=v;
}
int getX()
{
return u*4 + 2*(v%2);
}
int getY()
{
return 3*v;
super(p);
u=p.getX();
v=p.getY();
setX(u*4 + 2*(v%2));
setY(3*v);
}
}
@Override
public void drawWalls(Graphics g, int pX, int pY, short walls) {
drawHalfNW(g, pX, pY, walls);
public void drawWalls(Graphics g, Position cell, short walls) {
drawHalfNW(g, cell, walls);
}
void drawHalfNW(Graphics g, int refx, int refy, short walls)
void drawHalfNW(Graphics g, Position cell, short walls)
{
UV uv= new UV(refx,refy);
int x = uv.getX();
int y = uv.getY();
UV uv= new UV(cell);
// NW : (0,1) - (2,0)
if (Brick.isFlagSet( Brick.UP, walls))
drawLine(g,x, y, 0,1,2,0);
drawLine(g,uv, 0,1,2,0);
// W : (0,1) - (0,3)
if (Brick.isFlagSet( Brick.LEFT, walls))
drawLine(g,x,y,0,1,0,3);
drawLine(g,uv,0,1,0,3);
// NE : (2,0) - (4,1)
// if (Brick.isFlagSet( Brick.NE, walls))
drawLine(g,x,y,2,0,4,1);
drawLine(g,uv,2,0,4,1);
}
void drawHalfSW(Graphics g)

View File

@@ -82,25 +82,25 @@ public class MazeCellRenderer {
Math.max(0,color.getBlue() - greylevel));
return greyedColor;
}
public void drawBackground(Graphics g, int pX, int pY, short walls) {
int x = offsetX + (int) (pX * width);
int y = offsetY + (int) (pY * height);
public void drawBackground(Graphics g, Position cell, short walls) {
int x = offsetX + (int) (cell.getX() * width);
int y = offsetY + (int) (cell.getY() * height);
Color savecolor = g.getColor();
int greylevel = walls << 2;
g.setColor(getGreyedColor(savecolor,greylevel));
//g.fillRect(x+1,y+1,((int)width) -1,((int)height) - 1);
g.fillRect(x,y,((int)width),((int)height));
}
public void drawWalls(Graphics g, int pX, int pY, short walls) {
int x = offsetX + (int) (pX * width);
int y = offsetY + (int) (pY * height);
if ((pY == 0) && ( Brick.isFlagSet( Brick.UP, walls)))
public void drawWalls(Graphics g, Position cell, short walls) {
int x = offsetX + (int) (cell.getX() * width);
int y = offsetY + (int) (cell.getY() * height);
if ((cell.getY() == 0) && ( Brick.isFlagSet( Brick.UP, walls)))
g.drawLine(x, y, x + (int) width, y);
if (Brick.isFlagSet( Brick.DOWN, walls))
g.drawLine(x, y + (int) height, x + (int) width, y + (int) height);
if (Brick.isFlagSet( Brick.RIGHT, walls))
g.drawLine(x + (int) width, y, x + (int) width, y + (int) height);
if ((pX == 0) && (Brick.isFlagSet( Brick.LEFT, walls)))
if ((cell.getX() == 0) && (Brick.isFlagSet( Brick.LEFT, walls)))
g.drawLine(x, y, x, y + (int) height);
}
@@ -145,23 +145,23 @@ public class MazeCellRenderer {
}
}
public void drawDot(Graphics g, Position dot, int pX, int pY, int mX, int mY) {
public void drawDot(Graphics g, Position dot, Position min, Position max) {
double h = getHeight();
double radius = (h > width) ? width : h;
int a = (int) (radius / 4);
if ((dot.getX() >= pX) && (dot.getY() >= pY) && (dot.getX() < mX) && (dot.getY() < mY)) {
if ((dot.getX() >= min.getX()) && (dot.getY() >= min.getY()) && (dot.getX() < max.getX()) && (dot.getY() < max.getY())) {
int x = offsetX + (int) (dot.getX() * width);
int y = offsetY + (int) (dot.getY() * h);
int r2 = (int) radius ; //(int) ((radius * 3) / 4);
g.drawOval(x, y, r2, r2);
// g.drawLine(x+a,y+a,x+width-a,y+height-a);
// g.drawLine(x+a,y+height-a,x+width-a,y+a);
} else {
int x = offsetX + (int) (pX * width);
int y = offsetY + (int) (pY * h);
}
/* why do we drwa anything at all ?
else {
int x = offsetX + (int) (min.getX() * width);
int y = offsetY + (int) (min.getY() * h);
g.drawLine(x + 1, y + 1, x + (int) width - 1, y + (int) h - 1);
}
*/
}
}

View File

@@ -0,0 +1,19 @@
package org.artisanlogiciel.games.maze.gui;
import java.awt.*;
public class MazeColorMap {
public Color background;
public Color wall;
public Color path;
public Color resolved_path;
public Color goal;
public MazeColorMap(Color background, Color wall, Color path, Color resolved_path, Color goal) {
this.background = background;
this.wall = wall;
this.path = path;
this.resolved_path = resolved_path;
this.goal = goal;
}
}

View File

@@ -19,16 +19,16 @@ import java.util.Date;
import java.util.LinkedList;
public class MazeComponent
extends JComponent
extends CellGridComponent
implements MazeCreationListener,
MazeResolutionListener,
MouseMotionListener,
Scrollable
{
private static final long serialVersionUID = 3163272907991176390L;
private boolean viewBackground = true;
LabyModelProvider map;
MazeCellRenderer cp;
Position current = null;
LinkedList<DirectionPosition> solvedPath = null;
LinkedList<DirectionPosition> drawingPath = null;
@@ -52,33 +52,8 @@ public class MazeComponent
StatusListener statusListener;
MazeCellRenderer createCellRenderer(boolean hexagon, WidthHeightProvider model, WidthHeightProvider frame)
{
MazeCellRenderer cellRenderer = hexagon ?
new HexagonCellRenderer(model, frame, 0, 0)
: new MazeCellRenderer(model, frame, 0, 0);
return cellRenderer;
}
MazeColorMap colorMap = new MazeColorMap(Color.white, Color.black, Color.blue, Color.green, Color.red);
public class MazeColorMap
{
public Color background;
public Color wall;
public Color path;
public Color resolved_path;
public Color goal;
public MazeColorMap(Color background, Color wall, Color path, Color resolved_path, Color goal) {
this.background = background;
this.wall = wall;
this.path = path;
this.resolved_path = resolved_path;
this.goal = goal;
}
}
public void setXpm(Xpm xpm)
{
this.xpm = xpm;
@@ -287,6 +262,15 @@ public class MazeComponent
*/
}
public void setViewBackground(boolean viewBackground)
{
this.viewBackground = viewBackground;
}
private boolean hasBackground()
{
return viewBackground && (xpm != null);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
@@ -300,101 +284,95 @@ public class MazeComponent
// Shape s=g.getClip();
Rectangle r = g.getClipBounds();
int mX = (int) ((double) r.getWidth() / cp.getWidth());
int mY = (int) ((double) r.getHeight() / cp.getHeight());
int pX = (int) ((double) (r.getX() - cp.getOffsetX()) / cp.getWidth());
int pY = (int) ((double) (r.getY() - cp.getOffsetY()) / cp.getHeight());
// visible part of grid :
// (min)-------------
// | |
// _______________(max)
if (pX < 0)
pX = 0;
if (pY < 0)
pY = 0;
if (pX >= map.getWidth())
return;
if (pY >= map.getHeight())
Position min = new Position(
(int) ((double) (r.getX() - cp.getOffsetX()) / cp.getWidth()),
(int) ((double) (r.getY() - cp.getOffsetY()) / cp.getHeight())
);
min.limitToMin(0,0);
if ( (min.getX() >= map.getWidth()) || (min.getY() >= map.getHeight()) )
return;
mX = mX + pX;
mY = mY + pY;
Position extent = new Position(
(int) ((double) r.getWidth() / cp.getWidth()),
(int) ((double) r.getHeight() / cp.getHeight())
);
Position max = min.translate(extent);
if (mX > map.getWidth()) {
mX = map.getWidth();
}
if (mY > map.getHeight()) {
mY = map.getHeight();
}
max.limitToMax(map.getWidth(), map.getHeight());
int mX = max.getX();
int mY = max.getY();
int aX = pX;
int aY = pY;
// draw background
for (; pY < mY; pY++) {
for (pX = 0; pX < mX; pX++) {
walls = map.getWalls(pX, pY);
if ( xpm != null ) {
int cX = ( pX < xpm.getWidth()) ? pX : xpm.getWidth() - 1;
int cY = ( pY < xpm.getHeight()) ? pY : xpm.getHeight() - 1;
g.setColor(xpm.getColor(cX,cY));
}
else {
g.setColor(colorMap.background);
}
cp.drawBackground(g, pX, pY, walls);
XYGridIterator iterator = new XYGridIterator(min,max);
Position cell = null;
while ( ( cell = iterator.next()) != null )
{
walls = map.getWalls(cell);
if ( hasBackground() ) {
Position colored = new Position(cell);
colored.limitToMax(xpm.getWidth() - 1, xpm.getHeight() -1 );
g.setColor(xpm.getColor(colored.getX(), colored.getY()));
}
else {
g.setColor(colorMap.background);
}
cp.drawBackground(g, cell, walls);
}
if ((sX == gX) && (sY == gY)) {
g.setColor(colorMap.goal);
} else {
g.setColor(colorMap.resolved_path);
cp.drawDot(g, new Position(gX, gY), pX, pY, mX, mY);
cp.drawDot(g, new Position(gX, gY), min, max);
g.setColor(colorMap.path);
}
cp.drawDot(g, new Position(sX, sY), pX, pY, mX, mY);
pX = aX;
pY = aY;
cp.drawDot(g, new Position(sX, sY), min, max);
// draw all walls within clip bounds horiz first then lines
g.setColor(colorMap.wall);
for (; pY < mY; pY++) {
for (pX = 0; pX < mX; pX++) {
walls = map.getWalls(pX, pY);
cp.drawWalls(g, pX, pY, walls);
}
iterator.reset();
cell = null;
while ( ( cell = iterator.next()) != null )
{
cp.drawWalls(g, cell, map.getWalls(cell));
}
if (this.showAll) {
statusListener.addStatus("*");
pX = aX;
pY = aY;
// draw all path within clip bounds horiz first then lines
for (; pY < mY; pY++) {
for (pX = 0; pX < mX; pX++) {
path = map.getPath(pX, pY);
g.setColor(colorMap.path);
if (path != 0) {
DirectionPosition dp = new DirectionPosition(path, new Position(pX, pY));
cp.drawPath(g, dp, pX, pY, pX + 1, pY + 1);
}
iterator.reset();
cell = null;
while ( ( cell = iterator.next()) != null )
{
path = map.getPath(cell.getX(), cell.getY());
g.setColor(colorMap.path);
if (path != 0) {
DirectionPosition dp = new DirectionPosition(path, new Position(cell));
cp.drawPath(g, dp, cell.getX(), cell.getY(), cell.getX() + 1, cell.getY() + 1);
}
}
}
pX = aX;
pY = aY;
synchronized (lockChange) {
g.setColor(colorMap.goal);
if (current != null) {
cp.drawDot(g, current, pX, pY, mX, mY);
cp.drawDot(g, current, min, max);
}
g.setColor(colorMap.resolved_path);
if (solvedPath != null) {
for (DirectionPosition resolved : solvedPath) {
cp.drawDot(g, resolved.getPosition(), pX, pY, mX, mY);
cp.drawPath(g, resolved, pX, pY, mX, mY);
cp.drawDot(g, resolved.getPosition(), min, max);
cp.drawPath(g, resolved, min.getX(), min.getY(), mX, mY);
}
}
}

View File

@@ -229,6 +229,11 @@ public class MazeControler extends JPanel {
return newActionButton("load", format, action);
}
private JPanel createPaletteChooser()
{
return new JPanel(new FlowLayout());
}
private JPanel createResolveQuitBar() {
JPanel resolveQuitBar = new JPanel(new FlowLayout());
@@ -388,6 +393,7 @@ public class MazeControler extends JPanel {
display.setAutoSize(autoSlide.isSelected());
}
});
final JCheckBox hexagon = new JCheckBox("hexagon");
hexagon.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
@@ -395,11 +401,19 @@ public class MazeControler extends JPanel {
}
});
final JCheckBox background = new JCheckBox("background");
background.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
display.setBackground(background.isSelected());
}
});
JPanel resizecontrol = new JPanel(new FlowLayout());
resizecontrol.add(showAll);
resizecontrol.add(slider);
resizecontrol.add(autoSlide);
resizecontrol.add(hexagon);
resizecontrol.add(background);
return resizecontrol;

View File

@@ -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;
/**
@@ -98,6 +99,12 @@ public class HalfSquareRasterModel
);
}
public short getWalls(Position cell)
{
return getWalls(cell.getX(), cell.getY());
}
public short getMoves(int x, int y)
{
// moves are where there is no walls ...

View File

@@ -2,6 +2,7 @@ package org.artisanlogiciel.games.maze.model;
import org.artisanlogiciel.games.maze.Brick;
import org.artisanlogiciel.games.maze.MovesProvider;
import org.artisanlogiciel.games.maze.Position;
import org.artisanlogiciel.games.maze.WallsProvider;
public class LineColumnModel
@@ -40,6 +41,10 @@ public class LineColumnModel
return c;
}
public short getWalls(Position cell) {
return getWalls(cell.getX(),cell.getY());
}
@Override
public short getWalls(int x, int y) {
return (short) (