Skip to content

Instantly share code, notes, and snippets.

@keturiosakys
Created August 24, 2023 09:44
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 keturiosakys/cee37eebfc4f897a4ff61908dfce1fcf to your computer and use it in GitHub Desktop.
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.
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