Advertisement
Yurry

Simple NP-complete analyzer of game passability

Dec 27th, 2012
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Data.Map
  2. import Control.Monad
  3.  
  4. type Item = String
  5. type Inventory = [Item]
  6. addToInv src dst = dst ++ [i | i <- src, i `notElem` dst]
  7.  
  8. type Location = (String, Inventory)
  9. locInv (_, inv) = inv
  10. makeLocation name inv = (name, inv)
  11. isFinal (name, _) = name == "End"
  12.  
  13. type Requirement = Maybe Item
  14. data GameState = GS {gsInv :: Inventory, gsLocation :: Location}
  15.  
  16. --type GameMap = M.Map (Location, Location) Requirement
  17. type GameMap = Map Location [(Location, Requirement)]
  18.  
  19. torch = "Torch"
  20. key = "Key"
  21.  
  22. start = makeLocation "Start" []
  23. end = makeLocation "End" []
  24. room = makeLocation "Dark room" [key]
  25. chest = makeLocation "A huge chest" [torch]
  26.  
  27. testMap = Data.Map.fromList
  28.      [(start, [(chest, Nothing), (room, Just torch)]),
  29.       (chest, [(start, Nothing)]),
  30.       (room, [(start, Nothing), (end, Just key)])]
  31.  
  32. passable :: [GameState] -> Bool
  33. passable states | any (isFinal . gsLocation) states = True
  34.                 | otherwise = passable $ do
  35.                     GS inv loc <- states
  36.                     let newInv = addToInv (locInv loc) inv
  37.                     (newLoc, req) <- testMap ! loc
  38.                     guard $ canPass req newInv
  39.                     return $ GS newInv newLoc
  40.     where canPass Nothing _ = True
  41.           canPass (Just item) inv = item `elem` inv
  42.  
  43. main = print $ passable [GS [] start]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement