-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRobotPlayer.java
243 lines (221 loc) · 6.63 KB
/
RobotPlayer.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package examplefuncsplayer;
//does this change things?
import battlecode.common.*;
import java.util.*;
public class RobotPlayer {
static RobotController rc;
static Team myTeam;
static Team enemyTeam;
static int myRange;
static Random rand;
static Direction[] directions = {Direction.NORTH, Direction.NORTH_EAST, Direction.EAST, Direction.SOUTH_EAST, Direction.SOUTH, Direction.SOUTH_WEST, Direction.WEST, Direction.NORTH_WEST};
public static void run(RobotController tomatojuice) {
rc = tomatojuice;
rand = new Random(rc.getID());
myRange = rc.getType().attackRadiusSquared;
MapLocation enemyLoc = rc.senseEnemyHQLocation();
Direction lastDirection = null;
myTeam = rc.getTeam();
enemyTeam = myTeam.opponent();
RobotInfo[] myRobots;
while(true) {
try {
rc.setIndicatorString(0, "This is an indicator string.");
rc.setIndicatorString(1, "I am a " + rc.getType());
} catch (Exception e) {
System.out.println("Unexpected exception");
e.printStackTrace();
}
if (rc.getType() == RobotType.HQ) {
try {
int fate = rand.nextInt(10000);
myRobots = rc.senseNearbyRobots(999999, myTeam);
int numSoldiers = 0;
int numBashers = 0;
int numBeavers = 0;
int numBarracks = 0;
for (RobotInfo r : myRobots) {
RobotType type = r.type;
if (type == RobotType.SOLDIER) {
numSoldiers++;
} else if (type == RobotType.BASHER) {
numBashers++;
} else if (type == RobotType.BEAVER) {
numBeavers++;
} else if (type == RobotType.BARRACKS) {
numBarracks++;
}
}
rc.broadcast(0, numBeavers);
rc.broadcast(1, numSoldiers);
rc.broadcast(2, numBashers);
rc.broadcast(100, numBarracks);
if (rc.isWeaponReady()) {
attackSomething();
}
if (rc.isCoreReady() && rc.getTeamOre() >= 100 && fate < Math.pow(1.2,12-numBeavers)*10000) {
trySpawn(directions[rand.nextInt(8)], RobotType.BEAVER);
}
} catch (Exception e) {
System.out.println("HQ Exception");
e.printStackTrace();
}
}
if (rc.getType() == RobotType.TOWER) {
try {
if (rc.isWeaponReady()) {
attackSomething();
}
} catch (Exception e) {
System.out.println("Tower Exception");
e.printStackTrace();
}
}
if (rc.getType() == RobotType.BASHER) {
try {
RobotInfo[] adjacentEnemies = rc.senseNearbyRobots(2, enemyTeam);
// BASHERs attack automatically, so let's just move around mostly randomly
if (rc.isCoreReady()) {
int fate = rand.nextInt(1000);
if (fate < 800) {
tryMove(directions[rand.nextInt(8)]);
} else {
tryMove(rc.getLocation().directionTo(rc.senseEnemyHQLocation()));
}
}
} catch (Exception e) {
System.out.println("Basher Exception");
e.printStackTrace();
}
}
if (rc.getType() == RobotType.SOLDIER) {
try {
if (rc.isWeaponReady()) {
attackSomething();
}
if (rc.isCoreReady()) {
int fate = rand.nextInt(1000);
if (fate < 800) {
tryMove(directions[rand.nextInt(8)]);
} else {
tryMove(rc.getLocation().directionTo(rc.senseEnemyHQLocation()));
}
}
} catch (Exception e) {
System.out.println("Soldier Exception");
e.printStackTrace();
}
}
if (rc.getType() == RobotType.BEAVER) {
try {
if (rc.isWeaponReady()) {
attackSomething();
}
if (rc.isCoreReady()) {
int fate = rand.nextInt(1000);
if (fate < 8 && rc.getTeamOre() >= 300) {
tryBuild(directions[rand.nextInt(8)],RobotType.BARRACKS);
} else if (fate < 600) {
rc.mine();
} else if (fate < 900) {
tryMove(directions[rand.nextInt(8)]);
} else {
tryMove(rc.senseHQLocation().directionTo(rc.getLocation()));
}
}
} catch (Exception e) {
System.out.println("Beaver Exception");
e.printStackTrace();
}
}
if (rc.getType() == RobotType.BARRACKS) {
try {
int fate = rand.nextInt(10000);
// get information broadcasted by the HQ
int numBeavers = rc.readBroadcast(0);
int numSoldiers = rc.readBroadcast(1);
int numBashers = rc.readBroadcast(2);
if (rc.isCoreReady() && rc.getTeamOre() >= 60 && fate < Math.pow(1.2,15-numSoldiers-numBashers+numBeavers)*10000) {
if (rc.getTeamOre() > 80 && fate % 2 == 0) {
trySpawn(directions[rand.nextInt(8)],RobotType.BASHER);
} else {
trySpawn(directions[rand.nextInt(8)],RobotType.SOLDIER);
}
}
} catch (Exception e) {
System.out.println("Barracks Exception");
e.printStackTrace();
}
}
rc.yield();
}
}
// This method will attack an enemy in sight, if there is one
static void attackSomething() throws GameActionException {
RobotInfo[] enemies = rc.senseNearbyRobots(myRange, enemyTeam);
if (enemies.length > 0) {
rc.attackLocation(enemies[0].location);
}
}
// This method will attempt to move in Direction d (or as close to it as possible)
static void tryMove(Direction d) throws GameActionException {
int offsetIndex = 0;
int[] offsets = {0,1,-1,2,-2};
int dirint = directionToInt(d);
boolean blocked = false;
while (offsetIndex < 5 && !rc.canMove(directions[(dirint+offsets[offsetIndex]+8)%8])) {
offsetIndex++;
}
if (offsetIndex < 5) {
rc.move(directions[(dirint+offsets[offsetIndex]+8)%8]);
}
}
// This method will attempt to spawn in the given direction (or as close to it as possible)
static void trySpawn(Direction d, RobotType type) throws GameActionException {
int offsetIndex = 0;
int[] offsets = {0,1,-1,2,-2,3,-3,4};
int dirint = directionToInt(d);
boolean blocked = false;
while (offsetIndex < 8 && !rc.canSpawn(directions[(dirint+offsets[offsetIndex]+8)%8], type)) {
offsetIndex++;
}
if (offsetIndex < 8) {
rc.spawn(directions[(dirint+offsets[offsetIndex]+8)%8], type);
}
}
// This method will attempt to build in the given direction (or as close to it as possible)
static void tryBuild(Direction d, RobotType type) throws GameActionException {
int offsetIndex = 0;
int[] offsets = {0,1,-1,2,-2,3,-3,4};
int dirint = directionToInt(d);
boolean blocked = false;
while (offsetIndex < 8 && !rc.canMove(directions[(dirint+offsets[offsetIndex]+8)%8])) {
offsetIndex++;
}
if (offsetIndex < 8) {
rc.build(directions[(dirint+offsets[offsetIndex]+8)%8], type);
}
}
static int directionToInt(Direction d) {
switch(d) {
case NORTH:
return 0;
case NORTH_EAST:
return 1;
case EAST:
return 2;
case SOUTH_EAST:
return 3;
case SOUTH:
return 4;
case SOUTH_WEST:
return 5;
case WEST:
return 6;
case NORTH_WEST:
return 7;
default:
return -1;
}
}
}