-
Notifications
You must be signed in to change notification settings - Fork 3
/
LookaheadBot.java
377 lines (298 loc) · 10.5 KB
/
LookaheadBot.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.*;
/** Another smarter kind of bot, which implements a minimax algorithm with look-ahead of two turns.
* It simulates the opponent using the BullyBot strategy and simulates the possible outcomes for any
* choice of source and destination planets in the attack. The simulated outcome states are ranked by
* the evaluation function, which returns the most promising one.
*
* Try to improve this bot. For example, you can try to answer some of this questions.
* Can you come up with smarter heuristics/scores for the evaluation function?
* What happens if you run this bot against your bot from week1?
* How can you change this bot to beat your week1 bot?
* Can you extend the bot to look ahead more than two turns? How many turns do you want to look ahead?
* Is there a smart way to make this more efficient?
*/
public class LookaheadBot {
public static void DoTurn(PlanetWars pw) {
double score = Double.MIN_VALUE;
Planet source = null;
Planet dest = null;
// We try to simulate each possible action and its outcome after two turns
// considering each of my planets as a possible source
// and each enemy planet as a possible destination
for (Planet myPlanet: pw.MyPlanets()){
//avoid planets with only one ship
if (myPlanet.NumShips() <= 1)
continue;
for (Planet notMyPlanet: pw.NotMyPlanets()){
// Create simulation environment - need to create one for each simulation
SimulatedPlanetWars simpw = createSimulation(pw);
// (1) simulate my turn with the current couple of source and destination
simpw.simulateAttack(myPlanet, notMyPlanet);
// (2) simulate the growth of ships that happens in each turn
simpw.simulateGrowth();
// (3) simulate the opponent's turn, assuming that the opponent is the BullyBot
// here you can add other opponents
simpw.simulateBullyBotAttack();
// (4) simulate the growth of ships that happens in each turn
simpw.simulateGrowth();
// (5) evaluate how the current simulated state is
// here you can change how a state is evaluated as good
double scoreMax = evaluateState(simpw);
// (6) find the planet with the maximum evaluated score
// this is the most promising future state
if (scoreMax > score) {
score = scoreMax;
source = myPlanet;
dest = notMyPlanet;
}
}
}
// Attack using the source and destinations that lead to the most promising state in the simulation
if (source != null && dest != null) {
pw.IssueOrder(source, dest);
}
}
/**
* This function evaluates how promising a simulated state is.
* You can change it to anything that makes sense, using combinations
* of number of planets, ships or growth rate.
* @param SimulatedPlanetWars pw
* @return score of the final state of the simulation
*/
public static double evaluateState(SimulatedPlanetWars pw){
// CHANGE HERE
double enemyShips = 1.0;
double myShips = 1.0;
for (Planet planet: pw.EnemyPlanets()){
enemyShips += planet.NumShips();
}
for (Planet planet: pw.MyPlanets()){
myShips += planet.NumShips();
}
return myShips/enemyShips;
}
// don't change this
public static void main(String[] args) {
String line = "";
String message = "";
int c;
try {
while ((c = System.in.read()) >= 0) {
switch (c) {
case '\n':
if (line.equals("go")) {
PlanetWars pw = new PlanetWars(message);
DoTurn(pw);
pw.FinishTurn();
message = "";
} else {
message += line + "\n";
}
line = "";
break;
default:
line += (char) c;
break;
}
}
} catch (Exception e) {
StringWriter writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
String stackTrace = writer.toString();
System.err.println(stackTrace);
System.exit(1); //just stop now. we've got a problem
}
}
/**
* Create the simulation environment. Returns a SimulatedPlanetWars instance.
* Call every time you want a new simulation environment.
* @param The original PlanetWars object
* @return SimulatedPlanetWars instance on which to simulate your attacks. Create a new one everytime you want to try alternative simulations.
*/
public static SimulatedPlanetWars createSimulation(PlanetWars pw){
return dummyBot.new SimulatedPlanetWars(pw);
}
/**
* Static LookaheadBot, used only to access SimulatedPlanetWars (DON'T CHANGE)
*/
static LookaheadBot dummyBot = new LookaheadBot();
/**
* Class which provide the simulation environment, has same interface as PlanetWars
* (except for Fleets, that are not used).
*
*/
public class SimulatedPlanetWars{
List<Planet> planets = new ArrayList<Planet>();
public SimulatedPlanetWars(PlanetWars pw) {
for (Planet planet: pw.Planets()){
planets.add(planet);
}
}
public void simulateGrowth() {
for (Planet p: planets){
if(p.Owner() == 0)
continue;
Planet newp = new Planet(p.PlanetID(), p.Owner(), p.NumShips()+p.GrowthRate() ,
p.GrowthRate(), p.X(), p.Y());
planets.set(p.PlanetID(), newp);
}
}
public void simulateAttack( int player, Planet source, Planet dest){
if (source.Owner() != player){
return;
}
// Simulate attack
if (source != null && dest != null) {
Planet newSource = new Planet(source.PlanetID(), source.Owner(), source.NumShips()/2 ,
source.GrowthRate(), source.X(), source.Y());
Planet newDest = new Planet(dest.PlanetID(), dest.Owner(), Math.abs(dest.NumShips()-source.NumShips()/2 ),
dest.GrowthRate(), dest.X(), dest.Y());
if(dest.NumShips()< source.NumShips()/2){
//change owner
newDest.Owner(player);
}
planets.set(source.PlanetID(), newSource);
planets.set(dest.PlanetID(), newDest);
}
}
public void simulateAttack( Planet source, Planet dest){
simulateAttack(1, source, dest);
}
public void simulateBullyBotAttack(){
Planet source = null;
Planet dest = null;
// (1) Apply your strategy
double sourceScore = Double.MIN_VALUE;
double destScore = Double.MAX_VALUE;
for (Planet planet : planets) {
if(planet.Owner() == 2) {// skip planets with only one ship
if (planet.NumShips() <= 1)
continue;
//This score is one way of defining how 'good' my planet is.
double scoreMax = (double) planet.NumShips();
if (scoreMax > sourceScore) {
//we want to maximize the score, so store the planet with the best score
sourceScore = scoreMax;
source = planet;
}
}
// (2) Find the weakest enemy or neutral planet.
if(planet.Owner() != 2){
double scoreMin = (double) (planet.NumShips());
//if you want to debug how the score is computed, decomment the System.err.instructions
// System.err.println("Planet: " +notMyPlanet.PlanetID()+ " Score: "+ score);
// System.err.flush();
if (scoreMin < destScore) {
//The way the score is defined, is that the weaker a planet is, the higher the score.
//So again, we want to select the planet with the best score
destScore = scoreMin;
dest = planet;
}
}
}
// (3) Simulate attack
if (source != null && dest != null) {
simulateAttack(2, source, dest);
}
}
public List<Planet> Planets(){
return planets;
}
// Returns the number of planets. Planets are numbered starting with 0.
public int NumPlanets() {
return planets.size();
}
// Returns the planet with the given planet_id. There are NumPlanets()
// planets. They are numbered starting at 0.
public Planet GetPlanet(int planetID) {
return planets.get(planetID);
}
// Return a list of all the planets owned by the current player. By
// convention, the current player is always player number 1.
public List<Planet> MyPlanets() {
List<Planet> r = new ArrayList<Planet>();
for (Planet p : planets) {
if (p.Owner() == 1) {
r.add(p);
}
}
return r;
}
// Return a list of all neutral planets.
public List<Planet> NeutralPlanets() {
List<Planet> r = new ArrayList<Planet>();
for (Planet p : planets) {
if (p.Owner() == 0) {
r.add(p);
}
}
return r;
}
// Return a list of all the planets owned by rival players. This excludes
// planets owned by the current player, as well as neutral planets.
public List<Planet> EnemyPlanets() {
List<Planet> r = new ArrayList<Planet>();
for (Planet p : planets) {
if (p.Owner() >= 2) {
r.add(p);
}
}
return r;
}
// Return a list of all the planets that are not owned by the current
// player. This includes all enemy planets and neutral planets.
public List<Planet> NotMyPlanets() {
List<Planet> r = new ArrayList<Planet>();
for (Planet p : planets) {
if (p.Owner() != 1) {
r.add(p);
}
}
return r;
}
// Returns the distance between two planets, rounded up to the next highest
// integer. This is the number of discrete time steps it takes to get
// between the two planets.
public int Distance(int sourcePlanet, int destinationPlanet) {
Planet source = planets.get(sourcePlanet);
Planet destination = planets.get(destinationPlanet);
double dx = source.X() - destination.X();
double dy = source.Y() - destination.Y();
return (int) Math.ceil(Math.sqrt(dx * dx + dy * dy));
}
// If the game is not yet over (ie: at least two players have planets or
// fleets remaining), returns -1. If the game is over (ie: only one player
// is left) then that player's number is returned. If there are no
// remaining players, then the game is a draw and 0 is returned.
public int Winner() {
Set<Integer> remainingPlayers = new TreeSet<Integer>();
for (Planet p : planets) {
remainingPlayers.add(p.Owner());
}
switch (remainingPlayers.size()) {
case 0:
return 0;
case 1:
return ((Integer) remainingPlayers.toArray()[0]).intValue();
default:
return -1;
}
}
// Returns the number of ships that the current player has, either located
// on planets or in flight.
public int NumShips(int playerID) {
int numShips = 0;
for (Planet p : planets) {
if (p.Owner() == playerID) {
numShips += p.NumShips();
}
}
return numShips;
}
public void IssueOrder(Planet source, Planet dest) {
simulateAttack(source,dest);
}
}
}