@@ -22,7 +22,8 @@ The code
22
22
--------
23
23
24
24
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
26
27
:code: `axelrod/strategies/titfortat.py ` file)::
27
28
28
29
class TitForTat(Player):
@@ -101,7 +102,8 @@ This helps classify the strategy as described in
101
102
:ref: `classification-of-strategies `.
102
103
103
104
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
105
107
strategy checks if it has any history (:code: `if len(self.history) == 0 `). If
106
108
it does not (ie this is the first play of the match) then it returns :code: `C `.
107
109
If not, the strategy simply repeats the opponent's last move (:code: `return
@@ -115,30 +117,21 @@ opponent.history[-1]`)::
115
117
# Repeat the opponent's last move
116
118
return opponent.history[-1]
117
119
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
142
135
143
136
There are various examples of helpful functions and properties that make
144
137
writing strategies easier. Do not hesitate to get in touch with the
0 commit comments