From 42b9d46f9d5c18447833c31026c336f4eb4e146c Mon Sep 17 00:00:00 2001
From: Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
Date: Mon, 29 Dec 2014 14:48:09 +0530
Subject: [PATCH] exercise 3

---
 hw12/Risk.hs | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

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
-- 
2.45.2