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

@@ -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;
}
}
}