-- Hangman game, Lecture 3A -- 2011-11-07 -- Dave Sands import Data.List import System.Random(randomRIO) wordList = "/usr/share/dict/words" -- mac OS X sizeWordList = 234935 guessLimit = 10 main :: IO() main = do w <- getWord gameLoop w "" getWord :: IO String getWord = do ws <- readFile wordList n <- randomRIO (0,sizeWordList) return $ lines ws !! n gameLoop w g | loose = looser w g | win = winner w g | otherwise = do putStrLn [if c `elem` g then c else '_'| c <- w] putStrLn "Please type one or more characters" s <- getLine gameLoop w (g `union` s) where win = all (`elem` g) w loose = length (g \\ w) >= guessLimit winner w g = putStrLn $ "Well done! You guessed " ++ w ++ " in " ++ show (length g) ++ " guesses" looser w g = putStrLn $ "You die. The word was " ++ w