-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpandBot.scala
59 lines (52 loc) · 1.66 KB
/
ExpandBot.scala
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
object ExpandBot {
def doTurn(pw:PlanetWars) {
// (1) If we current have a fleet in flight, just do nothing.
if (pw.myFleets.size >= 1) {
return
}
// (2) Find my strongest planet.
val source = pw.myPlanets.reduceLeft((p1, p2) => if (p1.numShips > p2.numShips) p1 else p2)
// (3) Find the weakest enemy or neutral planet.
val dest = pw.notMyPlanets.reduceLeft {
(p1, p2) =>
val s1 = 1 / (1 + p1.numShips)
val s2 = 1 / (1 + p2.numShips)
if (s1 > s2) p1 else p2
}
// (4) Send half the ships from my strongest planet to the weakest
// planet that I do not own.
if (source != null && dest != null) {
val numShips = source.numShips / 2
pw.IssueOrder(source, dest, numShips)
}
}
def main(args: Array[String]) {
var line = ""
var message = ""
try {
var c:Char = System.in.read().toChar
while (c >= 0) {
if (c == '\n') {
if (line.equals("go")) {
val pw = new PlanetWars(message)
doTurn(pw)
pw.FinishTurn()
message = ""
}
else {
message += line + "\n"
}
line = ""
}
else {
line += c
}
c = System.in.read().toChar
}
}
catch {
// Owned.
case e => println(e)
}
}
}