Permalink
Browse files

rpsls_nicolas_player.erl

Jugador analitico
  • Loading branch information...
Nicolas Maman Nicolas Maman
Nicolas Maman authored and Nicolas Maman committed Jul 30, 2014
1 parent e681cd7 commit de499ff9e88ede6f8cd0807b5b77df2dbe18a6ac
Showing with 71 additions and 0 deletions.
  1. +71 −0 src/players/rpsls_nicolas_player.erl
@@ -0,0 +1,71 @@
-module(rpsls_nicolas_player).
-author('nicolas nicolasmd87@gmail.com').
-behaviour(rpsls_player).
-export([init/0, play/2]).
-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,State) ->
case History of
[]->
{lists:nth(random:uniform(5), [rock, paper, scissors, lizard, spock]), State};
Other->
{ProcessedHistoryList,TotalPlayedQuantity}=processHistory(History,[{rock,0},{paper,0},{scissors,0},{lizard,0},{spock,0}],0),
%%Ordeno la lista de mayor a menor
ProcessedHistoryList_Sorted=lists:keysort(2,ProcessedHistoryList),
%%Ahora se la jugada mas utilizada por mi rival (Key), pero primero deberia verificar si esa jugada ya la jugo la mano pasada, eso me daria un indicio que capaz eliga la segunda mas jugada por el
%%No es el algoritmo mas ganador del mundo pero sirve
{Key,_Val}=lists:last(ProcessedHistoryList_Sorted),
[{_,LastRivalPlay}|RestOfHistory]=History,
WinningPlayKey=if(Key==LastRivalPlay)->
lists:keydelete(Key,1, ProcessedHistoryList_Sorted),
{NewKey,_}=lists:last(ProcessedHistoryList_Sorted),
getWinningKeyFor(NewKey);
true->
getWinningKeyFor(Key)
end,
{WinningPlayKey,State}
end.
getWinningKeyFor(rock) -> paper;
getWinningKeyFor(paper) -> scissors;
getWinningKeyFor(scissors) -> rock;
getWinningKeyFor(lizard) -> scissors;
getWinningKeyFor(spock) -> lizard.
%%Se procesa el historial recursivamente para saber cuantas ocurrencias de cada jugada del rival hay para su posterior procesamiento
processHistory([],ProcessedHistoryList,PlayCounter)->
{ProcessedHistoryList,PlayCounter};
processHistory(History,ProcessedHistoryList,PlayCounter)->
[{_,RivalPlay}|RestOfHistory]=History,
NewProcessedHistoryList=case RivalPlay of
rock->
generateNewProcessedList(rock,ProcessedHistoryList);
paper->
generateNewProcessedList(paper,ProcessedHistoryList);
scissors->
generateNewProcessedList(scissors,ProcessedHistoryList);
lizard->
generateNewProcessedList(lizard,ProcessedHistoryList);
spock->
generateNewProcessedList(spock,ProcessedHistoryList)
end,
processHistory(RestOfHistory,NewProcessedHistoryList,PlayCounter+1).
%%Busca por clave (rock,paper,etc) y luego incrementa en su debido contador y devuelve la lista
generateNewProcessedList(Key,ProcessedHistoryList)->
{_,{TupleKey,Value}}=lists:keysearch(Key,1,ProcessedHistoryList),
lists:keyreplace(TupleKey,1,ProcessedHistoryList,{TupleKey,Value+1}).

0 comments on commit de499ff

Please sign in to comment.