module GameLogic(State,lost,won,word,createState,gameState,addGuess) -- State as a type is exported, but not its constructor (i.e. it is an abstract data type) where import Data.List --------------- -- Game logic for Hangman game -- L03A 2014-11-17 -- Dave Sands data State = State String String -- word guesslist word, guess :: State -> String word (State w _) = w guess (State _ g) = g createState w = State w "" guessLimit :: Int guessLimit = 6 badGuesses :: State -> String badGuesses (State w g) = [c | c <- g, c `notElem` w] lives :: State -> Int lives s = guessLimit - length (badGuesses s) wordState,gameState :: State -> String wordState (State w g) = [if c `elem` g then c else '_' | c <- w ] gameState s = unlines [wordState s, show (lives s) ++ " lives remaining"] won,lost :: State -> Bool won (State w g) = and [c `elem` g| c <- w ] lost s = lives s <= 0 addGuess :: String -> State -> State addGuess newguess (State w g) = State w (g ++ g') where g' = take (lives $ State w g) newguess