Skip to content

Commit 3581045

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 5dd874d commit 3581045

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
@@ -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,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: 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)