@@ -8,36 +8,29 @@ class TitForTat(Player):
8
8
name = 'Tit For Tat'
9
9
10
10
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'
15
12
16
13
class TitFor2Tats (Player ):
17
14
"""A player starts by cooperating and then defects only after two defects by opponent."""
18
15
19
16
name = "Tit For 2 Tats"
20
17
21
18
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'
25
20
26
21
class TwoTitsForTat (Player ):
27
22
"""A player starts by cooperating and replies to each defect by two defections."""
28
23
29
24
name = "Two Tits For Tat"
30
25
31
26
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'
35
28
36
29
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 .
38
31
39
32
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 .
41
34
"""
42
35
43
36
name = "Bully"
@@ -65,18 +58,15 @@ class SuspiciousTitForTat(Player):
65
58
name = "Suspicious Tit For Tat"
66
59
67
60
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'
72
62
73
63
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
+ """
75
68
76
69
name = 'Anti Tit For Tat'
77
70
78
71
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