Skip to content

Commit 888b299

Browse files
drvinceknightmarcharper
authored andcommitted
Fix off by one errors and fix tests.
1 parent 5af8d2a commit 888b299

File tree

2 files changed

+76
-26
lines changed

2 files changed

+76
-26
lines changed

axelrod/strategies/axelrod_first.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ def strategy(self, opponent: Player) -> Action:
257257
if not self.history:
258258
return C
259259
# React to the opponent's last move
260-
if len(self.history) <= 56:
261-
if opponent.history[-1] == D or len(self.history) == 51:
260+
if len(self.history) < 56:
261+
if opponent.history[-1] == D or len(self.history) == 50:
262262
return D
263263
return C
264264

@@ -270,9 +270,9 @@ def strategy(self, opponent: Player) -> Action:
270270
return D
271271
if all(opponent.history[i] == self.history[i - 1] for i in range(1, len(self.history))):
272272
# Check if opponent plays Tit for Tat
273-
if opponent.history[-1] == D:
274-
return D
275-
return C
273+
if opponent.history[-1] == D:
274+
return D
275+
return C
276276

277277
if self.next_random_defection_turn is None:
278278
self.next_random_defection_turn = random.randint(5, 15) + len(self.history)

axelrod/tests/strategies/test_axelrod_first.py

Lines changed: 71 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -149,36 +149,86 @@ class TestGraaskamp(TestPlayer):
149149
name = "Graaskamp: 0.05"
150150
player = axelrod.Graaskamp
151151
expected_classifier = {
152-
'memory_depth': float("inf"),
153-
'stochastic': True,
154-
'makes_use_of': set(),
155-
'inspects_source': False,
156-
'manipulates_source': False,
157-
'manipulates_state': False
152+
"memory_depth": float("inf"),
153+
"stochastic": True,
154+
"makes_use_of": set(),
155+
"inspects_source": False,
156+
"manipulates_source": False,
157+
"manipulates_state": False,
158158
}
159159

160160
def test_strategy(self):
161-
# Play against opponents
162-
actions = [(C, D), (D, C), (C, C), (C, C), (C, D), (D, D)]
163-
self.versus_test(axelrod.Random(), expected_actions=actions, seed=0)
161+
# Test TfT in first 50 rounds followed by defection followed by 5 rounds
162+
# of TfT
163+
expected_attrs = {
164+
"opponent_is_random": False,
165+
"next_random_defection_turn": None,
166+
}
167+
168+
# Against alternator
169+
actions = [(C, C)] + [(C, D), (D, C)] * 24 + [(C, D)] # 50 turns
170+
actions += [(D, C)] # 51 turns
171+
actions += [(C, D), (D, C)] * 2 + [(C, D)] # 56 turns
172+
self.versus_test(
173+
axelrod.Alternator(), expected_actions=actions, attrs=expected_attrs
174+
)
164175

165-
actions = [(C, C), (C, C), (C, D), (D, C), (C, D), (D, D)]
166-
self.versus_test(axelrod.Random(), expected_actions=actions, seed=1)
176+
# Against defector
177+
actions = [(C, D)] + [(D, D)] * 55 # 56 turns
178+
self.versus_test(
179+
axelrod.Defector(), expected_actions=actions, attrs=expected_attrs
180+
)
167181

168-
actions = [(C, C), (C, D), (D, C), (C, D), (D, C)]
169-
self.versus_test(axelrod.Alternator(), expected_actions=actions)
182+
# Against cooperator
183+
actions = [(C, C)] * 50 + [(D, C)] + [(C, C)] * 5
184+
self.versus_test(
185+
axelrod.Cooperator(), expected_actions=actions, attrs=expected_attrs
186+
)
170187

171-
actions = [(C, C) * 50, (D, C), (C, C) * 5]
172-
self.versus_test(axelrod.Cooperator(), expected_actions=actions)
188+
# Test recognition of random player
173189

174-
actions = [(C, D), (D, D) * 49, (D, D) * 6]
175-
self.versus_test(axelrod.Defector(), expected_actions=actions)
190+
expected_attrs = {
191+
"opponent_is_random": False,
192+
"next_random_defection_turn": None,
193+
}
194+
actions = [(C, C)] * 50 + [(D, C)] + [(C, C)] * 5 # 56 turns
195+
self.versus_test(
196+
axelrod.Cooperator(), expected_actions=actions, attrs=expected_attrs
197+
)
198+
expected_attrs = {"opponent_is_random": False, "next_random_defection_turn": 68}
199+
actions += [(C, C)] # 57 turns
200+
self.versus_test(
201+
axelrod.Cooperator(), expected_actions=actions, attrs=expected_attrs
202+
)
203+
204+
expected_attrs = {
205+
"opponent_is_random": True,
206+
"next_random_defection_turn": None,
207+
}
208+
actions = [(C, C)] + [(C, D), (D, C)] * 24 + [(C, D)] # 50 turns
209+
actions += [(D, C)] # 51 turns
210+
actions += [(C, D), (D, C)] * 3 # 57 turns
211+
actions += [(D, D)]
212+
self.versus_test(
213+
axelrod.Alternator(), expected_actions=actions, attrs=expected_attrs
214+
)
215+
actions += [(D, C), (D, D)] * 5
216+
self.versus_test(
217+
axelrod.Alternator(), expected_actions=actions, attrs=expected_attrs
218+
)
176219

177-
actions = [(C, C) * 50, (D, C), (C, C) * 5]
178-
self.versus_test(axelrod.Grumpy(), expected_actions=actions)
220+
# Test random defections
221+
expected_attrs = {"opponent_is_random": False, "next_random_defection_turn": 78}
222+
actions = [(C, C)] * 50 + [(D, C)] + [(C, C)] * 16 + [(D, C)] + [(C, C)]
223+
self.versus_test(
224+
axelrod.Cooperator(), expected_actions=actions, seed=0, attrs=expected_attrs
225+
)
179226

180-
actions = [(C, C) * 50, (D, C), (C, D) * 5]
181-
self.versus_test(axelrod.Grudger(), expected_actions=actions)
227+
expected_attrs = {"opponent_is_random": False, "next_random_defection_turn": 77}
228+
actions = [(C, C)] * 50 + [(D, C)] + [(C, C)] * 12 + [(D, C)] + [(C, C)]
229+
self.versus_test(
230+
axelrod.Cooperator(), expected_actions=actions, seed=1, attrs=expected_attrs
231+
)
182232

183233

184234
class TestGrofman(TestPlayer):

0 commit comments

Comments
 (0)