-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman.hs
47 lines (39 loc) · 1.12 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
import System.IO
getCh = do
hSetEcho stdin False
x <- getChar
hSetEcho stdin True
return x
sgetLine = do
x <- getCh
if x == '\n' then
do
putChar x
return []
else
do
putChar '-'
xs <- sgetLine
return (x:xs)
hangman = do
putStrLn "Think a word: "
word <- sgetLine
putStrLn "Try to guess it: "
play word
play word = do
putChar '?'
guess <- getLine
let result = fun1 word guess
putStrLn result
if (guess == word) then putStr "You got it!"
else
play word
fun1 word guess = map fun4 (map fun2 lst)
where
lst = [[(x,y) | x <- guess] | y <- word]
fun2 lst = map fun3 t
where
t = [ t | t <- lst]
fun3 t = if fst (t) == snd (t) then fst(t) else '-'
fun4 "" = '-'
fun4 (x:xs) = if x == '-' then fun4 xs else x