Skip to content

Commit dcccd15

Browse files
author
margaret
committed
Add Prober4 class
The class implements prober4 strategy from PRISON project.
1 parent deb7bad commit dcccd15

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

axelrod/strategies/prober.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,57 @@ def strategy(self, opponent):
102102
return D if opponent.history[-1:] == [D] else C
103103

104104

105+
class Prober4(Player):
106+
"""
107+
Plays fixed sequence of 20 moves initially.
108+
If the opponent played D for D at least 3 times more than D for C,
109+
defects forever.
110+
Otherwise cooperates for the next 5 moves, and plays TFT afterwards.
111+
"""
112+
113+
name = 'Prober 4'
114+
115+
def __init__(self):
116+
self.probe_sequence = [C, C, D, C, D, D, D, C, C, D,
117+
C, D, C, C, D, C, D, D, C, D]
118+
self.probe_lenght = len(self.probe_sequence)
119+
self.politness_pool = [C, C, C, C, C]
120+
self.is_angry = False
121+
122+
def strategy(self, opponent):
123+
turn = len(self.history)
124+
if turn <= self.probe_length:
125+
return self.probe_sequence[turn - 1]
126+
if turn == self.probe_length + 1:
127+
self.judge(opponent)
128+
if self.angry:
129+
return D
130+
else:
131+
# Cooperate for the next 5 turns
132+
if self.politness_pool:
133+
return self.politness_pool.pop()
134+
else:
135+
# TFT
136+
return D if opponent.history[-1:] == [D] else C
137+
138+
def judge(self, opponent):
139+
just_defect = 0
140+
unjust_defect = 0
141+
for turn in range(self.probe_lenght):
142+
if opponent.history[turn + 1] == D:
143+
if self.history[turn] == C:
144+
unjust_defect += 1
145+
if self.history[turn] == D:
146+
just_defect += 1
147+
if just_defect - unjust_defect >= 3:
148+
self.is_angry = True
149+
150+
def reset(self):
151+
Player.reset(self)
152+
self.politeness_pool = [C, C, C, C, C]
153+
self.is_angry = False
154+
155+
105156
class HardProber(Player):
106157
"""
107158
Plays D, D, C, C initially. Defects forever if opponent cooperated in moves

0 commit comments

Comments
 (0)