Skip to content

Commit 0c88bf8

Browse files
committed
Merge pull request #505 from marcharper/504
Catch self.plays == 0 corner case of LookerUp
2 parents e87918d + 3be95df commit 0c88bf8

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

axelrod/strategies/lookerup.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,15 @@ def strategy(self, opponent):
122122
if len(self.history) < max(self.plays, self.opponent_start_plays):
123123
return C
124124
# Count backward m turns to get my own recent history.
125-
history_start = -1 * self.plays
126-
my_history = ''.join(self.history[history_start:])
127-
# Do the same for the opponent.
128-
opponent_history = ''.join(opponent.history[history_start:])
129-
# Get the opponents first n actions.
125+
if self.plays == 0:
126+
my_history = ''
127+
opponent_history = ''
128+
else:
129+
history_start = -1 * self.plays
130+
my_history = ''.join(self.history[history_start:])
131+
# Do the same for the opponent.
132+
opponent_history = ''.join(opponent.history[history_start:])
133+
# Get the opponents first n actions.
130134
opponent_start = ''.join(opponent.history[:self.opponent_start_plays])
131135
# Put these three strings together in a tuple.
132136
key = (opponent_start, my_history, opponent_history)

axelrod/tests/unit/test_lookerup.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import axelrod
44
from .test_player import TestPlayer, TestHeadsUp
5+
from axelrod.strategies.lookerup import create_lookup_table_keys
56

67
C, D = axelrod.Actions.C, axelrod.Actions.D
78

@@ -66,6 +67,21 @@ def test_defector_table(self):
6667
self.responses_test([C, D], [D, C], [D])
6768
self.responses_test([D, D], [D, D], [D])
6869

70+
def test_zero_tables(self):
71+
"""Test the corner case where n=0."""
72+
pattern = "CD"
73+
lookup_table_keys = create_lookup_table_keys(plays=0,
74+
opponent_start_plays=1)
75+
76+
lookup_table = dict(zip(lookup_table_keys, pattern))
77+
player = axelrod.LookerUp(lookup_table)
78+
self.assertEqual(player.plays, 0)
79+
opp = axelrod.Cooperator()
80+
# This shouldn't throw an exception.
81+
for _ in range(5):
82+
player.play(opp)
83+
84+
6985
def test_starting_move(self):
7086
"""A lookup table that always repeats the opponent's first move."""
7187

0 commit comments

Comments
 (0)