Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions airesources/Haskell/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.stack-work/
halite.exe
halite
2 changes: 1 addition & 1 deletion airesources/Haskell/halite-haskell.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ cabal-version: >=1.10

library
hs-source-dirs: lib
exposed-modules: Halite, Halite.Networking, Halite.Types, Halite.Classes
exposed-modules: Halite, Halite.Networking, Halite.Types, Halite.Classes, Halite.Logic
default-language: Haskell2010
build-depends: base >= 4.7 && < 5
, random
Expand Down
1 change: 1 addition & 0 deletions airesources/Haskell/lib/Halite.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ module Halite
import Halite.Networking as M
import Halite.Classes as M
import Halite.Types as M
import Halite.Logic as M
47 changes: 47 additions & 0 deletions airesources/Haskell/lib/Halite/Logic.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module Halite.Logic
( toLocation
, distance
, angle
, moveToLocation
, site
) where

import Halite.Types

toLocation :: Map -> (Int, Int) -> Location
toLocation (Map width height _) (x, y) =
Location (mod x width) (mod y height)

distance :: Map -> Location -> Location -> Int
distance (Map width height _) (Location x1 y1) (Location x2 y2) =
(x2 - x1) `mod` width + (y2 - y1) `mod` height

angle :: Map -> Location -> Location -> Double
angle (Map width height _) (Location x1 y1) (Location x2 y2) =
atan2 (fromIntegral $ foo dx width) (fromIntegral $ foo dy height)
where
dx = x2 - x1
dy = y2 - y1
foo a b
| a > b - a = a - b
| -a > b + a = a + b
| otherwise = a

moveToLocation :: Map -> Move -> Location
moveToLocation _ (Move location Still) = location
moveToLocation (Map width height _) (Move (Location x y) dir) = case dir of
North -> if y == 0
then Location x (height -1)
else Location x (y +1)
South -> if y == height -1
then Location x 0
else Location x (y -1)
West -> if x == 0
then Location (width -1) y
else Location (x +1) y
East -> if x == width -1
then Location 0 y
else Location (x +1) y

site :: Map -> Location -> Site
site (Map _ _ sites) (Location x y) = sites !! y !! x
5 changes: 3 additions & 2 deletions airesources/Haskell/lib/Halite/Networking.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ communicate
:: Monad m
=> String
-> (ID -> Map -> m [Move])
-> a -> (m [Move] -> a -> ([Move], a))
-> (m [Move] -> a -> ([Move], a))
-> a
-> IO ()
communicate name algorithm input runMonad = do
communicate name algorithm runMonad input = do
(myID, gameMap) <- getInit
sendInit name
loop (algorithm myID) gameMap input runMonad
Expand Down
6 changes: 3 additions & 3 deletions airesources/Haskell/lib/Halite/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ module Halite.Types
, Site(..)
, Move(..)
, ID
, Effects(..)
, Test(..)
, Effects(runEffects) -- you don't want to export constructor
, Test(runTest)
) where


Expand Down Expand Up @@ -64,7 +64,7 @@ instance Monad Effects where
in runEffects (k a) s'

instance RandomReader Effects where
rand i = Effects $ randomR (0, i)
rand i = Effects $ randomR (0, i-1)

data Test a = Test {runTest :: [Int] -> (a, [Int])}
deriving (Functor)
Expand Down
25 changes: 6 additions & 19 deletions airesources/Haskell/src/MyBot.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,19 @@ import System.Random (getStdGen)
import Halite

main :: IO ()
main = do
input <- getStdGen
communicate name algorithm input runEffects
main = getStdGen >>= communicate name algorithm runEffects

name = "Awesome Haskell Bot"

algorithm :: RandomReader m => ID -> Map -> m [Move]
algorithm me (Map width height sites) =
randomMoves
locations $
fmap (ownedBy me) (concat sites)
where
xs = concat $ replicate height [0 .. width - 1]
ys = concatMap (replicate width) [0 .. height - 1]
locations = zipWith Location xs ys
algorithm me g@(Map width height sites) =
randomMoves $ filter ((== me) . siteOwner) (concat sites)

randomMoves :: RandomReader m => [Location] -> [Bool] -> m [Move]
randomMoves l = traverse randMove . filter' l
randomMoves :: RandomReader m => [Site] -> m [Move]
randomMoves = traverse $ randMove . siteLocation
where
filter' :: [a] -> [Bool] -> [a]
filter' [] [] = []
filter' (a:as) (True:bs) = a : filter' as bs
filter' (_:as) (False:bs) = filter' as bs
randMove :: RandomReader r => Location -> r Move
randMove location = do
direction <- toEnum <$> rand 4
direction <- toEnum <$> rand 5
return $ Move location direction

ownedBy :: ID -> Site -> Bool
Expand Down
27 changes: 7 additions & 20 deletions airesources/Haskell/src/RandomBot.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,19 @@ import System.Random (getStdGen)
import Halite

main :: IO ()
main = do
input <- getStdGen
communicate name algorithm input runEffects
main = getStdGen >>= communicate name algorithm runEffects

name = "Awesome Haskell Bot"
name = "Random Haskell Bot"

algorithm :: RandomReader m => ID -> Map -> m [Move]
algorithm me (Map width height sites) =
randomMoves
locations $
fmap (ownedBy me) (concat sites)
where
xs = concat $ replicate height [0 .. width - 1]
ys = concatMap (replicate width) [0 .. height - 1]
locations = zipWith Location xs ys
algorithm me g@(Map width height sites) =
randomMoves $ filter ((== me) . siteOwner) (concat sites)

randomMoves :: RandomReader m => [Location] -> [Bool] -> m [Move]
randomMoves l = traverse randMove . filter' l
randomMoves :: RandomReader m => [Site] -> m [Move]
randomMoves = traverse $ randMove . siteLocation
where
filter' :: [a] -> [Bool] -> [a]
filter' [] [] = []
filter' (a:as) (True:bs) = a : filter' as bs
filter' (_:as) (False:bs) = filter' as bs
randMove :: RandomReader r => Location -> r Move
randMove location = do
direction <- toEnum <$> rand 4
direction <- toEnum <$> rand 5
return $ Move location direction

ownedBy :: ID -> Site -> Bool
Expand Down