-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman.hs
More file actions
41 lines (34 loc) · 998 Bytes
/
hangman.hs
File metadata and controls
41 lines (34 loc) · 998 Bytes
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
import System.IO
act :: IO (Char , Char)
act = do x <- getChar
y <- getChar
return (x,y)
hangman :: IO ()
hangman = do putStrLn "Think of a word: "
word <- sgetLine
putStrLn "Try to guess it: "
play word
sgetLine :: IO String
sgetLine = do x <- getCh
if x == '\n' then
do putChar x
return []
else
do putChar '-'
xs <- sgetLine
return (x:xs)
getCh :: IO Char
getCh = do hSetEcho stdin False
x <- getChar
hSetEcho stdin True
return x
play :: String -> IO ()
play word = do putStr "? "
guess <- getLine
if guess == word then
putStrLn "You got it!!"
else
do putStrLn (match word guess)
play word
match :: String -> String -> String
match xs ys = [if elem x ys then x else '-' | x <- xs]