endgame :: Battlefield -> Bool
endgame (Battlefield {attackers=a, defenders=d}) = d == 0 || a < 2
+enemyDestroyed :: Battlefield -> Bool
+enemyDestroyed bf = endgame bf && (defenders bf) == 1
+
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
+-- exercise 4
+successProb :: Battlefield -> Rand StdGen Double
+successProb battlefield = (pure (filter enemyDestroyed) <*> replicateM 1000 (invade battlefield))
+ >>= \xs -> return (fromIntegral (length xs) / 1000)