Skip to content

Instantly share code, notes, and snippets.

@gmichokostas
Created September 28, 2016 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gmichokostas/2162aaf1930569ddeb786ec09947357e to your computer and use it in GitHub Desktop.
Save gmichokostas/2162aaf1930569ddeb786ec09947357e to your computer and use it in GitHub Desktop.
module GuessTheNumber where
import System.Random
startGame :: IO ()
startGame = do
let tries = 5
num <- randomRIO (0, 9) :: IO Int
makeGuess num tries
makeGuess :: Int -> Int -> IO ()
makeGuess num tries = do
putStrLn("You have got " ++ show(tries) ++ " tries")
if tries == 0 then putStrLn "You have lost."
else do
putStr "Guess the number: "
input <- getLine
checkNumber num input tries
checkNumber :: Int -> String -> Int -> IO ()
checkNumber num guess tries = do
if num > (read guess :: Int) then do
putStrLn "You have guessed too low, please try a higher number."
makeGuess num (tries - 1)
else if num < (read guess :: Int) then do
putStrLn "You have guessed too high, please try a lower number."
makeGuess num (tries - 1)
else putStrLn "You won!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment