Permalink
Browse files

Two new players

  • Loading branch information...
Fernando "Brujo" Benavides
Fernando "Brujo" Benavides committed May 4, 2012
1 parent 2a60397 commit a08f2f6794d70240f099bfef1128179eddf9bd7e
Showing with 59 additions and 0 deletions.
  1. +16 −0 src/players/rpsls_leor_player.erl
  2. +43 −0 src/players/rpsls_marcelo_player.erl
@@ -0,0 +1,16 @@
+%% @author Leor Leor <leorleor@gmail.com>
+%% @copyright 2012
+%% @doc Rockem Spockem bot by Leor
+-module(rpsls_leor_player).
+-author(' Leor Leor <leorleor@gmail.com>').
+
+-behaviour(rpsls_player).
+
+-export([init/0, play/2]).
+
+%% @private
+-spec init() -> State::term().
+init() -> _ = random:seed(erlang:now()), {}.
+
+-spec play(History::[{You::rpsls_player:choice(), Rival::rpsls_player:choice()}], State::term()) -> {rpsls_player:choice(), NewState::term()}.
+play(_History, {}) -> {lists:nth(random:uniform(5), [paper, scissors, spock, rock, lizard]), {}}.
@@ -0,0 +1,43 @@
+%% @author Fernando Benavides <fernando.benavides@inakanetworks.com>
+%% @copyright 2012 Inaka Networks
+%% @doc Player that always choose spock
+-module(rpsls_marcelo_player).
+-author('Fernando Benavides <fernando.benavides@inakanetworks.com>').
+
+-behaviour(rpsls_player).
+
+-export([init/0, play/2]).
+
+%% @private
+-spec init() -> State::term().
+init() -> spock.
+
+-spec play(History::[{You::rpsls_player:choice(), Rival::rpsls_player:choice()}], State::term()) -> {rpsls_player:choice(), NewState::term()}.
+
+choose_winner(spock) ->
+ lizard;
+
+choose_winner(lizard) ->
+ rock;
+
+choose_winner(rock) ->
+ paper;
+
+choose_winner(paper) ->
+ scissors;
+
+choose_winner(scissors) ->
+ spock.
+
+play(History, _State) ->
+ MoveCount = lists:foldl(
+ fun(Round, Acc) ->
+ {_Mine, Opponent} = Round,
+ {Opponent,Times} = lists:keyfind(Opponent, 1, Acc),
+ lists:keystore(Opponent, 1, Acc, {Opponent, Times + 1})
+ end,
+ [{spock, 0}, {scissors, 0}, {paper, 0}, {rock, 0}, {lizard, 0}],
+ History
+ ),
+ {Move, _Times} = lists:last(lists:keysort(2, MoveCount)),
+ {choose_winner(Move), _State}.

0 comments on commit a08f2f6

Please sign in to comment.