forked from jwkvam/ants-haskell
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MyBot.hs
42 lines (35 loc) · 1.23 KB
/
MyBot.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
module Main where
import Data.List
import Data.Maybe (mapMaybe)
import System.IO
import Ants
-- | Picks the first "passable" order in a list
-- returns Nothing if no such order exists
tryOrder :: World -> [Order] -> Maybe Order
tryOrder w = find (passable w)
-- | Generates orders for an Ant in all directions
generateOrders :: Ant -> [Order]
generateOrders a = map (Order a) [North .. West]
{- |
- Implement this function to create orders.
- It uses the IO Monad so algorithms can call timeRemaining.
-
- GameParams data holds values that are constant throughout the game
- GameState holds data that changes between each turn
- for each see Ants module for more information
-}
doTurn :: GameParams -> GameState -> IO [Order]
doTurn gp gs = do
-- generate orders for all ants belonging to me
let generatedOrders = map generateOrders $ myAnts $ ants gs
-- for each ant take the first "passable" order, if one exists
orders = mapMaybe (tryOrder (world gs)) generatedOrders
-- this shows how to check the remaining time
elapsedTime <- timeRemaining gs
hPutStrLn stderr $ show elapsedTime
-- wrap list of orders back into a monad
return orders
-- | This runs the game
main :: IO ()
main = game doTurn
-- vim: set expandtab: