From 5ef86599194915812d0c3bd44f1c79ff2afe411a Mon Sep 17 00:00:00 2001
From: Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
Date: Mon, 29 Dec 2014 21:17:40 +0530
Subject: [PATCH] hw12: exercise 4

---
 hw12/Risk.hs | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/hw12/Risk.hs b/hw12/Risk.hs
index 24acbc0..31298fa 100644
--- a/hw12/Risk.hs
+++ b/hw12/Risk.hs
@@ -49,8 +49,15 @@ battle (Battlefield {attackers = a, defenders = d}) =
 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)
-- 
2.45.2