/
DualBot.hs
80 lines (61 loc) · 2.52 KB
/
DualBot.hs
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
-- | Example bot using the PlanetWars module
--
module Main where
import Data.List (maximumBy, partition)
import qualified Data.IntMap as IM
import Data.Ord (comparing)
import PlanetWars
doTurn :: GameState -- ^ Game state
-> [Order] -- ^ Blocks forever
doTurn state = if length myFleets >= numFleets || null myPlanets
|| null candidates
-- Do not send anymore out
then []
else [Order (planetId strongest) (planetId weakest) ships]
where
-- Find my fleets
(myFleets, enemyFleets) = partition isAllied $ gameStateFleets state
-- Separate out my planets
(myPlanets, notMyPlanets) = partition isAllied $ IM.elems $ gameStatePlanets state
enemyPlanets = filter isHostile notMyPlanets
-- Common sub-expressions
sumBy f = sum . map f
fleetsShips = sumBy fleetShips
planetsShips = sumBy planetShips
-- Count my ships and opposing ships
myShips = fleetsShips myFleets + planetsShips myPlanets
enemyShips = fleetsShips enemyFleets + planetsShips enemyPlanets
-- Common sub-expression
planetsProduction = sumBy planetGrowthRate
-- Sum my production and opposing production
myProduction = planetsProduction myPlanets
enemyProduction = planetsProduction enemyPlanets
-- Determine fleet count, and attack stance
(numFleets, attackMode) = if myShips > enemyShips
then if winningProduction
then (1, True)
else (3, False)
else if winningProduction
then (1, False)
else (5, False)
where winningProduction = myProduction > enemyProduction
-- Conversion function
intToDouble :: Int -> Double
intToDouble = fromInteger . toInteger
-- Sub-expressions
growthFactor p = intToDouble $ 1 + planetGrowthRate p
shipsFactor p = intToDouble $ planetShips p
-- Value of my planets
myValue p = shipsFactor p / growthFactor p
-- Value of other planets
notMyValue p = growthFactor p / shipsFactor p
-- Find my strongest planet
strongest = maximumBy (comparing myValue) myPlanets
-- Determine where to sent fleets
candidates = if attackMode then enemyPlanets else notMyPlanets
-- Find the weakest candidate planet
weakest = maximumBy (comparing notMyValue) candidates
-- Send half the planet's ships
ships = planetShips strongest `div` 2
main :: IO ()
main = bot doTurn