Skip to content

Commit 9d6ac1d

Browse files
committed
GeneralSoftGrudger changed to cover edge cases. edge case tests included
1 parent d2ed293 commit 9d6ac1d

File tree

2 files changed

+59
-9
lines changed

2 files changed

+59
-9
lines changed

axelrod/strategies/grudger.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ def __init__(self) -> None:
6363
def strategy(self, opponent: Player) -> Action:
6464
"""Begins by playing C, then plays D for mem_length rounds if the
6565
opponent ever plays D."""
66-
if self.grudge_memory >= self.mem_length:
66+
if self.grudge_memory == self.mem_length:
6767
self.grudge_memory = 0
6868
self.grudged = False
6969

70-
if not self.grudged and D in opponent.history[-1:]:
70+
if D in opponent.history[-1:]:
7171
self.grudged = True
7272

7373
if self.grudged:
@@ -284,7 +284,7 @@ def __init__(self, n: int=1, d: int=4, c: int=2) -> None:
284284
self.n = n
285285
self.d = d
286286
self.c = c
287-
self.grudge = [D] * (d - 1) + [C] * c
287+
self.grudge = [D] * d + [C] * c
288288
self.grudged = False
289289
self.grudge_memory = 0
290290

@@ -294,16 +294,18 @@ def strategy(self, opponent: Player) -> Action:
294294
The punishment is in the form of 'd' defections followed by a penance of
295295
'c' consecutive cooperations.
296296
"""
297+
if self.grudge_memory == len(self.grudge):
298+
self.grudged = False
299+
self.grudge_memory = 0
300+
301+
if [D] * self.n == opponent.history[-self.n:] or self.n == 0:
302+
self.grudged = True
303+
297304
if self.grudged:
298305
strategy = self.grudge[self.grudge_memory]
299306
self.grudge_memory += 1
300-
if self.grudge_memory == len(self.grudge):
301-
self.grudged = False
302-
self.grudge_memory = 0
303307
return strategy
304-
elif [D] * self.n == opponent.history[-self.n:]:
305-
self.grudged = True
306-
return D
308+
307309
return C
308310

309311
def reset(self):

axelrod/tests/strategies/test_grudger.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ def test_set_n(self):
288288
init_kwargs=init_kwargs)
289289

290290
def test_set_d_and_c(self):
291+
# Sets the number of D's then C's in the grudge response.
291292
init_kwargs = {'d': 3, 'c': 3}
292293
grudge_response_d = [(D, D)] * 3 + [(C, D)] * 3
293294
grudge_response_c = [(D, C)] * 3 + [(C, C)] * 3
@@ -305,6 +306,53 @@ def test_set_d_and_c(self):
305306
self.versus_test(opponent, expected_actions=actions,
306307
init_kwargs=init_kwargs)
307308

309+
def test_edge_case_n_is_zero(self):
310+
# Always uses grudge response.
311+
init_kwargs = {'n': 0}
312+
grudge_response_d = [(D, D)] * 4 + [(C, D)] * 2
313+
grudge_response_c = [(D, C)] * 4 + [(C, C)] * 2
314+
315+
opponent = axl.Defector()
316+
actions = grudge_response_d * 5
317+
self.versus_test(opponent, expected_actions=actions,
318+
init_kwargs=init_kwargs)
319+
320+
opponent = axl.Cooperator()
321+
actions = grudge_response_c * 5
322+
self.versus_test(opponent, expected_actions=actions,
323+
init_kwargs=init_kwargs)
324+
325+
def test_edge_case_d_is_zero(self):
326+
# Grudge response is only C, so acts like Cooperator.
327+
init_kwargs = {'d': 0}
328+
329+
opponent = axl.Defector()
330+
actions = [(C, D)] * 5
331+
self.versus_test(opponent, expected_actions=actions,
332+
init_kwargs=init_kwargs)
333+
334+
opponent = axl.Alternator()
335+
actions = [(C, C), (C, D)] * 5
336+
self.versus_test(opponent, expected_actions=actions,
337+
init_kwargs=init_kwargs)
338+
339+
def test_edge_case_c_is_zero(self):
340+
# Grudge response is a set number of D's (defaults to 4)
341+
init_kwargs = {'c': 0}
342+
343+
opponent = axl.Defector()
344+
actions = [(C, D)] + [(D, D)] * 10
345+
self.versus_test(opponent, expected_actions=actions,
346+
init_kwargs=init_kwargs)
347+
348+
opponent_actions = [C] * 10 + [D]
349+
opponent = axl.MockPlayer(actions=opponent_actions)
350+
actions_start = [(C, C)] * 10 + [(C, D)]
351+
subsequent = [(D, C)] * 4 + [(C, C)] * 6 + [(C, D)]
352+
actions = actions_start + subsequent * 5
353+
self.versus_test(opponent, expected_actions=actions,
354+
init_kwargs=init_kwargs)
355+
308356
def test_repr(self):
309357
default_player = self.player()
310358
self.assertEqual(repr(default_player),

0 commit comments

Comments
 (0)