Skip to content

Commit

Permalink
Merge pull request #993 from eric-s-s/884-grudger
Browse files Browse the repository at this point in the history
884 grudger - with 1 to 3 issues
  • Loading branch information
meatballs authored Apr 30, 2017
2 parents b898304 + 4351ca0 commit 76ebcaa
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 116 deletions.
32 changes: 17 additions & 15 deletions axelrod/strategies/grudger.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ def __init__(self) -> None:
def strategy(self, opponent: Player) -> Action:
"""Begins by playing C, then plays D for mem_length rounds if the
opponent ever plays D."""
if self.grudge_memory >= self.mem_length:
if self.grudge_memory == self.mem_length:
self.grudge_memory = 0
self.grudged = False

if D in opponent.history[-1:]:
self.grudged = True

if self.grudged:
self.grudge_memory += 1
return D
elif D in opponent.history[-1:]:
self.grudged = True
return D
return C

def reset(self):
Expand Down Expand Up @@ -206,9 +206,9 @@ def strategy(self, opponent: Player) -> Action:
"""Begins by playing C, then plays Alternator for the remaining rounds
if the opponent ever plays D."""
if opponent.defections:
if self.history[-1] == Actions.C:
return Actions.D
return Actions.C
if self.history[-1] == C:
return D
return C


class EasyGo(Player):
Expand Down Expand Up @@ -240,13 +240,14 @@ def strategy(opponent: Player) -> Action:
return C
return D


class GeneralSoftGrudger(Player):
"""
A generalization of the SoftGrudger strategy. SoftGrudger punishes by playing:
D, D, D, D, C, C. after a defection by the opponent. GeneralSoftGrudger
only punishes after its opponent defects a specified amount of times consecutively.
The punishment is in the form of a series of defections followed by a 'penance' of
a series of consecutive cooperations.
A generalization of the SoftGrudger strategy. SoftGrudger punishes by
playing: D, D, D, D, C, C. after a defection by the opponent.
GeneralSoftGrudger only punishes after its opponent defects a specified
amount of times consecutively. The punishment is in the form of a series of
defections followed by a 'penance' of a series of consecutive cooperations.
Names:
Expand All @@ -268,11 +269,11 @@ def __init__(self, n: int=1, d: int=4, c: int=2) -> None:
"""
Parameters
----------
n, int
n: int
The number of defections by the opponent to trigger punishment
d, int
d: int
The number of defections to punish the opponent
c, int
c: int
The number of cooperations in the 'penance' stage
Special Cases
Expand Down Expand Up @@ -303,6 +304,7 @@ def strategy(self, opponent: Player) -> Action:
elif [D] * self.n == opponent.history[-self.n:]:
self.grudged = True
return D

return C

def reset(self):
Expand Down
Loading

0 comments on commit 76ebcaa

Please sign in to comment.