Skip to content

Commit 49e22d1

Browse files
author
margaret
committed
Add basic tests for Prober4
1 parent df394b4 commit 49e22d1

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

axelrod/strategies/prober.py

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

105105
class Prober4(Player):
106106
"""
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.
107+
Plays initial sequence of 20 fixed moves.
108+
Counts just (retaliated) and unjust defections of the opponent.
109+
If the absolute difference between just and unjust defections
110+
is greater than 2, defects forever.
110111
Otherwise cooperates for the next 5 moves, and plays TFT afterwards.
111112
"""
112113

@@ -129,7 +130,7 @@ def __init__(self):
129130
self.just_Ds = 0
130131
self.unjust_Ds = 0
131132
self.politeness_pool = [C, C, C, C, C]
132-
self.is_angry = False
133+
self.is_naughty = False
133134

134135
def strategy(self, opponent):
135136
if len(opponent.history) == 0:
@@ -146,10 +147,11 @@ def strategy(self, opponent):
146147
self.unjust_Ds += 1
147148
return self.init_sequence[turn]
148149
if turn == len(self.init_sequence):
149-
self.is_angry = (self.just_Ds - self.unjust_Ds >= 3)
150-
if self.is_angry:
150+
diff_in_Ds = abs(self.just_Ds - self.unjust_Ds)
151+
self.is_naughty = (diff_in_Ds > 2)
152+
if self.is_naughty:
151153
return D
152-
if not self.is_angry:
154+
if not self.is_naughty:
153155
if self.politeness_pool:
154156
return self.politeness_pool.pop()
155157
else:

axelrod/tests/unit/test_prober.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,26 @@ class TestProber4(TestPlayer):
112112
'manipulates_source': False,
113113
'manipulates_state': False
114114
}
115+
initial_sequence = [C, C, D, C, D, D, D, C, C, D,
116+
C, D, C, C, D, C, D, D, C, D]
115117

116118
def test_initial_strategy(self):
117119
"""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-
#
120+
self.responses_test([], [], self.initial_sequence)
121+
122+
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)
123135

124136

125137
class TestHardProber(TestPlayer):

0 commit comments

Comments
 (0)