-- hangman game from Lecture 03A -- Refactored slightly -- Can you see further opportunities for refactoring? -- Hint: in more than one place we invite the user to input a value (string). -- Perhaps this should be abstracted as a function... -- To compile to a standalone program use -- ghc --make Hangman.hs main :: IO() main = do w <- getWord gameLoop "" w gameLoop :: String -> String -> IO() gameLoop g w = do if gameOver g w then endGame g else do putStrLn ("Word so far:" ++ progress g w) putStrLn ("Guesses so far: " ++ g) putStrLn "Guess some characters" putStr ">" g' <- getLine gameLoop (g' ++ g) w endGame g = do putStrLn ("Yay! You guessed it in " ++ show (length g) ++ " characters") putStrLn "(Type Enter key to exit)" getLine return () clear :: IO() -- clear screen clear = putStrLn (replicate screenheight '\n') where screenheight = 40 getWord :: IO(String) -- Get a word from player 1 getWord = do putStr "Player 1 please type a word\n" putStr ">" w <- getLine clear return w gameOver :: String -> String -> Bool gameOver g w = and [elem c g | c <- w ] progress :: String -> String -> String -- progress "abc" "baby" == "bab*" progress g w = [if elem c g then c else '*' | c <- w]