From eec2f37d438222450f41ceeafc7267224f8025f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Wed, 1 Oct 2014 13:45:03 +0200 Subject: findAllPaths: support roadMarch, do not touch nodes state, returns List> --- .../ch/asynk/tankontank/engine/SearchBoard.java | 53 ++++++++++------------ 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/core/src/ch/asynk/tankontank/engine/SearchBoard.java b/core/src/ch/asynk/tankontank/engine/SearchBoard.java index a782cb5..7e03153 100644 --- a/core/src/ch/asynk/tankontank/engine/SearchBoard.java +++ b/core/src/ch/asynk/tankontank/engine/SearchBoard.java @@ -35,7 +35,7 @@ public class SearchBoard private ArrayDeque roadMarch; private ArrayDeque path = new ArrayDeque(20); - private List> possiblePaths = new LinkedList>(); + private List> possiblePaths = new LinkedList>(); private List moves; private List targets; @@ -487,7 +487,7 @@ public class SearchBoard return los; } - public List> possiblePaths(Pawn pawn, int col0, int row0, int col1, int row1) + public List> possiblePaths(Pawn pawn, int col0, int row0, int col1, int row1) { path.clear(); possiblePaths.clear(); @@ -495,43 +495,40 @@ public class SearchBoard Node from = getNode(col0, row0); Node to = getNode(col1, row1); - searchCount += 1; - // TODO : search - from.search = searchCount; - from.remaining = pawn.getMovementPoints(); - // TODO : roadMarch - from.roadMarch = true; - path.add(from); - findAllPaths(pawn, from, to); + findAllPaths(pawn, from, to, pawn.getMovementPoints(), true, pawn.getMovementPoints()); return possiblePaths; } - private void findAllPaths(Pawn pawn, Node from, Node to) + private void findAllPaths(Pawn pawn, Node from, Node to, int mvtLeft, boolean roadMarch, int roadMarchBonus) { Node moves[] = new Node[6]; adjacentMoves(from, moves); for(int i = 0; i < 6; i++) { Node next = moves[i]; - if (next != null) { - Tile t = board.getTile(next.col, next.row); - boolean road = t.road(sides[i]); - int r = (from.remaining - t.costFrom(pawn, sides[i], road)); - if ((r >= 0) && (distance(next, to) <= r)) { - if (next == to) { - ArrayDeque temp = new ArrayDeque(path.size() + 1); - temp.add(next); - for (Node n: path) - temp.add(n); - possiblePaths.add(temp); - } else { - next.remaining = r; - path.push(next); - findAllPaths(pawn, next, to); - path.pop(); - } + if (next == null) continue; + + Tile t = board.getTile(next.col, next.row); + boolean road = t.road(sides[i]); + int cost = t.costFrom(pawn, sides[i], road); + int r = (mvtLeft - cost); + roadMarch = roadMarch & road; + if (roadMarch) r += roadMarchBonus; + + if ((r >= 0) && (distance(next, to) <= r)) { + if (next == to) { + Vector temp = new Vector(path.size() + 1); + temp.add(next); + for (Node n: path) + temp.add(n); + possiblePaths.add(temp); + } else { + next.remaining = r; + path.push(next); + findAllPaths(pawn, next, to, (mvtLeft - cost), roadMarch, roadMarchBonus); + path.pop(); } } } -- cgit v1.1-2-g2b99