-
-
Save rightfold/9271943 to your computer and use it in GitHub Desktop.
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
-module(numbergame). | |
-export([play/0]). | |
play() -> | |
intro(), | |
game_loop(get_int(), 666). | |
intro() -> | |
io:format("Welcome to this 'game'.~n"), | |
io:format("Enter numbers to narrow down onto what I am hard coded for~n"), | |
io:format("Enter one mumber at a time (integers),~n"), | |
io:format(" I will tell you if you need to go higher or lower.~n~n"). | |
game_loop(Target, Target) -> | |
io:format("Well done, you got it right!~n"); | |
game_loop(Guess, Target) -> | |
io:format("Nope, that is not the number.~n"), | |
game_loop(get_int(), Target). | |
get_int() -> | |
Input = io:get_line("Enter a number: "), | |
case string:to_integer(Input) of | |
{error, Reason} -> show_error(Reason), get_int(); | |
{Int, _} -> Int | |
end. | |
show_error(no_integer) -> io:format("I said a number!~n"); | |
show_error(Reason) -> io:format("You did something funky: ~p~n", [Reason]). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment