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

@@ -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) {