diff options
-rw-r--r-- | ws2015/ffp/blaetter/07/FFP_U07_Monaden2.hs | 25 |
1 files changed, 19 insertions, 6 deletions
diff --git a/ws2015/ffp/blaetter/07/FFP_U07_Monaden2.hs b/ws2015/ffp/blaetter/07/FFP_U07_Monaden2.hs index 942fd63..5200851 100644 --- a/ws2015/ffp/blaetter/07/FFP_U07_Monaden2.hs +++ b/ws2015/ffp/blaetter/07/FFP_U07_Monaden2.hs | |||
@@ -280,13 +280,26 @@ memoWinchance :: [Monster] -> [Monster] -> Double | |||
280 | memoWinchance atts defs = runST (newSTRef Map.empty >>= memoWinchance' atts defs) | 280 | memoWinchance atts defs = runST (newSTRef Map.empty >>= memoWinchance' atts defs) |
281 | 281 | ||
282 | memoWinchance' :: forall s. [Monster] -> [Monster] -> STRef s MM -> ST s Double | 282 | memoWinchance' :: forall s. [Monster] -> [Monster] -> STRef s MM -> ST s Double |
283 | memoWinchance' [] _ = 0 | 283 | memoWinchance' [] _ _ = return 0 -- We do this here so we don't clutter our manual with entries for empty lists |
284 | memoWinchance' _ [] = 1 | 284 | memoWinchance' _ [] _ = return 1 |
285 | memoWinchance' atts@(a:att) defs@(d:def) mmRef = (Map.lookup (atts, defs) <$> readSTRef mmRef) >>= fromMaybe winchance . fmap return . traceShowId | 285 | memoWinchance' atts defs mmRef = recall >>= fromMaybeM (winchance >>= memoize) |
286 | where | 286 | where |
287 | memoize x = modifySTRef mmRef (Map.insert (atts, defs) x) | 287 | recall = Map.lookup (atts, defs) <$> readSTRef mmRef |
288 | winchance | 288 | memoize x = modifySTRef mmRef (Map.insert (atts, defs) x) >> return x |
289 | | hitpoints a <= 0 = | 289 | winchance = winchance' atts defs mmRef |
290 | fromMaybeM x Nothing = x | ||
291 | fromMaybeM _ (Just x) = return x | ||
292 | |||
293 | winchance' :: forall s. [Monster] -> [Monster] -> STRef s MM -> ST s Double | ||
294 | winchance' allatts@(a:atts) alldefs@(d:defs) mmRef | ||
295 | | hitpoints a <= 0 = memoWinchance' atts alldefs mmRef | ||
296 | | hitpoints d <= 0 = memoWinchance' allatts defs mmRef | ||
297 | | otherwise = do | ||
298 | let attchance = nextHitchance dice (strength a) (strength d) | ||
299 | defchance = 1 - attchance | ||
300 | atthit <- memoWinchance' allatts (takeHit d:defs) mmRef | ||
301 | defhit <- memoWinchance' (takeHit a:atts) alldefs mmRef | ||
302 | return $ (attchance * atthit) + (defchance * defhit) | ||
290 | 303 | ||
291 | -- | 304 | -- |
292 | -- Simples Beispiel zur Verwendung der ST-Monade: | 305 | -- Simples Beispiel zur Verwendung der ST-Monade: |