From 08fb894cf22534a05866e1b95436eb5e7c9cbc45 Mon Sep 17 00:00:00 2001 From: Soupdoop Date: Thu, 7 Jan 2016 20:43:34 -0500 Subject: [PATCH 1/2] Update RobotPlayer.java --- RobotPlayer.java | 206 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 203 insertions(+), 3 deletions(-) diff --git a/RobotPlayer.java b/RobotPlayer.java index dd18526..adf2028 100644 --- a/RobotPlayer.java +++ b/RobotPlayer.java @@ -4,6 +4,7 @@ import java.util.Random; //Use this instead of Math.random(); seeded by the robot's id so more likely to be random than Math.random import java.util.Arrays; import java.util.Collection; +import java.util.ArrayList; public class RobotPlayer{ /** @@ -488,8 +489,8 @@ public static void collectEnemyArchonID() { * @return array of MapLocations in sight but not on the map * */ - public static MapLocation[] inSightButOffMap(){ - MapLocation[] allInSight = MapLocation.getAllMapLocationsWithinRadiusSq(rc.getLocation(),rc.getType.sensorRadiusSquared); + public static MapLocation[] inSightButOffMap() throws GameActionException{ + MapLocation[] allInSight = MapLocation.getAllMapLocationsWithinRadiusSq(rc.getLocation(),rc.getType().sensorRadiusSquared); int numOffMap = 0; for(int i = 0; i < allInSight.length; i++){ if(rc.onTheMap(allInSight[i])){ @@ -508,6 +509,55 @@ public static MapLocation[] inSightButOffMap(){ } return ret; } + + /** + * boolean BUG + * + * Pathfinding method. Here's how it basically works: + * * * If we can move directly towards target, do that. + * * * Otherwise, we move along side of obstacle until we can move directly towards target + * * * * * Remember Branches (where we start/stop following walls. If we return to one, obviously following the one way didn't help, so we try the other one. If both are tried, we run the first way again for all following runs) + * + * @param target: where we are trying to get to + * @return true if we move, false if we don't + * + */ + public boolean BUG(MapLocation target) throws GameActionException{ + if(!rc.isCoreReady()) return false; + MapLocation current = rc.getLocation(); + Direction directionToTarget = current.directionTo(target); + if(rc.canMove(directionToTarget)){ + rc.move(directionToTarget); + Branch.lastStatus = 0; + if(current.add(directionToTarget).equals(target)){ + Branch.resetPath(); + } + return true; + }else if(Branch.lastStatus == 0){ + Branch decision = Branch.fork(current,target); + MapLocation bestChoice = decision.bestBranch(); + if(bestChoice != null){ + rc.move(current.directionTo(bestChoice)); + return true; + } + return false; + }else if(Branch.lastStatus == 1){ + MapLocation nextChoice = (new Branch(current,target)).getRightCanditate(); + if(nextChoice != null){ + rc.move(current.directionTo(nextChoice)); + return true; + } + return false; + }else if(Branch.lastStatus == -1){ + MapLocation nextChoice = (new Branch(current,target)).getLeftCanditate(); + if(nextChoice != null){ + rc.move(current.directionTo(nextChoice)); + return true; + } + return false; + } + return false; + } } /** @@ -677,5 +727,155 @@ public static Tuple encrypt(int type,int first,int second){ first |= type; return new Tuple(first,second); } - } + } + + /** + * class Branch + * + * Represents a branch in the BUG pathfinding + * + * + * + */ + public static class Branch{ + public MapLocation branchPoint; + public boolean branchedLeft = false,branchedRight = false; + public MapLocation target; + public static ArrayList branchesInCurrentPath = new ArrayList(); + public static int lastStatus = 0; + public Direction lastDirection = null; + private MapLocation leftCanditate; + private MapLocation rightCanditate; + private MapLocation bestCanditate; + public Branch(MapLocation whereWeAre,MapLocation targe){ + branchPoint = whereWeAre; + target = targe; + } + public static Branch fork(MapLocation here,MapLocation targe){ + Branch n = new Branch(here,targe); + for(int i = 0; i < branchesInCurrentPath.size(); i++){ + if(n.equals(branchesInCurrentPath.get(i))){ + return branchesInCurrentPath.get(i); + } + } + branchesInCurrentPath.add(n); + return n; + } + public static void resetPath(){ + branchesInCurrentPath = new ArrayList(); + } + public MapLocation bestBranch(){ + if(branchedLeft && !branchedRight){ + lastStatus = 1; + return getRightCanditate(); + } + if(branchedRight && !branchedLeft){ + lastStatus = -1; + return getLeftCanditate(); + } + if(bestCanditate != null){ + lastStatus = bestCanditate.equals(leftCanditate) ? -1 : 1 ; + return bestCanditate; + } + MapLocation left = getLeftCanditate(); + MapLocation right = getRightCanditate(); + if(left == null && right != null){ + lastStatus = 1; + bestCanditate = right; + return right; + }else if(right == null && left != null){ + lastStatus = -1; + bestCanditate = left; + return left; + }else if(right == null && left == null){ + return null; + } + if(left.distanceSquaredTo(target) < right.distanceSquaredTo(target)){ + lastStatus = -1; + bestCanditate = left; + return left; + } + lastStatus = 1; + bestCanditate = right; + return right; + } + public MapLocation getLeftCanditate(){ + if(leftCanditate != null){ + return leftCanditate; + } + ArrayList lefts = getAllLeftCanditates(); + if(lefts.size() == 0){ + return null; + } + int min = 0; + int accmin = lefts.get(0).distanceSquaredTo(target); + for(int i = 1; i < lefts.size(); i++){ + int newMin = lefts.get(i).distanceSquaredTo(target); + if(accmin > newMin){ + min = i; + accmin = newMin; + } + } + return lefts.get(min); + } + public MapLocation getRightCanditate(){ + if(rightCanditate != null){ + return rightCanditate; + } + ArrayList rights = getAllRightCanditates(); + if(rights.size() == 0){ + return null; + } + int min = 0; + int accmin = rights.get(0).distanceSquaredTo(target); + for(int i = 1; i < rights.size(); i++){ + int newMin = rights.get(i).distanceSquaredTo(target); + if(accmin > newMin){ + min = i; + accmin = newMin; + } + } + return rights.get(min); + } + public ArrayList getAllRightCanditates(){ + ArrayList base = new ArrayList(); + MapLocation[] options = MapLocation.getAllMapLocationsWithinRadiusSq(branchPoint,3); + for(int i = 0; i < options.length; i++){ + boolean isRight = false; + Direction forward = branchPoint.directionTo(options[i]); + Direction[] rights = new Direction[]{RESOURCE_FUNCTIONS.intToDir((RESOURCE_FUNCTIONS.dirToInt(forward) + 5) % 8),RESOURCE_FUNCTIONS.intToDir((RESOURCE_FUNCTIONS.dirToInt(forward) + 6) % 8),RESOURCE_FUNCTIONS.intToDir((RESOURCE_FUNCTIONS.dirToInt(forward) + 7) % 8)}; + if(rc.senseRubble(options[i]) < 100){ + for(int j = 0; j < rights.length && !isRight; j++){ + if(rc.senseRubble(options[i].add(rights[j])) > 100){ + isRight = true; + } + } + } + if(isRight){ + base.add(options[i]); + } + } + return base; + } + public ArrayList getAllLeftCanditates(){ + ArrayList base = new ArrayList(); + MapLocation[] options = MapLocation.getAllMapLocationsWithinRadiusSq(branchPoint,3); + for(int i = 0; i < options.length; i++){ + boolean isLeft = false; + Direction forward = branchPoint.directionTo(options[i]); + Direction[] lefts = new Direction[]{RESOURCE_FUNCTIONS.intToDir((RESOURCE_FUNCTIONS.dirToInt(forward) + 1) % 8),RESOURCE_FUNCTIONS.intToDir((RESOURCE_FUNCTIONS.dirToInt(forward) + 2) % 8),RESOURCE_FUNCTIONS.intToDir((RESOURCE_FUNCTIONS.dirToInt(forward) + 3) % 8)}; + if(rc.senseRubble(options[i]) < 100){ + for(int j = 0; j < lefts.length && !isLeft; j++){ + if(rc.senseRubble(options[i].add(lefts[j])) > 100){ + isLeft = true; + } + } + } + if(isLeft){ + base.add(options[i]); + } + } + return base; + } + } } From 98b60e5df1b1faad8149df0e395c8069b07fd9e7 Mon Sep 17 00:00:00 2001 From: Soupdoop Date: Thu, 7 Jan 2016 22:45:25 -0500 Subject: [PATCH 2/2] Update RobotPlayer.java --- RobotPlayer.java | 118 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 86 insertions(+), 32 deletions(-) diff --git a/RobotPlayer.java b/RobotPlayer.java index adf2028..638b73d 100644 --- a/RobotPlayer.java +++ b/RobotPlayer.java @@ -522,11 +522,16 @@ public static MapLocation[] inSightButOffMap() throws GameActionException{ * @return true if we move, false if we don't * */ - public boolean BUG(MapLocation target) throws GameActionException{ + public static boolean BUG(MapLocation target) throws GameActionException{ + rc.setIndicatorString(1,target.toString()); if(!rc.isCoreReady()) return false; MapLocation current = rc.getLocation(); Direction directionToTarget = current.directionTo(target); - if(rc.canMove(directionToTarget)){ + if(rc.canMove(directionToTarget) && (Branch.last == null || !Branch.last.contains(current.add(directionToTarget)))){ + rc.setIndicatorString(2,"Starting no branch // "); + rc.setIndicatorString(0,current.add(directionToTarget).toString() + " from no branch"); + Branch.last = new ArrayList(); + Branch.last.add(current); rc.move(directionToTarget); Branch.lastStatus = 0; if(current.add(directionToTarget).equals(target)){ @@ -534,28 +539,54 @@ public boolean BUG(MapLocation target) throws GameActionException{ } return true; }else if(Branch.lastStatus == 0){ + rc.setIndicatorString(2,"Starting choosing branch"); Branch decision = Branch.fork(current,target); + rc.setIndicatorString(1,"Made it past $fork"); MapLocation bestChoice = decision.bestBranch(); + rc.setIndicatorString(1,"Made it past @bestBranch"); if(bestChoice != null){ + rc.setIndicatorString(0,bestChoice.toString() + " from choosing branch"); + Branch.last.add(current); rc.move(current.directionTo(bestChoice)); return true; } + rc.setIndicatorString(0,"Failed in choosing branch"); return false; }else if(Branch.lastStatus == 1){ - MapLocation nextChoice = (new Branch(current,target)).getRightCanditate(); + rc.setIndicatorString(2,"Starting right branch"); + Branch currentStep = new Branch(current,target); + Branch temp = Branch.getFromEarlier(currentStep); + if(temp != null){ + currentStep = temp; + } + MapLocation nextChoice = currentStep.getRightCanditate(); if(nextChoice != null){ + rc.setIndicatorString(0,nextChoice.toString() + " from right branch"); + Branch.last.add(current); rc.move(current.directionTo(nextChoice)); return true; } + rc.setIndicatorString(0,"Failed in right branch"); return false; }else if(Branch.lastStatus == -1){ - MapLocation nextChoice = (new Branch(current,target)).getLeftCanditate(); + rc.setIndicatorString(2,"Starting left branch"); + Branch currentStep = new Branch(current,target); + Branch temp = Branch.getFromEarlier(currentStep); + if(temp != null){ + currentStep = temp; + + } + MapLocation nextChoice = currentStep.getLeftCanditate(); if(nextChoice != null){ + rc.setIndicatorString(0,nextChoice.toString() + " from left branch"); + Branch.last.add(current); rc.move(current.directionTo(nextChoice)); return true; } + rc.setIndicatorString(0,"Failed in choosing branch"); return false; } + rc.setIndicatorString(0,"Failed outside of branch"); return false; } } @@ -747,19 +778,27 @@ public static class Branch{ private MapLocation leftCanditate; private MapLocation rightCanditate; private MapLocation bestCanditate; + public static ArrayList last = new ArrayList(); public Branch(MapLocation whereWeAre,MapLocation targe){ branchPoint = whereWeAre; target = targe; } public static Branch fork(MapLocation here,MapLocation targe){ Branch n = new Branch(here,targe); + Branch e = getFromEarlier(n); + if(e == null){ + return n; + } + return e; + } + public static Branch getFromEarlier(Branch n){ for(int i = 0; i < branchesInCurrentPath.size(); i++){ - if(n.equals(branchesInCurrentPath.get(i))){ + System.out.println(n.branchPoint.toString() + "::" + branchesInCurrentPath.get(i).branchPoint.toString()); + if(n.equal(branchesInCurrentPath.get(i))){ return branchesInCurrentPath.get(i); } } - branchesInCurrentPath.add(n); - return n; + return null; } public static void resetPath(){ branchesInCurrentPath = new ArrayList(); @@ -785,8 +824,7 @@ public MapLocation bestBranch(){ return right; }else if(right == null && left != null){ lastStatus = -1; - bestCanditate = left; - return left; + bestCanditate = left; return left; }else if(right == null && left == null){ return null; } @@ -840,42 +878,58 @@ public MapLocation getRightCanditate(){ public ArrayList getAllRightCanditates(){ ArrayList base = new ArrayList(); MapLocation[] options = MapLocation.getAllMapLocationsWithinRadiusSq(branchPoint,3); - for(int i = 0; i < options.length; i++){ - boolean isRight = false; - Direction forward = branchPoint.directionTo(options[i]); - Direction[] rights = new Direction[]{RESOURCE_FUNCTIONS.intToDir((RESOURCE_FUNCTIONS.dirToInt(forward) + 5) % 8),RESOURCE_FUNCTIONS.intToDir((RESOURCE_FUNCTIONS.dirToInt(forward) + 6) % 8),RESOURCE_FUNCTIONS.intToDir((RESOURCE_FUNCTIONS.dirToInt(forward) + 7) % 8)}; - if(rc.senseRubble(options[i]) < 100){ - for(int j = 0; j < rights.length && !isRight; j++){ - if(rc.senseRubble(options[i].add(rights[j])) > 100){ - isRight = true; + try{ + for(int i = 0; i < options.length; i++){ + boolean isRight = false; + Direction forward = branchPoint.directionTo(options[i]); + Direction[] rights = new Direction[]{RESOURCE_FUNCTIONS.intToDir((RESOURCE_FUNCTIONS.dirToInt(forward) + 5) % 8),RESOURCE_FUNCTIONS.intToDir((RESOURCE_FUNCTIONS.dirToInt(forward) + 6) % 8),RESOURCE_FUNCTIONS.intToDir((RESOURCE_FUNCTIONS.dirToInt(forward) + 7) % 8)}; + if(rc.senseRubble(options[i]) < 100 && rc.onTheMap(options[i]) && !branchPoint.equals(options[i])){ + for(int j = 0; j < rights.length && !isRight; j++){ + if(rc.senseRubble(options[i].add(rights[j])) > 100 && !last.contains(options[i])){ + isRight = true; + } } } + if(isRight){ + base.add(options[i]); + } } - if(isRight){ - base.add(options[i]); - } + }catch(Exception e){ + e.printStackTrace(); } return base; } public ArrayList getAllLeftCanditates(){ ArrayList base = new ArrayList(); - MapLocation[] options = MapLocation.getAllMapLocationsWithinRadiusSq(branchPoint,3); - for(int i = 0; i < options.length; i++){ - boolean isLeft = false; - Direction forward = branchPoint.directionTo(options[i]); - Direction[] lefts = new Direction[]{RESOURCE_FUNCTIONS.intToDir((RESOURCE_FUNCTIONS.dirToInt(forward) + 1) % 8),RESOURCE_FUNCTIONS.intToDir((RESOURCE_FUNCTIONS.dirToInt(forward) + 2) % 8),RESOURCE_FUNCTIONS.intToDir((RESOURCE_FUNCTIONS.dirToInt(forward) + 3) % 8)}; - if(rc.senseRubble(options[i]) < 100){ - for(int j = 0; j < lefts.length && !isLeft; j++){ - if(rc.senseRubble(options[i].add(lefts[j])) > 100){ - isLeft = true; + try{ + MapLocation[] options = MapLocation.getAllMapLocationsWithinRadiusSq(branchPoint,3); + for(int i = 0; i < options.length; i++){ + boolean isLeft = false; + Direction forward = branchPoint.directionTo(options[i]); + Direction[] lefts = new Direction[]{RESOURCE_FUNCTIONS.intToDir((RESOURCE_FUNCTIONS.dirToInt(forward) + 1) % 8),RESOURCE_FUNCTIONS.intToDir((RESOURCE_FUNCTIONS.dirToInt(forward) + 2) % 8),RESOURCE_FUNCTIONS.intToDir((RESOURCE_FUNCTIONS.dirToInt(forward) + 3) % 8)}; + if(rc.senseRubble(options[i]) < 100 && rc.onTheMap(options[i]) && !branchPoint.equals(options[i])){ + for(int j = 0; j < lefts.length && !isLeft; j++){ + if(rc.senseRubble(options[i].add(lefts[j])) > 100&& !last.contains(options[i])){ + isLeft = true; + } } } + if(isLeft){ + base.add(options[i]); + } } - if(isLeft){ - base.add(options[i]); - } + }catch(Exception e){ + e.printStackTrace(); } return base; } + public boolean equal(Object other){ + Branch o = (Branch)other; + System.out.println(this.branchPoint.toString() + "::" + o.branchPoint.toString()); + if(o.branchPoint.equals(this.branchPoint)){ + return true; + } + return false; + } } }