Skip to content

Commit e5f080f

Browse files
authored
Merge pull request #1128 from Axelrod-Python/document-receive-match-attributes
Write description/ex of `receive_match_attributes`
2 parents 3fd3250 + e225a9f commit e5f080f

File tree

3 files changed

+20
-27
lines changed

3 files changed

+20
-27
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ script:
2424
- cd docs; make html
2525
# Run the test suit with coverage
2626
- cd ..
27-
- travis_wait 30 coverage run --source=axelrod -m unittest discover
27+
- travis_wait 60 coverage run --source=axelrod -m unittest discover
2828
- coverage report -m
2929
# Run the doctests
3030
- python doctests.py

docs/tutorials/contributing/strategy/writing_the_new_strategy.rst

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ The code
2222
--------
2323

2424
There are a couple of things that need to be created in a strategy.py file. Let
25-
us take a look at the :code:`TitForTat` class (located in the
25+
us take a look at the :class:`TitForTat
26+
<axelrod.strategies.titfortat.TitForTat>` class (located in the
2627
:code:`axelrod/strategies/titfortat.py` file)::
2728

2829
class TitForTat(Player):
@@ -101,7 +102,8 @@ This helps classify the strategy as described in
101102
:ref:`classification-of-strategies`.
102103

103104
After that the only thing required is to write the :code:`strategy` method
104-
which takes an opponent as an argument. In the case of :code:`TitForTat` the
105+
which takes an opponent as an argument. In the case of
106+
:class:`TitForTat <axelrod.strategies.titfortat.TitForTat>` the
105107
strategy checks if it has any history (:code:`if len(self.history) == 0`). If
106108
it does not (ie this is the first play of the match) then it returns :code:`C`.
107109
If not, the strategy simply repeats the opponent's last move (:code:`return
@@ -115,30 +117,21 @@ opponent.history[-1]`)::
115117
# Repeat the opponent's last move
116118
return opponent.history[-1]
117119

118-
The variables :code:`C` and :code:`D` represent the cooperate and defect actions respectively.
119-
120-
You can also modify the name of the strategy with the :code:`__repr__` method,
121-
which is invoked when :code:`str` is applied to a player instance. For example,
122-
the :code:`Random` strategy takes a parameter :code:`p` for how often it
123-
cooperates, and the :code:`__repr__` method adds the value of this parameter to
124-
the name::
125-
126-
def __repr__(self):
127-
return "%s: %s" % (self.name, round(self.p, 2))
128-
129-
Now we have separate names for different instantiations::
130-
131-
>>> import axelrod
132-
>>> player1 = axelrod.Random(p=0.5)
133-
>>> player2 = axelrod.Random(p=0.1)
134-
>>> player1
135-
Random: 0.5
136-
>>> player2
137-
Random: 0.1
138-
139-
This helps distinguish players in tournaments that have multiple instances of the
140-
same strategy. If you modify the :code:`__repr__` method of player, be sure to
141-
add an appropriate test.
120+
The variables :code:`C` and :code:`D` represent the cooperate and defect actions
121+
respectively.
122+
123+
Some strategies make specific use of the variables of a match to create their
124+
own attributes. In principle these attributes could change throughout a match
125+
or tournament if the match properties (like the game matrix) change, so we
126+
require that this logic live in the :code:`receive_match_attributes` method for
127+
correct dynamic updating. Here is how this is done for :class:`Stalker
128+
<axelrod.strategies.stalker.Stalker>`::
129+
130+
def receive_match_attributes(self)
131+
R, P, S, T = self.match_attributes["game"].RPST()
132+
self.very_good_score = R
133+
self.very_bad_score = P
134+
self.wish_score = (R + P) / 2
142135

143136
There are various examples of helpful functions and properties that make
144137
writing strategies easier. Do not hesitate to get in touch with the

training_data.csv

Whitespace-only changes.

0 commit comments

Comments
 (0)