-
Notifications
You must be signed in to change notification settings - Fork 5
/
Navigation.java
41 lines (35 loc) · 1.21 KB
/
Navigation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package lectureplayer;
import battlecode.common.*;
public class Navigation {
RobotController rc;
// state related only to navigation should go here
public Navigation(RobotController r) {
rc = r;
}
/**
* Attempts to move in a given direction.
*
* @param dir The intended direction of movement
* @return true if a move was performed
* @throws GameActionException
*/
boolean tryMove(Direction dir) throws GameActionException {
if (rc.isReady() && rc.canMove(dir) && !rc.senseFlooding(rc.getLocation().add(dir))) {
rc.move(dir);
return true;
} else return false;
}
// tries to move in the general direction of dir
boolean goTo(Direction dir) throws GameActionException {
Direction[] toTry = {dir, dir.rotateLeft(), dir.rotateRight(), dir.rotateLeft().rotateLeft(), dir.rotateRight().rotateRight()};
for (Direction d : toTry){
if(tryMove(d))
return true;
}
return false;
}
// navigate towards a particular location
boolean goTo(MapLocation destination) throws GameActionException {
return goTo(rc.getLocation().directionTo(destination));
}
}