-
-
Save keturiosakys/cee37eebfc4f897a4ff61908dfce1fcf to your computer and use it in GitHub Desktop.
Make a "guessing game" where there is a target number, and as the user makes guesses, the output returns higher or lower until the user is correct.
This file contains 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
open Stdio | |
let parse_input inp = | |
match inp with | |
| None -> raise (Failure "No input recorded! Exiting") | |
| Some x -> int_of_string x | |
;; | |
let rec guesser secret num_of_guesses = | |
let input = Stdio.In_channel.input_line Stdio.stdin in | |
let guess = parse_input input in | |
match Int.compare guess secret with | |
| 0 -> printf "Correct! You won in %d guesses!\n" num_of_guesses | |
| 1 -> | |
print_endline "Lower"; | |
guesser secret (num_of_guesses + 1) | |
| _ -> | |
print_endline "Higher"; | |
guesser secret (num_of_guesses + 1) | |
;; | |
let () = | |
print_endline "Guess the number!"; | |
let secret = Random.int 100 in | |
guesser secret 1 | |
;; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment