Skip to content

Commit df394b4

Browse files
author
margaret
committed
Simplify Prober4
Calculate defections on the fly. Place the strategy logic in a single function.
1 parent 0e6b7d5 commit df394b4

File tree

2 files changed

+52
-30
lines changed

2 files changed

+52
-30
lines changed

axelrod/strategies/prober.py

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class Prober4(Player):
113113
name = 'Prober 4'
114114
classifier = {
115115
'stochastic': False,
116-
'memory_depth': 20,
116+
'memory_depth': 1,
117117
'makes_use_of': set(),
118118
'long_run_time': False,
119119
'inspects_source': False,
@@ -124,42 +124,41 @@ class Prober4(Player):
124124
@init_args
125125
def __init__(self):
126126
Player.__init__(self)
127-
self.probe_sequence = [C, C, D, C, D, D, D, C, C, D,
128-
C, D, C, C, D, C, D, D, C, D]
129-
self.probe_length = len(self.probe_sequence)
130-
self.politness_pool = [C, C, C, C, C]
127+
self.init_sequence = [C, C, D, C, D, D, D, C, C, D,
128+
C, D, C, C, D, C, D, D, C, D]
129+
self.just_Ds = 0
130+
self.unjust_Ds = 0
131+
self.politeness_pool = [C, C, C, C, C]
131132
self.is_angry = False
132133

133134
def strategy(self, opponent):
134-
turn = len(self.history)
135-
if turn <= self.probe_length:
136-
return self.probe_sequence[turn - 1]
137-
if turn == self.probe_length + 1:
138-
self.judge(opponent)
139-
if self.is_angry:
140-
return D
135+
if len(opponent.history) == 0:
136+
return C
137+
if len(self.history) == 0:
138+
return C
141139
else:
142-
# Cooperate for the next 5 turns
143-
if self.politness_pool:
144-
return self.politness_pool.pop()
145-
else:
146-
# TFT
147-
return D if opponent.history[-1:] == [D] else C
148-
149-
def judge(self, opponent):
150-
just_defect = 0
151-
unjust_defect = 0
152-
for turn in range(self.probe_length):
153-
if opponent.history[turn + 1] == D:
154-
if self.history[turn] == C:
155-
unjust_defect += 1
156-
if self.history[turn] == D:
157-
just_defect += 1
158-
if just_defect - unjust_defect >= 3:
159-
self.is_angry = True
140+
turn = len(self.history)
141+
if turn < len(self.init_sequence):
142+
if opponent.history[-1] == D:
143+
if self.history[-1] == D:
144+
self.just_Ds += 1
145+
if self.history[-1] == C:
146+
self.unjust_Ds += 1
147+
return self.init_sequence[turn]
148+
if turn == len(self.init_sequence):
149+
self.is_angry = (self.just_Ds - self.unjust_Ds >= 3)
150+
if self.is_angry:
151+
return D
152+
if not self.is_angry:
153+
if self.politeness_pool:
154+
return self.politeness_pool.pop()
155+
else:
156+
return D if opponent.history[-1:] == [D] else C
160157

161158
def reset(self):
162159
Player.reset(self)
160+
self.just_Ds = 0
161+
self.unjust_Ds = 0
163162
self.politeness_pool = [C, C, C, C, C]
164163
self.is_angry = False
165164

axelrod/tests/unit/test_prober.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,29 @@ def test_strategy(self):
9999
self.responses_test([D, C, C, D, C], [C, D, C, C], [C])
100100

101101

102+
class TestProber4(TestPlayer):
103+
104+
name = "Prober 4"
105+
player = axelrod.Prober4
106+
expected_classifier = {
107+
'memory_depth': 1,
108+
'stochastic': False,
109+
'makes_use_of': set(),
110+
'long_run_time': False,
111+
'inspects_source': False,
112+
'manipulates_source': False,
113+
'manipulates_state': False
114+
}
115+
116+
def test_initial_strategy(self):
117+
"""Starts by playing CCDCDDDCCDCDCCDCDDCD."""
118+
self.responses_test([], [], [C, C, D, C, D, D, D, C, C, D,
119+
C, D, C, C, D, C, D, D, C, D])
120+
# def test_strategy(self):
121+
# pass
122+
#
123+
124+
102125
class TestHardProber(TestPlayer):
103126

104127
name = "Hard Prober"

0 commit comments

Comments
 (0)