Skip to content

Commit 300fcc4

Browse files
all bot functions fully setup
1 parent 8f86842 commit 300fcc4

File tree

2 files changed

+120
-60
lines changed

2 files changed

+120
-60
lines changed

java/gradle.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# What are you trying to run?
22

33
teamA=examplefuncsplayer
4-
teamB=examplefuncsplayer
4+
teamB=myplayer
55
maps=DefaultSmall
66

77
# Some optional configurations you can adjust as you need

java/src/myplayer/RobotPlayer.java

+119-59
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,50 @@ public class RobotPlayer {
1616
static final Random rng = new Random(6147);
1717

1818
static final Direction[] directions = {
19-
Direction.NORTH,
20-
Direction.NORTHEAST,
21-
Direction.EAST,
22-
Direction.SOUTHEAST,
23-
Direction.SOUTH,
24-
Direction.SOUTHWEST,
25-
Direction.WEST,
26-
Direction.NORTHWEST,
19+
Direction.NORTH,
20+
Direction.NORTHEAST,
21+
Direction.EAST,
22+
Direction.SOUTHEAST,
23+
Direction.SOUTH,
24+
Direction.SOUTHWEST,
25+
Direction.WEST,
26+
Direction.NORTHWEST,
2727
};
2828

2929
/**
30-
* run() is the method that is called when a robot is instantiated in the Battlecode world.
31-
* It is like the main function for your robot. If this method returns, the robot dies!
30+
* run() is the method that is called when a robot is instantiated in the
31+
* Battlecode world.
32+
* It is like the main function for your robot. If this method returns, the
33+
* robot dies!
3234
*
33-
* @param rc The RobotController object. You use it to perform actions from this robot, and to get
34-
* information on its current status. Essentially your portal to interacting with the world.
35+
* @param rc The RobotController object. You use it to perform actions from this
36+
* robot, and to get
37+
* information on its current status. Essentially your portal to
38+
* interacting with the world.
3539
**/
3640
@SuppressWarnings("unused")
3741
public static void run(RobotController rc) throws GameActionException {
3842

3943
rc.setIndicatorString("Hello world!");
40-
4144
while (true) {
42-
turnCount += 1;
45+
turnCount += 1;
4346

4447
try {
45-
switch (rc.getType()){
46-
case SOLDIER: runSoldier(rc); break;
47-
case MOPPER: runMopper(rc); break;
48-
case SPLASHER: runSplasher(rc); break; // Consider upgrading examplefuncsplayer to use splashers!
49-
default: runTower(rc); break;
48+
switch (rc.getType()) {
49+
case SOLDIER:
50+
runSoldier(rc);
51+
break;
52+
case MOPPER:
53+
runMopper(rc);
54+
break;
55+
case SPLASHER:
56+
runSplasher(rc);
57+
break; // Consider upgrading examplefuncsplayer to use splashers!
58+
default:
59+
runTower(rc);
60+
break;
5061
}
51-
}
52-
catch (GameActionException e) {
62+
} catch (GameActionException e) {
5363
System.out.println("GameActionException");
5464
e.printStackTrace();
5565
} catch (Exception e) {
@@ -58,35 +68,32 @@ public static void run(RobotController rc) throws GameActionException {
5868

5969
} finally {
6070
// Signify we've done everything we want to do, thereby ending our turn.
61-
// This will make our code wait until the next turn, and then perform this loop again.
71+
// This will make our code wait until the next turn, and then perform this loop
72+
// again.
6273
Clock.yield();
6374
}
6475
}
6576
}
6677

67-
public static void runSplasher(RobotController rc) {
68-
rc.setIndicatorString("I do nothing.");
69-
}
7078

7179
/**
7280
* Run a single turn for towers.
73-
* This code is wrapped inside the infinite loop in run(), so it is called once per turn.
81+
* This code is wrapped inside the infinite loop in run(), so it is called once
82+
* per turn.
7483
*/
75-
public static void runTower(RobotController rc) throws GameActionException{
84+
public static void runTower(RobotController rc) throws GameActionException {
7685
// Pick a direction to build in.
7786
Direction dir = directions[rng.nextInt(directions.length)];
7887
MapLocation nextLoc = rc.getLocation().add(dir);
7988
// Pick a random robot type to build.
8089
int robotType = rng.nextInt(3);
81-
if (robotType == 0 && rc.canBuildRobot(UnitType.SOLDIER, nextLoc)){
90+
if (robotType == 0 && rc.canBuildRobot(UnitType.SOLDIER, nextLoc)) {
8291
rc.buildRobot(UnitType.SOLDIER, nextLoc);
8392
System.out.println("BUILT A SOLDIER");
84-
}
85-
else if (robotType == 1 && rc.canBuildRobot(UnitType.MOPPER, nextLoc)){
93+
} else if (robotType == 1 && rc.canBuildRobot(UnitType.MOPPER, nextLoc)) {
8694
rc.buildRobot(UnitType.MOPPER, nextLoc);
8795
System.out.println("BUILT A MOPPER");
88-
}
89-
else if (robotType == 2 && rc.canBuildRobot(UnitType.SPLASHER, nextLoc)){
96+
} else if (robotType == 2 && rc.canBuildRobot(UnitType.SPLASHER, nextLoc)) {
9097
// rc.buildRobot(UnitType.SPLASHER, nextLoc);
9198
// System.out.println("BUILT A SPLASHER");
9299
rc.setIndicatorString("SPLASHER NOT IMPLEMENTED YET");
@@ -101,42 +108,95 @@ else if (robotType == 2 && rc.canBuildRobot(UnitType.SPLASHER, nextLoc)){
101108
// TODO: can we attack other bots?
102109
}
103110

111+
public static void runSplasher(RobotController rc) throws GameActionException {
112+
// Sense information about all visible nearby tiles.
113+
MapInfo[] nearbyTiles = rc.senseNearbyMapInfos();
114+
// Search for a nearby ruin to complete.
115+
MapInfo curRuin = null;
116+
for (MapInfo tile : nearbyTiles) {
117+
if (tile.hasRuin()) {
118+
curRuin = tile;
119+
}
120+
}
121+
if (curRuin != null) {
122+
MapLocation targetLoc = curRuin.getMapLocation();
123+
Direction dir = rc.getLocation().directionTo(targetLoc);
124+
if (rc.canMove(dir))
125+
rc.move(dir);
126+
// Mark the pattern we need to draw to build a tower here if we haven't already.
127+
MapLocation shouldBeMarked = curRuin.getMapLocation().subtract(dir);
128+
if (rc.senseMapInfo(shouldBeMarked).getMark() == PaintType.EMPTY
129+
&& rc.canMarkTowerPattern(UnitType.LEVEL_ONE_PAINT_TOWER, targetLoc)) {
130+
rc.markTowerPattern(UnitType.LEVEL_ONE_PAINT_TOWER, targetLoc);
131+
System.out.println("Trying to build a tower at " + targetLoc);
132+
}
133+
// Fill in any spots in the pattern with the appropriate paint.
134+
for (MapInfo patternTile : rc.senseNearbyMapInfos(targetLoc, 8)) {
135+
if (patternTile.getMark() != patternTile.getPaint() && patternTile.getMark() != PaintType.EMPTY) {
136+
boolean useSecondaryColor = patternTile.getMark() == PaintType.ALLY_SECONDARY;
137+
if (rc.canAttack(patternTile.getMapLocation()))
138+
rc.attack(patternTile.getMapLocation(), useSecondaryColor);
139+
}
140+
}
141+
// Complete the ruin if we can.
142+
if (rc.canCompleteTowerPattern(UnitType.LEVEL_ONE_PAINT_TOWER, targetLoc)) {
143+
rc.completeTowerPattern(UnitType.LEVEL_ONE_PAINT_TOWER, targetLoc);
144+
rc.setTimelineMarker("Tower built", 0, 255, 0);
145+
System.out.println("Built a tower at " + targetLoc + "!");
146+
}
147+
}
104148

149+
// Move and attack randomly if no objective.
150+
Direction dir = directions[rng.nextInt(directions.length)];
151+
MapLocation nextLoc = rc.getLocation().add(dir);
152+
if (rc.canMove(dir)) {
153+
rc.move(dir);
154+
}
155+
// Try to paint beneath us as we walk to avoid paint penalties.
156+
// Avoiding wasting paint by re-painting our own tiles.
157+
MapInfo currentTile = rc.senseMapInfo(rc.getLocation());
158+
if (!currentTile.getPaint().isAlly() && rc.canAttack(rc.getLocation())) {
159+
rc.attack(rc.getLocation());
160+
}
161+
162+
}
105163
/**
106164
* Run a single turn for a Soldier.
107-
* This code is wrapped inside the infinite loop in run(), so it is called once per turn.
165+
* This code is wrapped inside the infinite loop in run(), so it is called once
166+
* per turn.
108167
*/
109-
public static void runSoldier(RobotController rc) throws GameActionException{
168+
public static void runSoldier(RobotController rc) throws GameActionException {
110169
// Sense information about all visible nearby tiles.
111170
MapInfo[] nearbyTiles = rc.senseNearbyMapInfos();
112171
// Search for a nearby ruin to complete.
113172
MapInfo curRuin = null;
114-
for (MapInfo tile : nearbyTiles){
115-
if (tile.hasRuin()){
173+
for (MapInfo tile : nearbyTiles) {
174+
if (tile.hasRuin()) {
116175
curRuin = tile;
117176
}
118177
}
119-
if (curRuin != null){
178+
if (curRuin != null) {
120179
MapLocation targetLoc = curRuin.getMapLocation();
121180
Direction dir = rc.getLocation().directionTo(targetLoc);
122181
if (rc.canMove(dir))
123182
rc.move(dir);
124183
// Mark the pattern we need to draw to build a tower here if we haven't already.
125184
MapLocation shouldBeMarked = curRuin.getMapLocation().subtract(dir);
126-
if (rc.senseMapInfo(shouldBeMarked).getMark() == PaintType.EMPTY && rc.canMarkTowerPattern(UnitType.LEVEL_ONE_PAINT_TOWER, targetLoc)){
185+
if (rc.senseMapInfo(shouldBeMarked).getMark() == PaintType.EMPTY
186+
&& rc.canMarkTowerPattern(UnitType.LEVEL_ONE_PAINT_TOWER, targetLoc)) {
127187
rc.markTowerPattern(UnitType.LEVEL_ONE_PAINT_TOWER, targetLoc);
128188
System.out.println("Trying to build a tower at " + targetLoc);
129189
}
130190
// Fill in any spots in the pattern with the appropriate paint.
131-
for (MapInfo patternTile : rc.senseNearbyMapInfos(targetLoc, 8)){
132-
if (patternTile.getMark() != patternTile.getPaint() && patternTile.getMark() != PaintType.EMPTY){
191+
for (MapInfo patternTile : rc.senseNearbyMapInfos(targetLoc, 8)) {
192+
if (patternTile.getMark() != patternTile.getPaint() && patternTile.getMark() != PaintType.EMPTY) {
133193
boolean useSecondaryColor = patternTile.getMark() == PaintType.ALLY_SECONDARY;
134194
if (rc.canAttack(patternTile.getMapLocation()))
135195
rc.attack(patternTile.getMapLocation(), useSecondaryColor);
136196
}
137197
}
138198
// Complete the ruin if we can.
139-
if (rc.canCompleteTowerPattern(UnitType.LEVEL_ONE_PAINT_TOWER, targetLoc)){
199+
if (rc.canCompleteTowerPattern(UnitType.LEVEL_ONE_PAINT_TOWER, targetLoc)) {
140200
rc.completeTowerPattern(UnitType.LEVEL_ONE_PAINT_TOWER, targetLoc);
141201
rc.setTimelineMarker("Tower built", 0, 255, 0);
142202
System.out.println("Built a tower at " + targetLoc + "!");
@@ -146,56 +206,56 @@ public static void runSoldier(RobotController rc) throws GameActionException{
146206
// Move and attack randomly if no objective.
147207
Direction dir = directions[rng.nextInt(directions.length)];
148208
MapLocation nextLoc = rc.getLocation().add(dir);
149-
if (rc.canMove(dir)){
209+
if (rc.canMove(dir)) {
150210
rc.move(dir);
151211
}
152212
// Try to paint beneath us as we walk to avoid paint penalties.
153213
// Avoiding wasting paint by re-painting our own tiles.
154214
MapInfo currentTile = rc.senseMapInfo(rc.getLocation());
155-
if (!currentTile.getPaint().isAlly() && rc.canAttack(rc.getLocation())){
215+
if (!currentTile.getPaint().isAlly() && rc.canAttack(rc.getLocation())) {
156216
rc.attack(rc.getLocation());
157217
}
158218
}
159219

160-
161220
/**
162221
* Run a single turn for a Mopper.
163-
* This code is wrapped inside the infinite loop in run(), so it is called once per turn.
222+
* This code is wrapped inside the infinite loop in run(), so it is called once
223+
* per turn.
164224
*/
165-
public static void runMopper(RobotController rc) throws GameActionException{
225+
public static void runMopper(RobotController rc) throws GameActionException {
166226
// Move and attack randomly.
167227
Direction dir = directions[rng.nextInt(directions.length)];
168228
MapLocation nextLoc = rc.getLocation().add(dir);
169-
if (rc.canMove(dir)){
229+
if (rc.canMove(dir)) {
170230
rc.move(dir);
171231
}
172-
if (rc.canMopSwing(dir)){
232+
if (rc.canMopSwing(dir)) {
173233
rc.mopSwing(dir);
174234
System.out.println("Mop Swing! Booyah!");
175-
}
176-
else if (rc.canAttack(nextLoc)){
235+
} else if (rc.canAttack(nextLoc)) {
177236
rc.attack(nextLoc);
178237
}
179-
// We can also move our code into different methods or classes to better organize it!
180-
updateEnemyRobots(rc);
238+
// We can also move our code into different methods or classes to better
239+
// organize it!
240+
updateEnemyRobots(rc);
181241
}
182242

183-
public static void updateEnemyRobots(RobotController rc) throws GameActionException{
184-
// Sensing methods can be passed in a radius of -1 to automatically
243+
public static void updateEnemyRobots(RobotController rc) throws GameActionException {
244+
// Sensing methods can be passed in a radius of -1 to automatically
185245
// use the largest possible value.
186246
RobotInfo[] enemyRobots = rc.senseNearbyRobots(-1, rc.getTeam().opponent());
187-
if (enemyRobots.length != 0){
247+
if (enemyRobots.length != 0) {
188248
rc.setIndicatorString("There are nearby enemy robots! Scary!");
189249
// Save an array of locations with enemy robots in them for possible future use.
190250
MapLocation[] enemyLocations = new MapLocation[enemyRobots.length];
191-
for (int i = 0; i < enemyRobots.length; i++){
251+
for (int i = 0; i < enemyRobots.length; i++) {
192252
enemyLocations[i] = enemyRobots[i].getLocation();
193253
}
194254
RobotInfo[] allyRobots = rc.senseNearbyRobots(-1, rc.getTeam());
195255
// Occasionally try to tell nearby allies how many enemy robots we see.
196-
if (rc.getRoundNum() % 20 == 0){
197-
for (RobotInfo ally : allyRobots){
198-
if (rc.canSendMessage(ally.location, enemyRobots.length)){
256+
if (rc.getRoundNum() % 20 == 0) {
257+
for (RobotInfo ally : allyRobots) {
258+
if (rc.canSendMessage(ally.location, enemyRobots.length)) {
199259
rc.sendMessage(ally.location, enemyRobots.length);
200260
}
201261
}

0 commit comments

Comments
 (0)