Skip to content

Commit 08bce9a

Browse files
committed
Merge pull request #1 from langner/ATFT
Simplify strategy code for most TFT strategies
2 parents 010c23f + c222802 commit 08bce9a

File tree

1 file changed

+11
-21
lines changed

1 file changed

+11
-21
lines changed

axelrod/strategies/titfortat.py

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,29 @@ class TitForTat(Player):
88
name = 'Tit For Tat'
99

1010
def strategy(self, opponent):
11-
try:
12-
return opponent.history[-1]
13-
except IndexError:
14-
return 'C'
11+
return 'D' if opponent.history[-1:] == ['D'] else 'C'
1512

1613
class TitFor2Tats(Player):
1714
"""A player starts by cooperating and then defects only after two defects by opponent."""
1815

1916
name = "Tit For 2 Tats"
2017

2118
def strategy(self, opponent):
22-
if opponent.history[-2:] == ['D', 'D']:
23-
return 'D'
24-
return 'C'
19+
return 'D' if opponent.history[-2:] == ['D', 'D'] else 'C'
2520

2621
class TwoTitsForTat(Player):
2722
"""A player starts by cooperating and replies to each defect by two defections."""
2823

2924
name = "Two Tits For Tat"
3025

3126
def strategy(self, opponent):
32-
if 'D' in opponent.history[-2:]:
33-
return 'D'
34-
return 'C'
27+
return 'D' if 'D' in opponent.history[-2:] else 'C'
3528

3629
class Bully(Player):
37-
"""A player that behaves opposite to Tit For Tat.
30+
"""A player that behaves opposite to Tit For Tat, including first move.
3831
3932
Starts by defecting and then does the opposite of opponent's previous move.
40-
This the opposite of TIT FOR TAT, also sometimes called BULLY.
33+
This the complete opposite of TIT FOR TAT, also called BULLY in literature.
4134
"""
4235

4336
name = "Bully"
@@ -65,18 +58,15 @@ class SuspiciousTitForTat(Player):
6558
name = "Suspicious Tit For Tat"
6659

6760
def strategy(self, opponent):
68-
try:
69-
return opponent.history[-1]
70-
except IndexError:
71-
return 'D'
61+
return 'C' if opponent.history[-1:] == ['C'] else 'D'
7262

7363
class AntiTitForTat(Player):
74-
"""A strategy that always plays the opposite of the opponents previous move."""
64+
"""A strategy that plays the opposite of the opponents previous move.
65+
66+
This is similar to BULLY above, except that the first move is cooperation.
67+
"""
7568

7669
name = 'Anti Tit For Tat'
7770

7871
def strategy(self, opponent):
79-
try:
80-
return flip_dict[opponent.history[-1]]
81-
except IndexError:
82-
return 'C'
72+
return 'D' if opponent.history[-1:] == ['C'] else 'C'

0 commit comments

Comments
 (0)