From 3fbaacf8ab9e77e8b964874226cbe1a2bf01bae8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Mon, 4 Jan 2016 23:51:51 +0100 Subject: PathBuilder: factorise best(Path, Path) out of chooseBest() and chooseExit() --- .../ch/asynk/rustanddust/engine/PathBuilder.java | 33 +++++++++++----------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/core/src/ch/asynk/rustanddust/engine/PathBuilder.java b/core/src/ch/asynk/rustanddust/engine/PathBuilder.java index 3bd057b..453583f 100644 --- a/core/src/ch/asynk/rustanddust/engine/PathBuilder.java +++ b/core/src/ch/asynk/rustanddust/engine/PathBuilder.java @@ -124,15 +124,13 @@ public class PathBuilder implements Disposable List ps = getPaths(); if (ps.size() > 1) { - Path good = ps.get(0); - for (Path path : ps) { - if ( (path.fitness > good.fitness) || ((path.fitness == good.fitness) && (path.cost < good.cost))) - good = path; - } + Path good = null; + for (Path p : ps) + good = best(good, p); keepOnly(good); } - return 1; + return ps.size(); } public int chooseShortest() @@ -148,7 +146,7 @@ public class PathBuilder implements Disposable keepOnly(good); } - return 1; + return ps.size(); } public int chooseExit(Orientation o) @@ -163,14 +161,8 @@ public class PathBuilder implements Disposable if (ps.size() > 1) { good = null; for (Path p : getPaths()) { - if (pathCanExit(p, mvt, cost, rBonus)) { - if (good != null) { - if ( (p.fitness > good.fitness) || ((p.fitness == good.fitness) && (p.cost < good.cost))) - good = p; - } else { - good = p; - } - } + if (pathCanExit(p, mvt, cost, rBonus)) + good = best(good, p); } keepOnly(good); @@ -186,7 +178,16 @@ public class PathBuilder implements Disposable } to = board.getAdjTileAt(to, o); - return 1; + return ps.size(); + } + + private Path best(Path a, Path b) + { + if (a == null) + return b; + if ( (b.fitness > a.fitness) || ((b.fitness == a.fitness) && (b.cost < a.cost))) + return b; + return a; } private void keepOnly(Path path) -- cgit v1.1-2-g2b99