Skip to content

Commit d535a74

Browse files
Merge pull request #1364 from RomeroLaura/SpitefulCC
SpitefulCC from [Mathieu2015]
2 parents bf42a8b + 43930e8 commit d535a74

File tree

5 files changed

+69
-5
lines changed

5 files changed

+69
-5
lines changed

axelrod/strategies/_strategies.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
GrudgerAlternator,
143143
OppositeGrudger,
144144
SoftGrudger,
145+
SpitefulCC,
145146
)
146147
from .grumpy import Grumpy
147148
from .handshake import Handshake
@@ -467,6 +468,7 @@
467468
SolutionB1,
468469
SolutionB5,
469470
SpitefulTitForTat,
471+
SpitefulCC,
470472
Stalker,
471473
StochasticCooperator,
472474
StochasticWSLS,

axelrod/strategies/grudger.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Grudger(Player):
2424

2525
name = "Grudger"
2626
classifier = {
27-
"memory_depth": float('inf'),
27+
"memory_depth": float("inf"),
2828
"stochastic": False,
2929
"long_run_time": False,
3030
"inspects_source": False,
@@ -310,3 +310,35 @@ def strategy(self, opponent: Player) -> Action:
310310

311311
def __repr__(self) -> str:
312312
return "%s: n=%s,d=%s,c=%s" % (self.name, self.n, self.d, self.c)
313+
314+
315+
class SpitefulCC(Player):
316+
"""
317+
Behaves like Grudger after cooperating for 2 turns
318+
319+
Names:
320+
321+
- spiteful_cc: [Mathieu2015]_
322+
"""
323+
324+
name = "SpitefulCC"
325+
classifier = {
326+
"memory_depth": float("inf"), # Long memory
327+
"stochastic": False,
328+
"long_run_time": False,
329+
"inspects_source": False,
330+
"manipulates_source": False,
331+
"manipulates_state": False,
332+
}
333+
334+
@staticmethod
335+
def strategy(opponent: Player) -> Action:
336+
"""
337+
Cooperates until the oponent defects, then defects forever.
338+
Always cooperates twice at the start.
339+
"""
340+
if len(opponent.history) < 2:
341+
return C
342+
elif opponent.defections:
343+
return D
344+
return C

axelrod/tests/strategies/test_grudger.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class TestGrudger(TestPlayer):
1212
name = "Grudger"
1313
player = axl.Grudger
1414
expected_classifier = {
15-
"memory_depth": float('inf'),
15+
"memory_depth": float("inf"),
1616
"stochastic": False,
1717
"makes_use_of": set(),
1818
"long_run_time": False,
@@ -276,3 +276,34 @@ def test_strategy(self):
276276
expected_actions=actions,
277277
init_kwargs={"n": 1, "d": 1, "c": 1},
278278
)
279+
280+
281+
class TestSpitefulCC(TestPlayer):
282+
283+
name = "SpitefulCC"
284+
player = axl.SpitefulCC
285+
expected_classifier = {
286+
"memory_depth": float("inf"), # Long memory
287+
"stochastic": False,
288+
"makes_use_of": set(),
289+
"long_run_time": False,
290+
"inspects_source": False,
291+
"manipulates_source": False,
292+
"manipulates_state": False,
293+
}
294+
295+
def test_strategy(self):
296+
# If opponent defects at any point then the player will defect forever.
297+
# Cooperates for the first 2 turns.
298+
opponent = axl.Cooperator()
299+
actions = [(C, C)] * 20
300+
self.versus_test(opponent, expected_actions=actions)
301+
302+
opponent = axl.Defector()
303+
actions = [(C, D)] * 2 + [(D, D)] * 20
304+
self.versus_test(opponent, expected_actions=actions)
305+
306+
opponent_actions = [D] * 20 + [C] * 20
307+
opponent = axl.MockPlayer(actions=opponent_actions)
308+
actions = [(C, D)] * 2 + [(D, D)] * 18 + [(D, C)] * 20
309+
self.versus_test(opponent, expected_actions=actions)

axelrod/tests/strategies/test_meta.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ def classifier_test(self, expected_class_classifier=None):
638638
pass
639639

640640
def test_strategy(self):
641-
actions = [(C, C), (C, D), (D, C), (D, D), (D, C)]
641+
actions = [(C, C), (C, D), (C, C), (D, D), (D, C)]
642642
self.versus_test(opponent=axl.Alternator(), expected_actions=actions, seed=11)
643643

644644

docs/index.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Count the number of available players::
5353

5454
>>> import axelrod as axl
5555
>>> len(axl.strategies)
56-
236
56+
237
5757

5858
Create matches between two players::
5959

@@ -111,4 +111,3 @@ Indices and tables
111111
* :ref:`genindex`
112112
* :ref:`modindex`
113113
* :ref:`search`
114-

0 commit comments

Comments
 (0)