Permalink
Browse files

Merge pull request #2 from georgeye/master

Beatself player. The idea is your previous throw affects greatly of the next throw of the other party. So try to beat the throws that beat your previous throw. While writing the code, cannot help to extend it to beat copycat and those repeaters.
  • Loading branch information...
Fernando Benavides
Fernando Benavides committed Mar 28, 2012
2 parents f119447 + 8f8c43d commit 1ed706ef62b4a25d1fd59136b61a4848bfba29a7
Showing with 31 additions and 0 deletions.
  1. +31 −0 src/players/rpsls_beatself_player.erl
@@ -0,0 +1,31 @@
-module(rpsls_beatself_player).
-author('George Ye <yegh98@yahoo.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([], State) -> {lists:nth(random:uniform(5), [rock, paper, scissors, lizard, spock]), State};
play([{Last, Mine},{Mine, _}|_], State) -> {beat_copycat(Last), State};
play([{_, Repeat},{_, Repeat }, {_, Repeat}|_], State) -> {beat_copycat(Repeat), State};
play([{Last, _}|_], State) -> {beat_winnersof(Last), State}.
beat_winnersof(rock) -> lizard;
beat_winnersof(paper) -> rock;
beat_winnersof(scissors) -> paper;
beat_winnersof(lizard) -> spock;
beat_winnersof(spock) -> scissors;
beat_winnersof(_) -> scissors. %% cannot happen but give scissors as default
beat_copycat(rock) -> lists:nth(random:uniform(2), [paper, spock]);
beat_copycat(paper) -> lists:nth(random:uniform(2), [scissors, lizard]);
beat_copycat(scissors) -> lists:nth(random:uniform(2), [rock, spock]);
beat_copycat(lizard) -> lists:nth(random:uniform(2), [scissors, rock]);
beat_copycat(spock) -> lists:nth(random:uniform(2), [paper, lizard]);
beat_copycat(_) -> scissors.

0 comments on commit 1ed706e

Please sign in to comment.