Skip to content

Instantly share code, notes, and snippets.

@jtomchak
Created September 28, 2017 04:02
Show Gist options
  • Save jtomchak/7ecf0d358c4b6fab5055ed37db6abcb6 to your computer and use it in GitHub Desktop.
Save jtomchak/7ecf0d358c4b6fab5055ed37db6abcb6 to your computer and use it in GitHub Desktop.
Starman, version of hangman, in Haskell on the command line
--starman super game
starman :: String -> Int -> IO ()
starman word n = turn word ['-' | x <- word] n
--checking the guessed letter against the word
check :: String -> String -> Char -> (Bool, String)
check word display c =
( c `elem` word
, [ if x == c
then c
else y
| (x, y) <- zip word display
])
--increment turn count, either end with win/loss or invoke make guess function
turn :: String -> String -> Int -> IO ()
turn word display n = do
if n == 0
then putStrLn "You Lose"
else if word == display
then putStrLn "You Totally Win"
else mkguess word display n
--make a guess from the user, get input from terminal
mkguess :: String -> String -> Int -> IO ()
mkguess word display n = do
putStrLn (display ++ " " ++ take n (repeat '*'))
putStr " Enter your guess: "
q <- getLine
let (correct, display') = check word display (q !! 0)
let n' =
if correct
then n
else n - 1
turn word display' n'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment