Created
September 28, 2016 21:12
-
-
Save gmichokostas/2162aaf1930569ddeb786ec09947357e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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