Skip to content

Instantly share code, notes, and snippets.

@hjpbarcelos
Created April 8, 2013 00:02
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 hjpbarcelos/5333196 to your computer and use it in GitHub Desktop.
Save hjpbarcelos/5333196 to your computer and use it in GitHub Desktop.
Pebolim com erlang.
%%% File: fussball.erl
%%% Description: A simple game of Fussball.
-module(fussball).
%% Interface
-export([start/2, init/2, stop/1, kickoff/1]).
start(MyCountry, OtherCountry) ->
spawn(?MODULE, init, [MyCountry, OtherCountry]),
spawn(?MODULE, init, [OtherCountry, MyCountry]),
ok.
stop(Country) ->
Country ! stop.
kickoff(Country) ->
Country ! kick,
ok.
init(MyCountry, OtherCountry) ->
process_flag(trap_exit, true),
register(MyCountry, self()),
try link(whereis(OtherCountry)) of
_ -> ok
catch
_:Error -> {error, caught, Error}
end,
loop(MyCountry, OtherCountry).
loop(MyCountry, OtherCountry) ->
receive
{'EXIT', _Pid, Reason} ->
io:format("Got exit signal: ~p~n", [Reason]),
exit(Reason);
stop ->
exit(user);
save ->
io:format("~p just saved...~n", [OtherCountry]),
loop(MyCountry, OtherCountry);
score ->
io:format("Oh no! ~p just scored!!~n", [OtherCountry]),
loop(MyCountry, OtherCountry);
kick ->
timer:sleep(500),
case random:uniform(1000) of
N when N > 950 ->
io:format("~p SAVES! And what a save!!~n", [MyCountry]),
OtherCountry ! save,
OtherCountry ! kick;
N when N > 800 ->
io:format("~p SCORES!!~n", [MyCountry]),
OtherCountry ! score;
_ ->
io:format("~p kicks the ball...~n", [MyCountry]),
OtherCountry ! kick
end,
loop(MyCountry, OtherCountry)
after
15000 ->
io:format("Geez, I'm bored. Bye!~n"),
exit(timeout)
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment