Skip to content

Commit

Permalink
Implemented MoveObject
Browse files Browse the repository at this point in the history
  • Loading branch information
MedeaMelana committed Sep 3, 2012
1 parent def3fbd commit a8ca4ce
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
10 changes: 10 additions & 0 deletions Engine.hs
Expand Up @@ -143,8 +143,18 @@ compileEffect (DrawCard rp) = do
case IdList.toList lib of
[] -> players .^ mapEl rp .^ failedCardDraw =: True
(ro, _) : _ -> executeEffect (MoveObject (Library rp, ro) (Hand rp))
compileEffect (MoveObject rObject@(rFromZone, i) rToZone) = do
mObject <- lookupObject rObject
case mObject of
Nothing -> return ()
Just object -> do
compileZoneRef rFromZone ~: IdList.remove i
compileZoneRef rToZone ~: IdList.cons object
compileEffect _ = undefined

lookupObject :: ObjectRef -> Engine (Maybe Object)
lookupObject (rz, i) = IdList.get i <$> gets (compileZoneRef rz)

sortOn :: Ord b => (a -> b) -> [a] -> [a]
sortOn = sortBy . comparing

Expand Down
11 changes: 7 additions & 4 deletions IdList.hs
Expand Up @@ -5,7 +5,7 @@ import qualified Prelude

type Id = Int

data IdList a = IdList { contents :: [(Id, a)], nextId :: Id }
data IdList a = IdList [(Id, a)] Id

empty :: IdList a
empty = IdList [] 0
Expand All @@ -20,14 +20,17 @@ set i x (IdList ixs ni) = IdList (map f ixs) ni
| i == i' = (i, x)
| otherwise = ix'

remove :: Id -> IdList a -> IdList a
remove i = contents (Prelude.filter (\(i', _) -> i /= i'))

cons :: a -> IdList a -> IdList a
cons x (IdList ixs i) = IdList ((i, x) : ixs) (succ i)

reorder :: ([(Id, a)] -> [(Id, a)]) -> IdList a -> IdList a
reorder f (IdList ixs i) = IdList (f ixs) i
contents :: ([(Id, a)] -> [(Id, a)]) -> IdList a -> IdList a
contents f (IdList ixs i) = IdList (f ixs) i

toList :: IdList a -> [(Id, a)]
toList = contents
toList (IdList ixs _) = ixs

filter :: (a -> Bool) -> IdList a -> [(Id, a)]
filter f = Prelude.filter (f . snd) . toList

0 comments on commit a8ca4ce

Please sign in to comment.