From: Ramakrishnan Muthukrishnan Date: Mon, 29 Dec 2014 09:18:09 +0000 (+0530) Subject: exercise 3 X-Git-Url: https://git.rkrishnan.org/pf/content/en/seg/priv//%22%22.?a=commitdiff_plain;h=42b9d46f9d5c18447833c31026c336f4eb4e146c;p=yorgey.git exercise 3 --- diff --git a/hw12/Risk.hs b/hw12/Risk.hs index 965c7c7..24acbc0 100644 --- a/hw12/Risk.hs +++ b/hw12/Risk.hs @@ -4,7 +4,7 @@ module Risk where import Control.Applicative import Control.Monad.Random -import Control.Monad(replicateM, liftM) +import Control.Monad(replicateM, liftM, foldM) import Data.List(sortBy) ------------------------------------------------------------ @@ -44,3 +44,13 @@ battle (Battlefield {attackers = a, defenders = d}) = 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