You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/contributing.rst
+23-29Lines changed: 23 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -95,18 +95,24 @@ For example there is strategy with :math:`\pi` as a name::
95
95
96
96
name = '$\pi$'
97
97
98
-
After that the only thing required is to write the :code:`strategy` method which takes an opponent as an argument.
99
-
In the case of :code:`TitForTat` the strategy attempts to play the same thing as the last strategy played by the opponent (:code:`opponent.history[-1]`) and if this is not possible (in other words the opponent has not played yet) will cooperate::
98
+
After that the only thing required is to write the :code:`strategy` method which
99
+
takes an opponent as an argument. In the case of :code:`TitForTat` the strategy
100
+
attempts to play the same thing as the last strategy played by the opponent
101
+
(:code:`opponent.history[-1]`) and if this is not possible (in other words the
102
+
opponent has not played yet) will cooperate::
100
103
101
104
def strategy(self, opponent):
102
105
try:
103
106
return opponent.history[-1]
104
107
except IndexError:
105
108
return 'C'
106
109
107
-
If your strategy creates any particular attribute along the way you need to make sure that there is a :code:`reset` method that takes account of it.
108
-
An example of this is the :code:`ForgetfulGrudger` strategy which creates a boolean variable :code:`grudged` and a counter :code:`grudge_memory` which keeps track of things during a duel.
109
-
Here is the :code:`reset` method which takes care of resetting this in between rounds::
110
+
If your strategy creates any particular attribute along the way you need to make
111
+
sure that there is a :code:`reset` method that takes account of it. An example
112
+
of this is the :code:`ForgetfulGrudger` strategy which creates a boolean
113
+
variable :code:`grudged` and a counter :code:`grudge_memory` which keeps track
114
+
of things during a duel. Here is the :code:`reset` method which takes care of
115
+
resetting this in between rounds::
110
116
111
117
def reset(self):
112
118
"""Resets scores and history."""
@@ -115,10 +121,10 @@ Here is the :code:`reset` method which takes care of resetting this in between r
115
121
self.grudge_memory = 0
116
122
117
123
118
-
You can also modify the name of the strategy with the `__repr__` method, which is invoked
119
-
when `str` is applied to a player instance. For example, the player `Random` takes a
120
-
parameter `p` for how often it cooperates, and the `__repr__` method adds the value
121
-
of this parameter to the name::
124
+
You can also modify the name of the strategy with the `__repr__` method, which
125
+
is invoked when `str` is applied to a player instance. For example, the player
126
+
`Random` takes a parameter `p` for how often it cooperates, and the `__repr__`
127
+
method adds the value of this parameter to the name::
122
128
123
129
def __repr__(self):
124
130
return "%s: %s" % (self.name, round(self.p, 2))
@@ -140,6 +146,10 @@ This helps distinguish players in tournaments that have multiple instances of th
140
146
same strategy. If you modify the `__repr__` method of player, be sure to add an
141
147
appropriate test.
142
148
149
+
There is also a behaviour dictionary that allows for easy classification of
150
+
strategies: take a look at the `Strategy Behaviour`_ section for more
151
+
information.
152
+
143
153
144
154
Adding the strategy to the library
145
155
''''''''''''''''''''''''''''''''''
@@ -156,27 +166,10 @@ So for the :code:`TitForTat` strategy which is written in the :code:`titfortat.p
156
166
157
167
from titfortat import *
158
168
159
-
Once you have done that (**and you need to do this even if you have added a strategy to an already existing file**), you need to add the class itself to one of the following lists::
160
-
161
-
basic_strategies
162
-
ordinary_strategies
163
-
cheating_strategies
164
-
165
-
You will most probably be adding the strategy to one of :code:`ordinary_strategies` or :code:`cheating_strategies`.
166
-
If you are unsure take a look at the section: `Strategy behaviour`_.
167
-
168
-
For :code:`TitForTat` this looks like::
169
-
170
-
basic_strategies = [
171
-
Alternator,
172
-
Cooperator,
173
-
Defector,
174
-
Random,
175
-
TitForTat,
176
-
]
169
+
Once you have done that (**and you need to do this even if you have added a
170
+
strategy to an already existing file**), you need to add the class itself to one
171
+
of the :code:`strategies` list.
177
172
178
-
Note that :code:`TitForTat` is here added to the :code:`basic_strategies` list.
179
-
If you would like to check if your strategy is honest, read the next section, if you would like to take a look at how to write tests please skip to `How to write tests`_ (again though if you need a hand with testing please let us know!).
180
173
181
174
Strategy behaviour
182
175
''''''''''''''''''
@@ -205,6 +198,7 @@ the initialisation depending on input parameters. A good example of this is the
0 commit comments