Skip to content

Commit

Permalink
Merge pull request #659 from ranjinidas/master
Browse files Browse the repository at this point in the history
Added Slow TF2T
  • Loading branch information
marcharper authored Jul 16, 2016
2 parents bc33384 + b1a015a commit ed8798a
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
3 changes: 2 additions & 1 deletion axelrod/strategies/_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
from .titfortat import (
TitForTat, TitFor2Tats, TwoTitsForTat, Bully, SneakyTitForTat,
SuspiciousTitForTat, AntiTitForTat, HardTitForTat, HardTitFor2Tats,
OmegaTFT, Gradual, ContriteTitForTat)
OmegaTFT, Gradual, ContriteTitForTat, SlowTitForTwoTats)


# Note: Meta* strategies are handled in .__init__.py
Expand Down Expand Up @@ -166,6 +166,7 @@
Ripoff,
RiskyQLearner,
Shubik,
SlowTitForTwoTats,
SneakyTitForTat,
SoftGrudger,
SoftJoss,
Expand Down
33 changes: 33 additions & 0 deletions axelrod/strategies/titfortat.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,36 @@ def reset(self):
Player.reset(self)
self.contrite = False
self._recorded_history = []


class SlowTitForTwoTats(Player):
"""
A player plays C twice, then if the opponent plays the same move twice,
plays that move
"""

name = 'Slow Tit For Two Tats'
classifier = {
'memory_depth': 2,
'stochastic': False,
'makes_use_of': set(),
'inspects_source': False,
'manipulates_source': False,
'manipulates_state': False
}

def strategy(self, opponent):

#Start with two cooperations
if len(self.history) < 2:
return C

#Mimic if opponent plays the same move twice
if opponent.history[-2] == opponent.history[-1]:
return opponent.history[-1]

#Otherwise cooperate
return C



1 change: 1 addition & 0 deletions axelrod/tests/unit/test_punisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,4 @@ def test_reset_method(self):
self.assertEqual(P1.history, [])
self.assertEqual(P1.grudged, False)
self.assertEqual(P1.grudge_memory, 0)

27 changes: 27 additions & 0 deletions axelrod/tests/unit/test_titfortat.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,35 @@ def test_strategy_with_noise(self):
self.assertEqual(opponent.history, [C, D, D, D])
self.assertFalse(ctft.contrite)


def test_reset_cleans_all(self):
p = self.player()
p.contrite = True
p.reset()
self.assertFalse(p.contrite)

class TestSlowTitForTwoTats(TestPlayer):

name = "Slow Tit For Two Tats"
player = axelrod.SlowTitForTwoTats
expected_classifier = {
'memory_depth': 2,
'stochastic': False,
'makes_use_of': set(),
'inspects_source': False,
'manipulates_source': False,
'manipulates_state': False
}

def test_strategy(self):
"""Starts by cooperating."""
self.first_play_test(C)

def test_effect_of_strategy(self):
"""If opponent plays the same move twice, repeats last action of opponent history."""
self.responses_test([C]*2, [C, C], [C])
self.responses_test([C]*3, [C, D, C], [C])
self.responses_test([C]*3, [C, D, D], [D])



0 comments on commit ed8798a

Please sign in to comment.