I am doing a number guessing game in Haskell and I have 3 difficulties.
I made a menu where the user selects what difficulty but once I implemented it, it started giving me an error and I can't figure out what it is.
The error:
finalename.hs:5:10:
Parse error in pattern: putStrLn
Possibly caused by a missing 'do'?
Also how would I go about implementing a count where it counts how many times it takes for the user to guess?
import System.Random (randomRIO)
--main app
main :: IO ()
main = do
putStrLn "Do you want to play 1) Easy, 2) Medium or 3) Hard"
putStrLn "Select your difficulty by entering a number 1, 2 or 3"
choice <- getLine
if (choice == "1")
then do putStrLn "You selected easy"
easy
else if (choice == "2")
then do putStrLn "You selected medium"
medium
else if (choice == "3"
then do putStrLn "You selected hard"
hard
else do putStrLn "You did not select a valid choice"
main
easy :: IO ()
easy = do
putStrLn "Guess a number between 1 and 13: "
rNumber <- randomRIO (1, 10) :: IO Int
loop rNumber --goes to the loop function
medium :: IO ()
medium = do
putStrLn "Guess a number between 1 and 25: "
rNumber <- randomRIO (1, 25) :: IO Int
loop rNumber --goes to the loop function
hard :: IO ()
hard = do
putStrLn "Guess a number between 1 and 50: "
rNumber <- randomRIO (1, 50) :: IO Int
loop rNumber --goes to the loop function
loop :: Int -> IO ()
loop rNumber = do -- does this function until the correct answer is selected
input <- getLine
let userInput = read input :: Int -- reads the user inputs
if userInput < rNumber -- if the unput is lower than the random number
then do
putStrLn "Too low; guess again: "
loop rNumber -- calls function again
else if userInput > rNumber -- if the input is higher than the random number
then do
putStrLn "Too high; guess again: "
loop rNumber
else do
putStrLn "Correct!"
main
Aucun commentaire:
Enregistrer un commentaire