-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangman.hs
36 lines (31 loc) · 834 Bytes
/
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
import System.IO
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
guess :: String -> IO ()
guess guessWord =
do putStr "Try to guess: "
attempt <- getLine
if attempt == guessWord then
do putStrLn "You win!!!"
else
do putStrLn (diff guessWord attempt)
guess guessWord
diff :: String -> String -> String
diff xs ys = [ if x `elem` ys then x else '_'| x <- xs ]
main :: IO ()
main =
do putStr "Enter a secret word: "
word <- sgetLine
guess word