import Control.Applicative
import Control.Monad.Random
import Control.Monad(replicateM, liftM)
-import Data.List(sort)
+import Data.List(sortBy)
------------------------------------------------------------
-- Die values
data Battlefield = Battlefield { attackers :: Army, defenders :: Army }
--- battle :: Battlefield -> Rand StdGen Battlefield
+newBattlefield :: Battlefield -> [DieValue] -> [DieValue] -> Battlefield
+newBattlefield battlefield rollA rollB =
+ let pairList = zip (sortBy (flip compare) rollA) (sortBy (flip compare) rollB)
+ in
+ foldr (\(a, d) acc -> if a > d then Battlefield (attackers acc) ((defenders acc) - 1)
+ else Battlefield ((attackers acc) - 1) (defenders acc)) battlefield pairList
+
+
+battle :: Battlefield -> Rand StdGen Battlefield
battle (Battlefield {attackers = a, defenders = d}) =
let rolla = replicateM (min (a - 1) 3) die
rolld = replicateM (min d 2) die
- zipped = liftA2 zip rolla rolld
in
-
+ liftA2 (newBattlefield (Battlefield a d)) rolla rolld