Skip to content

Instantly share code, notes, and snippets.

@thecoshman
Last active August 29, 2015 13:56
Show Gist options
  • Save thecoshman/9271838 to your computer and use it in GitHub Desktop.
Save thecoshman/9271838 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) when Guess > Target ->
io:format("Woah there nelly, back it down a spot!~n"),
game_loop(get_int(), Target);
game_loop(_Guess, Target) ->
io:format("Nope, need to go higher!~n"),
game_loop(get_int(), Target).
get_int() ->
Input = io:get_line("Enter a number: "),
case string:to_integer(Input) of
{error, Reason} ->
shout_at_user(Reason),
get_int();
{Int, _} ->
Int
end.
shout_at_user(no_integer) ->
io:format("I said a number!~n");
shout_at_user(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