Skip to content

Commit 8e5622b

Browse files
committed
Fix strategy and adjust tests.
1 parent 206b414 commit 8e5622b

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

axelrod/strategies/titfortat.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -642,10 +642,10 @@ def strategy(self, opponent: Player) -> Action:
642642
class Alexei(Player):
643643
"""
644644
Plays similar to Tit-for-Tat, but always defect on last turn.
645-
645+
646646
Names:
647647
648-
- Alexei's Strategy: [LessWrong2011]_
648+
- Alexei's Strategy: [LessWrong2011]_
649649
"""
650650

651651
name = 'Alexei'
@@ -672,10 +672,10 @@ class EugineNier(Player):
672672
Plays similar to Tit-for-Tat, but with two conditions:
673673
1) Always Defect on Last Move
674674
2) If other player defects five times, switch to all defects.
675-
675+
676676
Names:
677677
678-
- EugineNier Strategy: [LessWrong2011]_
678+
- EugineNier Strategy: [LessWrong2011]_
679679
"""
680680

681681
name = 'EugineNier'
@@ -689,11 +689,18 @@ class EugineNier(Player):
689689
'manipulates_state': False
690690
}
691691

692+
def __init__(self):
693+
super().__init__()
694+
self.is_defector = False
695+
692696
def strategy(self, opponent: Player) -> Action:
693697
if not self.history:
694698
return C
695-
if opponent.history[-5:] == [D, D, D, D, D]:
696-
return D
697-
if opponent.history[-1] == D:
699+
if self.is_defector or opponent.history[-5:] == [D] * 5:
700+
self.is_defector = True
698701
return D
699-
return C
702+
return opponent.history[-1]
703+
704+
def reset(self):
705+
super().reset()
706+
self.is_defector = False

axelrod/tests/strategies/test_titfortat.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ def test_strategy(self):
593593
actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (C, D)]
594594
self.versus_test(axelrod.Alternator(), expected_actions=actions,
595595
match_attributes={"length": -1})
596-
596+
597597
actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (D, D)]
598598
self.versus_test(axelrod.Alternator(), expected_actions=actions)
599599

@@ -621,23 +621,27 @@ def test_strategy(self):
621621
self.first_play_test(C)
622622
self.second_play_test(rCC=C, rCD=D, rDC=C, rDD=D)
623623

624-
actions = [(C, C), (C, C), (C, C), (C, C), (D, C)]
624+
actions = [(C, C), (C, C), (C, C), (D, C)]
625625
self.versus_test(axelrod.Cooperator(), expected_actions=actions)
626626

627-
actions = [(C, D), (D, D), (D, D), (D, D), (D, D)]
628-
self.versus_test(axelrod.Defector(), expected_actions=actions)
627+
actions = [(C, C), (C, C), (C, C), (C, C)]
628+
self.versus_test(axelrod.Cooperator(), expected_actions=actions,
629+
match_attributes={"length": -1})
630+
631+
632+
# Plays TfT and defects in last round
633+
actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (D, D)]
634+
self.versus_test(axelrod.Alternator(), expected_actions=actions)
629635

630636
actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (C, D)]
631637
self.versus_test(axelrod.Alternator(), expected_actions=actions,
632638
match_attributes={"length": -1})
633-
639+
634640
actions = [(C, C), (C, D), (D, C), (C, D), (D, C), (D, D)]
635641
self.versus_test(axelrod.Alternator(), expected_actions=actions)
636642

637-
opponent = axelrod.MockPlayer(actions=[C, C, D, D, C, D])
638-
actions = [(C, C), (C, C), (C, D), (D, D), (D, C), (D, D)]
639-
self.versus_test(opponent, expected_actions=actions)
640-
641-
opponent = axelrod.MockPlayer(actions=[C, D, D, D, D, D])
642-
actions = [(C, C), (D, D), (D, D), (D, D), (D, D), (D, D)]
643+
# Becomes defector after 5 defections
644+
opponent = axelrod.MockPlayer(actions=[C, D, D, D, D, D, C, C])
645+
actions = [(C, C), (C, D), (D, D), (D, D),
646+
(D, D), (D, D), (D, C), (D, C)]
643647
self.versus_test(opponent, expected_actions=actions)

0 commit comments

Comments
 (0)