-- | Hangman: a word guessing game -- Functional Programming course 2018. -- Thomas Hallgren {- This started as a skeleton, the definitions were filled in during the lecture. -} import System.Random(randomRIO) import Data.List(nub) main = do word <- getRandomWord play word "" getRandomWord :: IO String getRandomWord = do text <- readFile "/usr/share/dict/words" let ls = lines text ix <- randomRIO (0,length ls-1) return (ls !! ix) play :: String -> String -> IO () play word guesses = do putStrLn [if l `elem` guesses then l else '_' | l <- word] putStrLn ("Number of guesses left: "++show guessesLeft) if and [l `elem` guesses | l <- word] then putStrLn "You won!" else if guessesLeft <= 0 then do putStrLn "You lost! Game over!" putStrLn ("The word was: "++word) else do putStrLn "Enter your guess:" s <- getLine play word (s++guesses) where guessesLeft = length word + 4 - length guesses -- TODO: test if the player has run out of guesses *before* testing if the -- player has won. Otherwise the player can enter the whole alphabet at once -- and win.