-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman.hs
59 lines (50 loc) · 1.1 KB
/
hangman.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
48
49
50
51
52
53
54
55
56
57
58
59
module Hangman where
import System.IO
getCh :: IO Char
getCh =
do hSetEcho stdin False
c <- getChar
hSetEcho stdin True
return c
diff :: String -> String -> String
diff xs ys =
[if x `elem` ys then x else '-' | x <- xs]
-- putStr :: String -> IO ()
-- putStr [] = return ()
-- putStr (x:xs) = do putChar x
-- putStr xs
-- putStrLn :: String -> IO ()
-- putStrLn xs = do putStr xs
-- putChar '\n'
-- getLine :: IO String
-- getLine = do x <- getChar
-- if x == '\n' then
-- return []
-- else
-- do xs <- getLine
-- return (x:xs)
sgetLine :: IO String
sgetLine =
do x <- getCh
if x == '\n' then
do putChar x
return []
else
do putChar '_'
xs <- sgetLine
return (x:xs)
guess :: String -> IO ()
guess word =
do putStr "> "
xs <- getLine
if xs == word then
putStrLn "You got it!"
else
do putStrLn (diff word xs)
guess word
hangman :: IO ()
hangman =
do putStrLn "Think of a word: "
word <- sgetLine
putStrLn "Try to guess it:"
guess word