Repository navigation
Expand file tree
/
Copy pathBase.hs
More file actions
104 lines (82 loc) · 2.82 KB
/
Copy pathBase.hs
File metadata and controls
104 lines (82 loc) · 2.82 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
module Base where
import Data.List
-- # Core types
-- ## Game State
-- The game state has a player and a room (the current room)
data GameState = GS {player :: Player, room :: Room}
-- The `Next` type is used to represent what happens next in a game
-- either things stay the `Same` or `Progress` is made to a new game state
data Next a
= Same String -- Message about why things are staying the same
| Progress String a -- Success message and next thing `a`
-- ## Player
-- Has a name a list of items
data Player = Player
{ playerName :: String,
inventory :: [Item]
}
-- ## Room (where all the action happens)
data Room = Room
{ name :: String,
description :: String,
isWinRoom :: Bool,
-- | whether an item is required to enter this room or not
requires :: Maybe Item,
-- | Association list of items and some description about where they are
items :: [(Item, String)],
-- | Some monsters in this room
monsters :: [Monster],
-- | Association list between directions and a room
doors :: [(Direction, Room)],
-- | Function taking an item and a game state and returning what to do next
actions :: Item -> GameState -> Next GameState
}
data Direction = North | South | East | West
deriving (Eq)
instance Show Direction where
show North = "north"
show South = "south"
show East = "east"
show West = "west"
-- ## Items and monsters
data Item = Key | Spoon
deriving (Eq)
instance Show Item where
show Key = "key"
show Spoon = "spoon"
data Monster = WoodTroll {health :: Int, holding :: Item}
instance Show Monster where
show (WoodTroll health item) = "wood troll holding a " ++ show item
-- ## Command interface
-- ## QUESTION 17, CHUNK 3 => ADDED 'help' command
data Command
= Move Direction
| Use Item
| PickUp Item
| Help -- ----------> +
| End
-- ## Key type class
class Parsable t where
parse :: String -> Maybe t
-- ## Helpers for outputting to the user
tellContextLine :: String -> IO ()
tellContextLine s = putStrLn $ " " ++ s ++ "."
tellDoors :: [(Direction, Room)] -> IO ()
tellDoors [] = tellContextLine $ "There are no doors."
tellDoors [(dir, _)] = tellContextLine $ "There is a door to the " ++ show dir
tellDoors doors =
tellContextLine $ "There are doors to the " ++ (intercalate " and " (map (show . fst) doors))
tellItem :: (Item, String) -> IO ()
tellItem (item, pos) = tellContextLine $ pos ++ " there is a " ++ show item
tellMonster :: Monster -> IO ()
tellMonster monster = tellContextLine $ "There is a " ++ show monster
-- Main help for taking a game state and outputting information about it
tellContext :: GameState -> IO ()
tellContext (GS p r) = do
putStrLn ""
tellContextLine $ "You are in a " ++ name r ++ ". It is " ++ description r
tellDoors (doors r)
mapM tellItem (items r)
mapM tellMonster (monsters r)
putStrLn ""
return ()