Skip to content

Instantly share code, notes, and snippets.

@beshanoe
Created April 9, 2018 12:23
Show Gist options
  • Save beshanoe/dcff78be24be087bb3dd4148da47ea07 to your computer and use it in GitHub Desktop.
Save beshanoe/dcff78be24be087bb3dd4148da47ea07 to your computer and use it in GitHub Desktop.
import System.Random
getRandomWord :: [String] -> IO String
getRandomWord words =
do n <- randomRIO (0, min 0 ((length words) - 1))
return $ words !! 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])
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
mkguess :: String -> String -> Int -> IO()
mkguess word display n =
do putStrLn (display ++ " " ++ take n (repeat '*'))
putStr " Enter your guess: "
q <- getLine
let (correct, display') = check word display (q!!0)
let n' = if correct then n else n-1
turn word display' n'
starmanBasic :: String -> Int -> IO ()
starmanBasic word n = turn word ['-' | x <- word] n
starmanWords :: [String]
starmanWords = ["haskell", "monad", "functor", "currying"]
starman :: IO ()
starman =
do word <- getRandomWord starmanWords
starmanBasic word 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment