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:
46
java/org/artisanlogiciel/games/maze/XYGridIterator.java
Normal file
46
java/org/artisanlogiciel/games/maze/XYGridIterator.java
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user