diff options
-rw-r--r-- | core/test/ch/asynk/tankontank/BoardUtils.java | 100 | ||||
-rw-r--r-- | core/test/ch/asynk/tankontank/Helpers.java | 139 | ||||
-rw-r--r-- | core/test/ch/asynk/tankontank/LineOfSightTest.java | 2 |
3 files changed, 125 insertions, 116 deletions
diff --git a/core/test/ch/asynk/tankontank/BoardUtils.java b/core/test/ch/asynk/tankontank/BoardUtils.java index 5324a93..55cdba8 100644 --- a/core/test/ch/asynk/tankontank/BoardUtils.java +++ b/core/test/ch/asynk/tankontank/BoardUtils.java @@ -7,16 +7,14 @@ import java.util.HashSet; import org.junit.Test; import org.junit.Before; -import com.badlogic.gdx.math.GridPoint2; - -import ch.asynk.tankontank.engine.SearchBoard; +import ch.asynk.tankontank.engine.Tile; import static org.junit.Assert.assertTrue; public class BoardUtils { - private SearchBoard sb; private Helpers.FakeBoard b; + private Helpers.FakeSearchBoard sb; @Before public void initialize() @@ -25,68 +23,68 @@ public class BoardUtils int rows = 9; int mvt = 3; b = new Helpers.FakeBoard(cols, rows, 3); - sb = new SearchBoard(b, cols, rows); + sb = new Helpers.FakeSearchBoard(b, cols, rows); + } + + private void assertTile(Tile t, int col, int row) + { + assertTrue(t.getCol() == col); + assertTrue(t.getRow() == row); } @Test public void testPaths() { - List<ArrayList<SearchBoard.Node>> paths = b.buildPossiblePaths(2, 2, 4, 3); + int n = b.buildPossiblePaths(2, 2, 4, 3); - assertTrue(paths.size() == 8); + assertTrue(n == 8); - for(ArrayList<SearchBoard.Node> path : paths) { + for(int p = 0; p < n; p++) { - assertTrue((path.size() == 3) || (path.size() == 4)); - SearchBoard.Node n = path.get(0); - assertTrue(n.col == 2); - assertTrue(n.row == 2); - n = path.get(path.size() - 1); - assertTrue(n.col == 4); - assertTrue(n.row == 3); + List<Tile> path = b.possiblePaths.getPath(p); + int z = path.size(); - int i = 1; - if (path.size() == 3) { - n = path.get(i); - assertTrue(n.col == 3); - assertTrue((n.row == 3) || (n.row == 2)); - } else { - n = path.get(i); - if (n.col == 2) { + Tile t; + int i = 0; + if (z == 1) { + t = path.get(i); + assertTrue(t.getCol() == 3); + assertTrue((t.getRow() == 2) || (t.getRow() == 3));; + } else if (z == 2) { + t = path.get(i); + if (t.getCol() == 2) { i += 1; - if (n.row == 1) { - n = path.get(i); - assert(n.col == 3); - assert(n.row == 2); + if (t.getRow() == 1) { + t = path.get(i); + assertTile(t, 3, 2); } else { - assert(n.row == 3); - n = path.get(i); - assert(n.col == 3); - assert(n.row == 3); + assert(t.getRow()== 3); + t = path.get(i); + assertTile(t, 3, 3); } - } else if (n.col == 3) { + } else if (t.getCol() == 3) { i += 1; - if (n.row == 2) { - n = path.get(i); - if (n.col == 3) - assert(n.row == 3); + if (t.getRow() == 2) { + t = path.get(i); + if (t.getCol() == 3) + assert(t.getRow()== 3); else { - assert(n.col == 4); - assert(n.row == 2); + assertTile(t, 4, 2); } } else { - assert(n.row == 3); - n = path.get(i); - if (n.col == 3) - assert(n.row == 2); + assert(t.getRow() == 3); + t = path.get(i); + if (t.getCol() == 3) + assert(t.getRow() == 2); else { - assert(n.col == 4); - assert(n.row == 4); + assertTile(t, 4, 4); } } } else { assertTrue(false); } + } else { + assertTrue(false); } } } @@ -94,20 +92,16 @@ public class BoardUtils @Test public void testPathSet() { - HashSet<GridPoint2> points = new HashSet<GridPoint2>(); - - int n = b.buildPossiblePaths(2, 2, 3, 3, points); + int n = b.buildPossiblePaths(2, 2, 3, 3); assertTrue(n == 1); - assertTrue(points.size() == 0); - n = b.buildPossiblePaths(2, 2, 4, 3, points); + n = b.buildPossiblePaths(2, 2, 4, 3); assertTrue(n == 8); - assertTrue(points.size() == 6); n = b.togglePoint(3, 3); assertTrue(n == 1); - n = b.buildPossiblePaths(2, 2, 5, 3, points); + n = b.buildPossiblePaths(2, 2, 5, 3); assertTrue(n == 3); n = b.togglePoint(3, 3); @@ -190,8 +184,8 @@ public class BoardUtils assertTrue(sb.distance(6, 4, 8, 8) == 4); assertTrue(sb.distance(6, 4, 7, 8) == 4); - assertTrue(sb.distance(6, 4, 8, 9) == 5); - assertTrue(sb.distance(6, 4, 9, 9) == 5); + assertTrue(sb.distance(6, 4, 1, 1) == 5); + assertTrue(sb.distance(6, 4, 7, 0) == 5); assertTrue(sb.distance(6, 4, 9, 8) == 4); assertTrue(sb.distance(6, 4, 8, 7) == 3); assertTrue(sb.distance(6, 4, 7, 7) == 3); diff --git a/core/test/ch/asynk/tankontank/Helpers.java b/core/test/ch/asynk/tankontank/Helpers.java index c0cf64c..9f49fc4 100644 --- a/core/test/ch/asynk/tankontank/Helpers.java +++ b/core/test/ch/asynk/tankontank/Helpers.java @@ -4,13 +4,12 @@ import java.util.List; import java.util.ArrayList; import java.util.HashSet; -import com.badlogic.gdx.math.GridPoint2; - import ch.asynk.tankontank.engine.Pawn; import ch.asynk.tankontank.engine.Tile; import ch.asynk.tankontank.engine.Board; import ch.asynk.tankontank.engine.Orientation; import ch.asynk.tankontank.engine.SearchBoard; +import ch.asynk.tankontank.engine.PossiblePaths; public class Helpers { @@ -22,107 +21,123 @@ public class Helpers this.mvt = mvt; } - public int getMovementPoints() { return mvt; } - public int getRoadMarchBonus() { return 1; } - public int getAngleOfAttack() { return 0; } - public int getFlankSides() { return 0; } - public int getAttackRangeFrom(Tile tile) { return 3; } - - public boolean isHq() { return true; } - public boolean isHqOf(Pawn other) { return true; } - public boolean isUnit() { return true; } - public boolean isEnemy(Pawn other) { return true; } - - public boolean canMove() { return true; } - public boolean canRotate() { return true; } - public boolean canAttack() { return true; } - public boolean canAttack(Pawn other) { return true; } - public boolean canAssistAttackWithoutLos() { return true; } - - public void move(int cost) { } - public void rotate(Orientation o) { } - public void attack(Pawn target) { } - - public void reset() { } - public void revertLastMove() { } + @Override public int getMovementPoints() { return mvt; } + @Override public int getRoadMarchBonus() { return 1; } + @Override public int getAngleOfAttack() { return 0; } + @Override public int getFlankSides() { return 0; } + @Override public int getDefense(Tile tile) { return 8; } + @Override public int getEngagementRangeFrom(Tile tile) { return 3; } + + @Override public boolean isA(PawnId id) { return true; } + @Override public boolean isA(PawnType type) { return true; } + @Override public boolean isHq() { return true; } + @Override public boolean isHqOf(Pawn other) { return true; } + @Override public boolean isUnit() { return true; } + @Override public boolean isHardTarget() { return true; } + @Override public boolean isEnemy(Pawn other) { return true; } + + @Override public boolean canMove() { return true; } + @Override public boolean canRotate() { return true; } + @Override public boolean canEngage() { return true; } + @Override public boolean canEngage(Pawn other) { return true; } + @Override public boolean canAssistEngagementWithoutLos() { return true; } + + @Override public void move() { } + @Override public void engage() { } + + @Override public void reset() { } + @Override public void revertLastMove() { } } public static class FakeTile extends Tile { public boolean offMap; - public FakeTile() + public FakeTile(int col, int row) { - super(); + super(col, row); offMap = false; } - public FakeTile(boolean offMap) + public FakeTile(int col, int row, boolean offMap) { + super(col, row); this.offMap = offMap; } - public boolean isOffMap() + @Override public boolean isOffMap() { return offMap; } - public int costFrom(Pawn pawn, Orientation side, boolean road) { return 1; } - public int defenseFor(Pawn pawn ,List<Pawn> foes) { return 8; } + @Override public int costFrom(Pawn pawn, Orientation side) { return 1; } + @Override public int defense() { return 0; } + + @Override public boolean isA(TileTerrain terrain) { return true; } + @Override public boolean road(Orientation side) { return false; } + @Override public boolean atLeastOneMove(Pawn pawn) { return true; } + @Override public boolean blockLineOfSightFrom(Tile from) { return false; } + } + + public static class FakeSearchBoard extends SearchBoard + { + public FakeSearchBoard(Board b, int cols, int rows) + { + super(b, cols, rows); + } - public boolean road(Orientation side) { return false; } - public boolean atLeastOneMove(Pawn pawn) { return true; } - public boolean hasTargetsFor(Pawn pawn) { return false; } - public boolean blockLineOfSightFrom(Tile from) { return false; } + public int distance(int col0, int row0, int col1, int row1) + { + return distance(getNode(col0, row0), getNode(col1, row1)); + } } public static class FakeBoard extends Board { - private int cols; - private int rows; - public FakeTile fakeTiles[]; public FakePawn pawn; + public FakeTile fakeTiles[]; + public PossiblePaths possiblePaths; public FakeBoard(int cols, int rows, int mvt) { super(cols, rows); - this.cols = cols; - this.rows = rows; - fakeTiles = new FakeTile[cols * rows]; - for (int i = 0; i < rows; i++) { - for ( int j = 0; j < cols; j ++) - fakeTiles[j + (i * cols)] = new FakeTile(); + fakeTiles = new FakeTile[(cols + 2) * (rows + 2)]; + + int idx = 0; + for (int i = -1; i < (rows + 1); i++) { + for ( int j = -1; j < (cols + 1); j ++) { + boolean offmap = ((j < 0) || (i < 0) || (j >= cols) || (i >= rows)); + fakeTiles[idx] = new FakeTile((j + ((i + 1) / 2)), i, offmap); + idx += 1; + } } - fakeTiles[19].offMap = true; - fakeTiles[39].offMap = true; - fakeTiles[59].offMap = true; - fakeTiles[79].offMap = true; + + fakeTiles[ 36 - 2].offMap = true; + fakeTiles[ 60 - 2].offMap = true; + fakeTiles[ 84 - 2].offMap = true; + fakeTiles[108 - 2].offMap = true; + pawn = new FakePawn(mvt); + possiblePaths = new PossiblePaths(this, 10, 20, 5, 10); } @Override public Tile getTile(int col, int row) { - int colOffset = ((row + 1) / 2); - if ((col < colOffset) || (row < 0) || (row >= rows) || ((col - colOffset) >= cols)) - return new FakeTile(true); - int idx = ((col - colOffset)) + (row * cols); - return fakeTiles[idx]; - } - - public List<ArrayList<SearchBoard.Node>> buildPossiblePaths(int x0, int y0, int x1, int y1) - { - buildPossiblePaths(pawn, new GridPoint2(x0, y0), new GridPoint2(x1, y1), new HashSet<GridPoint2>()); - return paths; + int i = getTileOffset(col, row); + if (i < 0) + return null; + return fakeTiles[i]; } - public int buildPossiblePaths(int x0, int y0, int x1, int y1, HashSet<GridPoint2> points) + public int buildPossiblePaths(int x0, int y0, int x1, int y1) { - return buildPossiblePaths(pawn, new GridPoint2(x0, y0), new GridPoint2(x1, y1), points); + possiblePaths.init(pawn, getTile(x0, y0)); + return possiblePaths.build(getTile(x1, y1)); } public int togglePoint(int x, int y) { - return possiblePathsFilterToggle(new GridPoint2(x, y), new HashSet<GridPoint2>()); + return possiblePaths.toggleCtrlTile(getTile(x, y)); } } } diff --git a/core/test/ch/asynk/tankontank/LineOfSightTest.java b/core/test/ch/asynk/tankontank/LineOfSightTest.java index b559e03..ee8d629 100644 --- a/core/test/ch/asynk/tankontank/LineOfSightTest.java +++ b/core/test/ch/asynk/tankontank/LineOfSightTest.java @@ -32,7 +32,7 @@ public class LineOfSightTest private List<Node> lineOfSight(int x0, int y0, int x1, int y1) { - return sb.lineOfSight(x0, y0, x1, y1); + return sb.lineOfSight(x0, y0, x1, y1, true); } // from bottom left |