-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstarman2.hs
44 lines (37 loc) · 1.15 KB
/
starman2.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
import System.Random
check :: String -> String -> Char -> (Bool, String)
check word display c =
(c `elem` word, [ if x == c
then c
else y
| (x,y) <- zip word display ])
turn :: String -> String -> Int -> IO ()
turn word display n =
do if n == 0
then putStrLn ("You lose! The word was " ++ word)
else if word == display
then putStrLn ("You win! The word is " ++ word)
else mkguess word display n
mkguess :: String -> String -> Int -> IO ()
mkguess word display n =
do putStr (display ++ " " ++ take n (repeat '*'))
putStr " Enter your guess: "
q <- getLine
if q == ""
then mkguess word display n
else do
let char = q !! 0
if (elem char ['a'..'z'])
then do
let (correct, display') = check word display char
let n' = if correct then n else n - 1
turn word display' n'
else do mkguess word display n
starman :: IO ()
starman = do
words <- readFile "words"
let list = lines words
index <- randomRIO (0, (length list)-1) :: IO Int
let word = list !! index
let n = length word
turn word [ '-' | x <- word ] n