Skip to content

Commit e0dad7a

Browse files
committed
Added new SpitefulCC Player to grudger.py, updated the list of all strategies and added coverage test in test_grudger.py
1 parent c89df5f commit e0dad7a

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
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: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Grudger(Player):
2323

2424
name = "Grudger"
2525
classifier = {
26-
"memory_depth": float('inf'),
26+
"memory_depth": float("inf"),
2727
"stochastic": False,
2828
"long_run_time": False,
2929
"inspects_source": False,
@@ -309,3 +309,32 @@ def strategy(self, opponent: Player) -> Action:
309309

310310
def __repr__(self) -> str:
311311
return "%s: n=%s,d=%s,c=%s" % (self.name, self.n, self.d, self.c)
312+
313+
314+
class SpitefulCC(Player):
315+
"""
316+
Behaves like Grudger after cooperating for 2 turns
317+
318+
Names:
319+
320+
- spiteful_cc: [Mathieu2015]_
321+
"""
322+
323+
name = "SpitefulCC"
324+
classifier = {
325+
"memory_depth": float("inf"), # Long memory
326+
"stochastic": False,
327+
"makes_use_of": set(),
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+
if len(opponent.history) < 2:
337+
return C
338+
elif opponent.defections:
339+
return D
340+
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)

0 commit comments

Comments
 (0)