Skip to content

Commit 4409a46

Browse files
RomeroLauradrvinceknight
authored andcommitted
Added new SpitefulCC Player to grudger.py, updated the list of all strategies and added coverage test in test_grudger.py
1 parent 7cbe2d7 commit 4409a46

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,3 +310,32 @@ 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+
"makes_use_of": set(),
329+
"long_run_time": False,
330+
"inspects_source": False,
331+
"manipulates_source": False,
332+
"manipulates_state": False,
333+
}
334+
335+
@staticmethod
336+
def strategy(opponent: Player) -> Action:
337+
if len(opponent.history) < 2:
338+
return C
339+
elif opponent.defections:
340+
return D
341+
return C

axelrod/tests/strategies/test_grudger.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,34 @@ def test_strategy(self):
305305
expected_actions=actions,
306306
init_kwargs={"n": 1, "d": 1, "c": 1},
307307
)
308+
309+
310+
class TestSpitefulCC(TestPlayer):
311+
312+
name = "SpitefulCC"
313+
player = axl.SpitefulCC
314+
expected_classifier = {
315+
"memory_depth": float("inf"), # Long memory
316+
"stochastic": False,
317+
"makes_use_of": set(),
318+
"long_run_time": False,
319+
"inspects_source": False,
320+
"manipulates_source": False,
321+
"manipulates_state": False,
322+
}
323+
324+
def test_strategy(self):
325+
# If opponent defects at any point then the player will defect forever.
326+
# Cooperates for the first 2 turns.
327+
opponent = axl.Cooperator()
328+
actions = [(C, C)] * 20
329+
self.versus_test(opponent, expected_actions=actions)
330+
331+
opponent = axl.Defector()
332+
actions = [(C, D)] * 2 + [(D, D)] * 20
333+
self.versus_test(opponent, expected_actions=actions)
334+
335+
opponent_actions = [D] * 20 + [C] * 20
336+
opponent = axl.MockPlayer(actions=opponent_actions)
337+
actions = [(C, D)] * 2 + [(D, D)] * 18 + [(D, C)] * 20
338+
self.versus_test(opponent, expected_actions=actions)

0 commit comments

Comments
 (0)