-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBullyBot.java
More file actions
83 lines (76 loc) · 2.27 KB
/
BullyBot.java
File metadata and controls
83 lines (76 loc) · 2.27 KB
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
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.*;
/* A bit smarter kind of bot, who searches for its strongest planet and then attacks the weakest planet.
The score is computed based on the number of ships.
*/
public class BullyBot {
public static void DoTurn(PlanetWars pw) {
// (1) Find my strongest planet.
Planet source = null;
double sourceScore = Double.MIN_VALUE;
for (Planet myPlanet : pw.MyPlanets()) {
// skip planets with only one ship
if (myPlanet.NumShips() <= 1)
continue;
//This score is one way of defining how 'good' my planet is.
double score = (double) myPlanet.NumShips();
if (score > sourceScore) {
//we want to maximize the score, so store the planet with the best score
sourceScore = score;
source = myPlanet;
}
}
// (2) Find the weakest enemy or neutral planet.
Planet dest = null;
double destScore = Double.MAX_VALUE;
for (Planet notMyPlanet : pw.NotMyPlanets()) {
//This score is one way of defining how 'good' the planet is.
// You can change it to something different and experiment with it.
double score = (double) (notMyPlanet.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 (score < destScore) {
//We want to select the planet with the lowest score
destScore = score;
dest = notMyPlanet;
}
}
// (3) Attack!
if (source != null && dest != null) {
pw.IssueOrder(source, dest);
}
}
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
}
}
}