import Control.Applicative
import Control.Monad.Random
-import Control.Monad(replicateM, liftM)
+import Control.Monad(replicateM, liftM, foldM)
import Data.List(sortBy)
------------------------------------------------------------
rolld = replicateM (min d 2) die
in
liftA2 (newBattlefield (Battlefield a d)) rolla rolld
+
+-- exercise 3.
+endgame :: Battlefield -> Bool
+endgame (Battlefield {attackers=a, defenders=d}) = d == 0 || a < 2
+
+invade :: Battlefield -> Rand StdGen Battlefield
+-- invade battlefield = foldM (\acc f -> f $ acc) battlefield (repeat battle)
+invade battlefield = battle battlefield >>= \bf ->
+ if (endgame bf) then return bf
+ else invade bf