Skip to content

Commit 9a1ab8f

Browse files
author
margaret
committed
Add more tests to Prober4
Tweak Prober4 property names
1 parent 49e22d1 commit 9a1ab8f

File tree

2 files changed

+54
-24
lines changed

2 files changed

+54
-24
lines changed

axelrod/strategies/prober.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def strategy(self, opponent):
104104

105105
class Prober4(Player):
106106
"""
107-
Plays initial sequence of 20 fixed moves.
107+
Plays fixed initial sequence of 20 moves.
108108
Counts just (retaliated) and unjust defections of the opponent.
109109
If the absolute difference between just and unjust defections
110110
is greater than 2, defects forever.
@@ -129,8 +129,8 @@ def __init__(self):
129129
C, D, C, C, D, C, D, D, C, D]
130130
self.just_Ds = 0
131131
self.unjust_Ds = 0
132-
self.politeness_pool = [C, C, C, C, C]
133-
self.is_naughty = False
132+
self.cooperation_pool = 5 * [C]
133+
self.turned_defector = False
134134

135135
def strategy(self, opponent):
136136
if len(opponent.history) == 0:
@@ -148,21 +148,21 @@ def strategy(self, opponent):
148148
return self.init_sequence[turn]
149149
if turn == len(self.init_sequence):
150150
diff_in_Ds = abs(self.just_Ds - self.unjust_Ds)
151-
self.is_naughty = (diff_in_Ds > 2)
152-
if self.is_naughty:
151+
self.turned_defector = (diff_in_Ds > 2)
152+
if self.turned_defector:
153153
return D
154-
if not self.is_naughty:
155-
if self.politeness_pool:
156-
return self.politeness_pool.pop()
154+
if not self.turned_defector:
155+
if self.cooperation_pool:
156+
return self.cooperation_pool.pop()
157157
else:
158-
return D if opponent.history[-1:] == [D] else C
158+
return D if opponent.history[-1] == D else C
159159

160160
def reset(self):
161161
Player.reset(self)
162162
self.just_Ds = 0
163163
self.unjust_Ds = 0
164-
self.politeness_pool = [C, C, C, C, C]
165-
self.is_angry = False
164+
self.cooperation_pool = [C, C, C, C, C]
165+
self.became_defector = False
166166

167167

168168
class HardProber(Player):

axelrod/tests/unit/test_prober.py

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ class TestProber4(TestPlayer):
104104
name = "Prober 4"
105105
player = axelrod.Prober4
106106
expected_classifier = {
107-
'memory_depth': 1,
108107
'stochastic': False,
108+
'memory_depth': 1,
109109
'makes_use_of': set(),
110110
'long_run_time': False,
111111
'inspects_source': False,
@@ -114,24 +114,54 @@ class TestProber4(TestPlayer):
114114
}
115115
initial_sequence = [C, C, D, C, D, D, D, C, C, D,
116116
C, D, C, C, D, C, D, D, C, D]
117+
cooperation_pool = [C] * 5
117118

118119
def test_initial_strategy(self):
119120
"""Starts by playing CCDCDDDCCDCDCCDCDDCD."""
120121
self.responses_test([], [], self.initial_sequence)
121122

122123
def test_strategy(self):
123-
# Defects forever if opponent played D for C
124-
# at least 3 more times than D for D
125-
self.responses_test(self.initial_sequence,
126-
self.initial_sequence, [D] * 10)
127-
128-
# Defects forever if opponent played D for D
129-
# at least 3 more times than C for D
130-
opponents_history = list(map(lambda x: D if x is C else C,
131-
self.initial_sequence))
132-
133-
self.responses_test(self.initial_sequence,
134-
opponents_history, [D] * 10)
124+
# Defects forever if the opponent played D for C
125+
# at least 3 times more than D for D
126+
history1 = self.initial_sequence
127+
history2 = self.initial_sequence
128+
responses = [D] * 10
129+
self.responses_test(history1, history2, responses)
130+
131+
# or if the opponent played D for D
132+
# at least 3 times more than D for C
133+
history1 = self.initial_sequence
134+
history2 = list(map(lambda x: D if x is C else C,
135+
self.initial_sequence))
136+
responses = [D] * 10
137+
self.responses_test(history1, history2, responses)
138+
139+
# Otherwise cooperates for 5 rounds
140+
history1 = self.initial_sequence
141+
history2 = [C] * len(history1)
142+
responses = self.cooperation_pool
143+
self.responses_test(history1, history2, responses)
144+
145+
# and plays like TFT afterwards
146+
history1 = self.initial_sequence + self.cooperation_pool
147+
history2 = [C] * (len(history1) - 1) + [D]
148+
self.responses_test(history1, history2, [D])
149+
150+
history1 = self.initial_sequence + self.cooperation_pool + [D]
151+
history2 = [C] * len(history1)
152+
self.responses_test(history1, history2, [C])
153+
154+
history1 = self.initial_sequence + self.cooperation_pool
155+
history2 = [C] * len(history1)
156+
self.responses_test(history1, history2, [C])
157+
158+
history1 = self.initial_sequence + self.cooperation_pool + [C]
159+
history2 = [C] * (len(history1) - 1) + [D]
160+
self.responses_test(history1, history2, [D])
161+
162+
history1 = self.initial_sequence + self.cooperation_pool + [C]
163+
history2 = [C] * (len(history1) - 1) + [D]
164+
self.responses_test(history1, history2, [D])
135165

136166

137167
class TestHardProber(TestPlayer):

0 commit comments

Comments
 (0)