Skip to content

Instantly share code, notes, and snippets.

@rightfold
Forked from thecoshman/numbergame.erl
Last active August 29, 2015 13:56
Show Gist options
  • Save rightfold/9271943 to your computer and use it in GitHub Desktop.
Save rightfold/9271943 to your computer and use it in GitHub Desktop.
-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