-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14 - IO.hs
47 lines (45 loc) · 1.26 KB
/
14 - IO.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
44
45
46
47
--getChar :: IO Char
--putChar :: Char -> IO()
--putStr :: String -> IO()
--putStrLn :: String -> IO()
--readFile :: FilePath -> IO String
--writeFile :: FilePath -> String -> IO()
--appendFile :: FilePath -> String -> IO()
--randomRIO :: (a,a) -> IO a
getPair :: IO(Char,Char)
getPair = do
x <- getChar
getChar
y <- getChar
return (x,y)
hangman :: IO()
hangman = do
putStrLn "Think of a word..."
word <- secretGetLine
let spaces = replicate (length word) '-'
putStrLn spaces
putStrLn "Try to guess it..."
play word spaces
secretGetLine :: IO String
secretGetLine = do
xs <- getLine
return xs
play :: String -> String -> IO()
play word currentAnswer
| currentAnswer == word = putStrLn "Correct!"
| otherwise = do
putStrLn "Enter a character..."
guess <- getChar
newAnswer <- putUpdate(updateMatch word currentAnswer guess)
play word newAnswer
putUpdate :: String -> IO String
putUpdate s = do
putStr "Your answer so far is: "
putStrLn s
return s
updateMatch :: String -> String -> Char -> String
updateMatch [] [] guess = []
updateMatch (x:xs) (y:ys) g
| x == y = x : updateMatch xs ys g
| x == g = x : updateMatch xs ys g
| otherwise = '-' : updateMatch xs ys g