projects
/
yorgey.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
42b9d46
)
hw12: exercise 4
master
author
Ramakrishnan Muthukrishnan
<ram@rkrishnan.org>
Mon, 29 Dec 2014 15:47:40 +0000
(21:17 +0530)
committer
Ramakrishnan Muthukrishnan
<ram@rkrishnan.org>
Mon, 29 Dec 2014 15:47:40 +0000
(21:17 +0530)
hw12/Risk.hs
patch
|
blob
|
history
diff --git
a/hw12/Risk.hs
b/hw12/Risk.hs
index 24acbc06232be1d41dbc1e95db52967faefcbc83..31298fa51df74d71d0addf55252a24eeafb01f5a 100644
(file)
--- 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
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
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)