forked from instinctive/edu-wumpus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Input.hs
43 lines (38 loc) · 1.31 KB
/
Input.hs
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
{-# LANGUAGE NoImplicitPrelude #-}
module Input where
import BasePrelude
import qualified System.IO as Sys
import Common
import Output
import Parse
-- | Get an 'Action' from the player.
--
-- This function just calls 'hGetAction' on 'Sys.stdin'.
getAction :: IO Action
getAction = hGetAction Sys.stdin
-- | Get an 'Action' from the player.
--
-- This function should prompt the user using 'printPrompt', and then use
-- 'hGetLine' and 'parseAction to get the player action.
--
-- Note that 'hGetLine' is just like 'getLine' except that it takes a 'Handle'
-- argument, which will be @h@ in this case. We write the function with this
-- extra handle argument so that we can test it.
--
-- Note that 'parseAction' has a 'Maybe' return type, and this function does
-- not. If the parsed action is 'Nothing', this function should use 'printHelp'
-- to print the help text, and then try again.
--
-- >>> test i = Sys.openTempFile "/tmp" "foo" >>= \(s,h) -> Sys.hPutStrLn h i >> Sys.hClose h >> pure s >> Sys.withFile s Sys.ReadMode hGetAction
-- >>> test "move 4"
-- What do you do? Move 4
-- >>> test "shoot 4"
-- What do you do? Shoot 4
-- >>> test "sdfsdf\nmove 4"
-- What do you do? Valid commands:
-- quit
-- move <cave>
-- shoot <cave>
-- What do you do? Move 4
hGetAction :: Sys.Handle -> IO Action
hGetAction h = undefined