Created
May 10, 2025 18:59
-
-
Save frectonz/18c0097fd8999302319417a66c8a99e7 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
| import System.Random | |
| import Data.String | |
| record Game where | |
| constructor MkGame | |
| answer : Nat | |
| tries : Nat | |
| Show Game where | |
| show (MkGame answer tries) = "Game (answer = \{show answer}, tries = \{show tries})" | |
| newGame : IO Game | |
| newGame = do | |
| answer <- randomRIO (the Int32 0, the Int32 100) | |
| pure $ MkGame (cast answer) 0 | |
| data Guess = Quit | NotNumber | OutOfBounds | Number Nat | |
| Show Guess where | |
| show Quit = "π Good Bye." | |
| show NotNumber = "π Guess is not a number." | |
| show OutOfBounds = "π Guess is out of bounds." | |
| show (Number x) = "π² You guessed \{show x}." | |
| getGuess : IO Guess | |
| getGuess = do | |
| putStr "π― Enter guess (q to Quit): " | |
| guess <- getLine | |
| pure (if guess == "q" then Quit else (checkGuess $ parsePositive guess)) | |
| where | |
| checkGuess : Maybe Nat -> Guess | |
| checkGuess Nothing = NotNumber | |
| checkGuess (Just x) = if (withInBounds x) then OutOfBounds else Number x | |
| where | |
| withInBounds : Nat -> Bool | |
| withInBounds x = x < 0 || x > 100 | |
| incrementTry : Game -> Game | |
| incrementTry game = | |
| { tries $= (+ 1) } game | |
| data CheckResult = GreaterThan | LessThan | Equal | |
| Show CheckResult where | |
| show GreaterThan = "π Try a bigger number." | |
| show LessThan = "π Try a smaller number." | |
| show Equal = "π You got it." | |
| checkGuess : Nat -> Game -> CheckResult | |
| checkGuess guess game = | |
| if game.answer > guess then | |
| GreaterThan | |
| else if game.answer < guess then | |
| LessThan | |
| else | |
| Equal | |
| gameLoop : Game -> IO () | |
| gameLoop game = do | |
| guess <- getGuess | |
| putStrLn (show guess) | |
| case guess of | |
| Quit => pure () | |
| Number guess => | |
| let result = checkGuess guess game in | |
| do | |
| putStrLn (show result) | |
| case result of | |
| Equal => pure () | |
| _ => gameLoop (incrementTry game) | |
| _ => gameLoop (incrementTry game) | |
| main : IO () | |
| main = do | |
| game <- newGame | |
| gameLoop game | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment