Last active
September 30, 2016 22:14
-
-
Save satiridev/12439b6a47de4956b0146f40f06476bd to your computer and use it in GitHub Desktop.
starman example from futururelearn.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- main function | |
starman :: String -> Int -> IO () | |
starman word n = turn word ['-' | x <- word] n | |
-- | |
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]) | |
-- count the turn | |
turn :: String -> String -> Int -> IO () | |
turn word display n = | |
do if n == 0 | |
then putStrLn "You lose" | |
else if word==display | |
then putStrLn "You win!" | |
else mkguess word display n | |
-- read a user response | |
mkguess :: String -> String -> Int -> IO() | |
mkguess word display n = | |
do putStrLn (display ++ " " ++ take n (repeat '*')) | |
putStr " Enter your guess (or type 1 to exit) : " | |
q <- getLine | |
let q' = head q | |
let (correct, display') = check word display q' | |
let n' = checkN correct q' n | |
turn word display' n' | |
-- check for special character to exit | |
checkN :: Bool -> Char -> Int -> Int | |
checkN x '1' n = 0 | |
checkN True c n = n | |
checkN x c n = n-1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment