@@ -16,40 +16,50 @@ public class RobotPlayer {
16
16
static final Random rng = new Random (6147 );
17
17
18
18
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 ,
27
27
};
28
28
29
29
/**
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!
32
34
*
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.
35
39
**/
36
40
@ SuppressWarnings ("unused" )
37
41
public static void run (RobotController rc ) throws GameActionException {
38
42
39
43
rc .setIndicatorString ("Hello world!" );
40
-
41
44
while (true ) {
42
- turnCount += 1 ;
45
+ turnCount += 1 ;
43
46
44
47
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 ;
50
61
}
51
- }
52
- catch (GameActionException e ) {
62
+ } catch (GameActionException e ) {
53
63
System .out .println ("GameActionException" );
54
64
e .printStackTrace ();
55
65
} catch (Exception e ) {
@@ -58,35 +68,32 @@ public static void run(RobotController rc) throws GameActionException {
58
68
59
69
} finally {
60
70
// 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.
62
73
Clock .yield ();
63
74
}
64
75
}
65
76
}
66
77
67
- public static void runSplasher (RobotController rc ) {
68
- rc .setIndicatorString ("I do nothing." );
69
- }
70
78
71
79
/**
72
80
* 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.
74
83
*/
75
- public static void runTower (RobotController rc ) throws GameActionException {
84
+ public static void runTower (RobotController rc ) throws GameActionException {
76
85
// Pick a direction to build in.
77
86
Direction dir = directions [rng .nextInt (directions .length )];
78
87
MapLocation nextLoc = rc .getLocation ().add (dir );
79
88
// Pick a random robot type to build.
80
89
int robotType = rng .nextInt (3 );
81
- if (robotType == 0 && rc .canBuildRobot (UnitType .SOLDIER , nextLoc )){
90
+ if (robotType == 0 && rc .canBuildRobot (UnitType .SOLDIER , nextLoc )) {
82
91
rc .buildRobot (UnitType .SOLDIER , nextLoc );
83
92
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 )) {
86
94
rc .buildRobot (UnitType .MOPPER , nextLoc );
87
95
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 )) {
90
97
// rc.buildRobot(UnitType.SPLASHER, nextLoc);
91
98
// System.out.println("BUILT A SPLASHER");
92
99
rc .setIndicatorString ("SPLASHER NOT IMPLEMENTED YET" );
@@ -101,42 +108,95 @@ else if (robotType == 2 && rc.canBuildRobot(UnitType.SPLASHER, nextLoc)){
101
108
// TODO: can we attack other bots?
102
109
}
103
110
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
+ }
104
148
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
+ }
105
163
/**
106
164
* 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.
108
167
*/
109
- public static void runSoldier (RobotController rc ) throws GameActionException {
168
+ public static void runSoldier (RobotController rc ) throws GameActionException {
110
169
// Sense information about all visible nearby tiles.
111
170
MapInfo [] nearbyTiles = rc .senseNearbyMapInfos ();
112
171
// Search for a nearby ruin to complete.
113
172
MapInfo curRuin = null ;
114
- for (MapInfo tile : nearbyTiles ){
115
- if (tile .hasRuin ()){
173
+ for (MapInfo tile : nearbyTiles ) {
174
+ if (tile .hasRuin ()) {
116
175
curRuin = tile ;
117
176
}
118
177
}
119
- if (curRuin != null ){
178
+ if (curRuin != null ) {
120
179
MapLocation targetLoc = curRuin .getMapLocation ();
121
180
Direction dir = rc .getLocation ().directionTo (targetLoc );
122
181
if (rc .canMove (dir ))
123
182
rc .move (dir );
124
183
// Mark the pattern we need to draw to build a tower here if we haven't already.
125
184
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 )) {
127
187
rc .markTowerPattern (UnitType .LEVEL_ONE_PAINT_TOWER , targetLoc );
128
188
System .out .println ("Trying to build a tower at " + targetLoc );
129
189
}
130
190
// 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 ) {
133
193
boolean useSecondaryColor = patternTile .getMark () == PaintType .ALLY_SECONDARY ;
134
194
if (rc .canAttack (patternTile .getMapLocation ()))
135
195
rc .attack (patternTile .getMapLocation (), useSecondaryColor );
136
196
}
137
197
}
138
198
// 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 )) {
140
200
rc .completeTowerPattern (UnitType .LEVEL_ONE_PAINT_TOWER , targetLoc );
141
201
rc .setTimelineMarker ("Tower built" , 0 , 255 , 0 );
142
202
System .out .println ("Built a tower at " + targetLoc + "!" );
@@ -146,56 +206,56 @@ public static void runSoldier(RobotController rc) throws GameActionException{
146
206
// Move and attack randomly if no objective.
147
207
Direction dir = directions [rng .nextInt (directions .length )];
148
208
MapLocation nextLoc = rc .getLocation ().add (dir );
149
- if (rc .canMove (dir )){
209
+ if (rc .canMove (dir )) {
150
210
rc .move (dir );
151
211
}
152
212
// Try to paint beneath us as we walk to avoid paint penalties.
153
213
// Avoiding wasting paint by re-painting our own tiles.
154
214
MapInfo currentTile = rc .senseMapInfo (rc .getLocation ());
155
- if (!currentTile .getPaint ().isAlly () && rc .canAttack (rc .getLocation ())){
215
+ if (!currentTile .getPaint ().isAlly () && rc .canAttack (rc .getLocation ())) {
156
216
rc .attack (rc .getLocation ());
157
217
}
158
218
}
159
219
160
-
161
220
/**
162
221
* 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.
164
224
*/
165
- public static void runMopper (RobotController rc ) throws GameActionException {
225
+ public static void runMopper (RobotController rc ) throws GameActionException {
166
226
// Move and attack randomly.
167
227
Direction dir = directions [rng .nextInt (directions .length )];
168
228
MapLocation nextLoc = rc .getLocation ().add (dir );
169
- if (rc .canMove (dir )){
229
+ if (rc .canMove (dir )) {
170
230
rc .move (dir );
171
231
}
172
- if (rc .canMopSwing (dir )){
232
+ if (rc .canMopSwing (dir )) {
173
233
rc .mopSwing (dir );
174
234
System .out .println ("Mop Swing! Booyah!" );
175
- }
176
- else if (rc .canAttack (nextLoc )){
235
+ } else if (rc .canAttack (nextLoc )) {
177
236
rc .attack (nextLoc );
178
237
}
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 );
181
241
}
182
242
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
185
245
// use the largest possible value.
186
246
RobotInfo [] enemyRobots = rc .senseNearbyRobots (-1 , rc .getTeam ().opponent ());
187
- if (enemyRobots .length != 0 ){
247
+ if (enemyRobots .length != 0 ) {
188
248
rc .setIndicatorString ("There are nearby enemy robots! Scary!" );
189
249
// Save an array of locations with enemy robots in them for possible future use.
190
250
MapLocation [] enemyLocations = new MapLocation [enemyRobots .length ];
191
- for (int i = 0 ; i < enemyRobots .length ; i ++){
251
+ for (int i = 0 ; i < enemyRobots .length ; i ++) {
192
252
enemyLocations [i ] = enemyRobots [i ].getLocation ();
193
253
}
194
254
RobotInfo [] allyRobots = rc .senseNearbyRobots (-1 , rc .getTeam ());
195
255
// 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 )) {
199
259
rc .sendMessage (ally .location , enemyRobots .length );
200
260
}
201
261
}
0 commit comments