Skip to content

Instantly share code, notes, and snippets.

@mem-memov mem-memov/starman.hs
Created Sep 18, 2019

Embed
What would you like to do?
guess dog or cat
import System.Random
starman :: Int -> IO ()
starman n = do
let allWords = ["cat", "dog"]
index <- randomRIO(0, (length allWords) - 1)
let word = allWords !! index
turn word ['-' | x <- word] n
turn :: String -> String -> Int -> IO ()
turn word display n =
do if n==0
then putStrLn "You lose"
else if word==display
then putStrLn "You win!"
else mkguess word display n
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'
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])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.