From 05f97e305da1b5dc2604a50b6d0f453f3431649b Mon Sep 17 00:00:00 2001 From: Vince Knight Date: Mon, 4 Sep 2017 08:13:42 +0100 Subject: [PATCH] Write description/ex of `receive_match_attributes` Note we only need to do this when the inputs from the match are used to change some other variable. Some other minor tweaks here: - Remove unneeded data file. This was checked in at some point but shouldn't have been. - Remove outdated documentation about repr. This is now done automatically by the parent class. - Make some minor stylistic changes. - PEP8 (column width) - Change to use class hyperlink instead of code syntax highlighting. --- .../strategy/writing_the_new_strategy.rst | 39 +++++++------------ training_data.csv | 0 2 files changed, 15 insertions(+), 24 deletions(-) delete mode 100644 training_data.csv diff --git a/docs/tutorials/contributing/strategy/writing_the_new_strategy.rst b/docs/tutorials/contributing/strategy/writing_the_new_strategy.rst index 43a43bbcb..cfa7ca00a 100644 --- a/docs/tutorials/contributing/strategy/writing_the_new_strategy.rst +++ b/docs/tutorials/contributing/strategy/writing_the_new_strategy.rst @@ -22,7 +22,8 @@ The code -------- There are a couple of things that need to be created in a strategy.py file. Let -us take a look at the :code:`TitForTat` class (located in the +us take a look at the :class:`TitForTat +` class (located in the :code:`axelrod/strategies/titfortat.py` file):: class TitForTat(Player): @@ -101,7 +102,8 @@ This helps classify the strategy as described in :ref:`classification-of-strategies`. After that the only thing required is to write the :code:`strategy` method -which takes an opponent as an argument. In the case of :code:`TitForTat` the +which takes an opponent as an argument. In the case of +:class:`TitForTat ` the strategy checks if it has any history (:code:`if len(self.history) == 0`). If it does not (ie this is the first play of the match) then it returns :code:`C`. If not, the strategy simply repeats the opponent's last move (:code:`return @@ -115,30 +117,19 @@ opponent.history[-1]`):: # Repeat the opponent's last move return opponent.history[-1] -The variables :code:`C` and :code:`D` represent the cooperate and defect actions respectively. +The variables :code:`C` and :code:`D` represent the cooperate and defect actions +respectively. -You can also modify the name of the strategy with the :code:`__repr__` method, -which is invoked when :code:`str` is applied to a player instance. For example, -the :code:`Random` strategy takes a parameter :code:`p` for how often it -cooperates, and the :code:`__repr__` method adds the value of this parameter to -the name:: +Some strategies make specific use of the variables of a match to create their +own attributes, in this case the :code:`receive_match_attributes` method must be +defined. Here is how this is done for +:class:`Stalker `:: - def __repr__(self): - return "%s: %s" % (self.name, round(self.p, 2)) - -Now we have separate names for different instantiations:: - - >>> import axelrod - >>> player1 = axelrod.Random(p=0.5) - >>> player2 = axelrod.Random(p=0.1) - >>> player1 - Random: 0.5 - >>> player2 - Random: 0.1 - -This helps distinguish players in tournaments that have multiple instances of the -same strategy. If you modify the :code:`__repr__` method of player, be sure to -add an appropriate test. + def receive_match_attributes(self) + R, P, S, T = self.match_attributes["game"].RPST() + self.very_good_score = R + self.very_bad_score = P + self.wish_score = (R + P) / 2 There are various examples of helpful functions and properties that make writing strategies easier. Do not hesitate to get in touch with the diff --git a/training_data.csv b/training_data.csv deleted file mode 100644 index e69de29bb..000000000