Skip to content

Instantly share code, notes, and snippets.

@freddie-freeloader
Forked from jtomchak/starman.hs
Last active November 14, 2018 21:51
Show Gist options
  • Save freddie-freeloader/6ef705dede560c9b199a6d3dff15c5c4 to your computer and use it in GitHub Desktop.
Save freddie-freeloader/6ef705dede560c9b199a6d3dff15c5c4 to your computer and use it in GitHub Desktop.
Starman, version of hangman, in Haskell on the command line
main :: IO ()
main = do
starman "covfefe" 10
putStrLn "Program now shuts down"
--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