Created
April 12, 2012 05:43
-
-
Save BigEndian/2364796 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
module Hangman( | |
Hangman, | |
createHangman, | |
chooseWord, | |
playGame) where | |
import System.IO(readFile) | |
import System.Random | |
import Data.Char(isLower) | |
import qualified Data.Map as Map | |
data Space c = Space c | Unguessed deriving (Show, Eq) | |
data Hangman = Hangman { dictwords :: [String], word :: String, rgen :: StdGen, guesses :: Map.Map Char Bool } deriving (Show) | |
removePossessives :: [String] -> [String] | |
removePossessives words = filter (\w -> drop (length w -2) w /= "'s") words | |
removeProperNouns :: [String] -> [String] | |
removeProperNouns words = filter (isLower . head) words | |
getDictionary :: String -> IO [String] | |
getDictionary name = do | |
c <- readFile $ "/usr/share/dict/" ++ name | |
return $ removePossessives $ removeProperNouns $ words c | |
createHangman :: IO Hangman | |
createHangman = do | |
hwords <- getDictionary "american-english" | |
gen <- newStdGen | |
let word = chooseWord gen hwords | |
return Hangman { dictwords = hwords, word = "", rgen = gen, guesses = Map.fromList _guesses } where | |
_guesses = map (\x -> (x, False)) ['a'..'z'] | |
chooseWord :: StdGen -> [String] -> String | |
chooseWord gen words = words !! (rn `mod` (length words)) where | |
rn = head (randoms gen :: [Int]) | |
playGame :: IO () | |
playGame = do | |
hng <- createHangman | |
gen <- newStdGen | |
putStrLn $ chooseWord gen $ dictwords hng | |
return () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment