Hangman in PureScript
An example run
Insert word to guess > hello ----- Try to guess > hol h-l-- > hello You won!
module Main where import Prelude import Effect (Effect) import Effect.Console (log) import Node.ReadLine as RL import Data.String.Yarn (fromChars, toChars) as S import Data.List.Lazy as L import Data.String (length) as S mask :: String -> String -> String = mask word guess let as = S.toChars word :: L.List Char = S.toChars guess :: L.List Char bs = bs <> L.replicate (L.length as - L.length bs) '-' bs' = if a == b then a else '-' zipper a b in $ L.zipWith zipper as bs' S.fromChars main :: Effect Unit = do main <- RL.createConsoleInterface RL.noCompletion interface let = lineHandler word guess case word of "" -> do log $ S.fromChars $ L.take (S.length guess) (L.repeat '-') log "Try to guess" $ lineHandler guess RL.setLineHandler interface "> " 2 interface RL.setPrompt RL.prompt interface-> _ if word == guess then do RL.close interfacelog "You won!" else do log $ mask word guess "> " 2 interface RL.setPrompt RL.prompt interface$ lineHandler "" RL.setLineHandler interface log "Insert word to guess" "> " 2 interface RL.setPrompt RL.prompt interface
Support my work by tweeting this article! 🙏