-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.hs
More file actions
66 lines (52 loc) · 1.52 KB
/
Copy pathGame.hs
File metadata and controls
66 lines (52 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
{-
-- EPITECH PROJECT, 2024
-- ppool3
-- File description:
-- Game
-}
import Data.Maybe (isJust)
data Item = Sword | Bow | MagicWand
deriving (Eq)
instance Show Item where
show Sword = "sword"
show Bow = "bow"
show MagicWand = "magic wand"
data Mob = Mummy | Skeleton Item | Witch (Maybe Item)
deriving (Eq)
instance Show Mob where
show Mummy = "mummy"
show (Skeleton Bow) = "doomed archer"
show (Skeleton Sword) = "dead knight"
show (Skeleton item) = "skeleton holding a " ++ show item
show (Witch Nothing) = "witch"
show (Witch (Just MagicWand)) = "sorceress"
show (Witch (Just item)) = "witch holding a " ++ show item
class HasItem a where
getItem :: a -> Maybe Item
hasItem :: a -> Bool
hasItem = isJust . getItem
instance HasItem Mob where
getItem (Skeleton item) = Just item
getItem (Witch item) = item
getItem Mummy = Nothing
createMummy :: Mob
createMummy = Mummy
createArcher :: Mob
createArcher = Skeleton Bow
createKnight :: Mob
createKnight = Skeleton Sword
createWitch :: Mob
createWitch = Witch Nothing
createSorceress :: Mob
createSorceress = Witch (Just MagicWand)
create :: String -> Maybe Mob
create "mummy" = Just createMummy
create "doomed archer" = Just createArcher
create "dead knight" = Just createKnight
create "witch" = Just createWitch
create "sorceress" = Just createSorceress
create _ = Nothing
equip :: Item -> Mob -> Maybe Mob
equip item (Skeleton _) = Just $ Skeleton item
equip item (Witch _) = Just $ Witch (Just item)
equip _ _ = Nothing