-
Notifications
You must be signed in to change notification settings - Fork 5
/
Miner.java
89 lines (78 loc) · 2.92 KB
/
Miner.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package lectureplayer;
import battlecode.common.*;
import java.util.ArrayList;
public class Miner extends Unit {
int numDesignSchools = 0;
ArrayList<MapLocation> soupLocations = new ArrayList<MapLocation>();
public Miner(RobotController r) {
super(r);
}
public void takeTurn() throws GameActionException {
super.takeTurn();
numDesignSchools += comms.getNewDesignSchoolCount();
comms.updateSoupLocations(soupLocations);
checkIfSoupGone();
for (Direction dir : Util.directions)
if (tryMine(dir)) {
System.out.println("I mined soup! " + rc.getSoupCarrying());
MapLocation soupLoc = rc.getLocation().add(dir);
if (!soupLocations.contains(soupLoc)) {
comms.broadcastSoupLocation(soupLoc);
}
}
// mine first, then when full, deposit
for (Direction dir : Util.directions)
if (tryRefine(dir))
System.out.println("I refined soup! " + rc.getTeamSoup());
if (numDesignSchools < 3){
if(tryBuild(RobotType.DESIGN_SCHOOL, Util.randomDirection()))
System.out.println("created a design school");
}
if (rc.getSoupCarrying() == RobotType.MINER.soupLimit) {
// time to go back to the HQ
if(nav.goTo(hqLoc))
System.out.println("moved towards HQ");
} else if (soupLocations.size() > 0) {
nav.goTo(soupLocations.get(0));
rc.setIndicatorLine(rc.getLocation(), soupLocations.get(0), 255, 255, 0);
} else if (nav.goTo(Util.randomDirection())) {
// otherwise, move randomly as usual
System.out.println("I moved randomly!");
}
}
/**
* Attempts to mine soup in a given direction.
*
* @param dir The intended direction of mining
* @return true if a move was performed
* @throws GameActionException
*/
boolean tryMine(Direction dir) throws GameActionException {
if (rc.isReady() && rc.canMineSoup(dir)) {
rc.mineSoup(dir);
return true;
} else return false;
}
/**
* Attempts to refine soup in a given direction.
*
* @param dir The intended direction of refining
* @return true if a move was performed
* @throws GameActionException
*/
boolean tryRefine(Direction dir) throws GameActionException {
if (rc.isReady() && rc.canDepositSoup(dir)) {
rc.depositSoup(dir, rc.getSoupCarrying());
return true;
} else return false;
}
void checkIfSoupGone() throws GameActionException {
if (soupLocations.size() > 0) {
MapLocation targetSoupLoc = soupLocations.get(0);
if (rc.canSenseLocation(targetSoupLoc)
&& rc.senseSoup(targetSoupLoc) == 0) {
soupLocations.remove(0);
}
}
}
}