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:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user