Skip to content

Commit 72f5edb

Browse files
committed
Cleaned up record history logic.
1 parent 5bf2d69 commit 72f5edb

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

axelrod/strategies/axelrod_second.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,6 @@ def __init__(self):
10941094
self.move_history = np.zeros([4, 2])
10951095
# Will cache value only for testing purposes, not used otherwise
10961096
self.chi_squared = None
1097-
self.history_row = 0
10981097

10991098
self.more_coop = 0 # This schedules cooperation for future turns
11001099
# Initial last_generous_n_turns_ago to 3 because this counts up and
@@ -1231,24 +1230,26 @@ def strategy(self, opponent: Player) -> Action:
12311230
# If not Defect mode, proceed to update history and check for random,
12321231
# check if burned, and check if opponent's fairweather.
12331232

1234-
# History only gets updated outside of Defect mode.
1235-
if turn > 2:
1236-
if opponent.history[-1] == D:
1237-
self.recorded_defects += 1
1238-
opp_col = 1 if opponent.history[-1] == D else 0
1239-
self.move_history[self.history_row, opp_col] += 1
1240-
1241-
# Detect random
1242-
if turn % 15 == 0 and turn > 15 and not self.was_defective:
1243-
if self.detect_random(turn):
1244-
self.mode = "Defect"
1245-
return self.try_return(D, lower_flags=False) # Lower_flags not used here.
1246-
1247-
# history_row only gets updated if not in Defect mode AND not entering
1248-
# Defect mode.
1249-
self.history_row = 1 if opponent.history[-1] == D else 0
1250-
if self.history[-1] == D:
1251-
self.history_row += 2
1233+
# If we haven't yet entered Defect mode
1234+
if not self.was_defective:
1235+
if turn > 2:
1236+
if opponent.history[-1] == D:
1237+
self.recorded_defects += 1
1238+
1239+
# Column decided by opponent's last turn
1240+
history_col = 1 if opponent.history[-1] == D else 0
1241+
# Row is decided by opponent's move two turns ago and our move
1242+
# two turns ago.
1243+
history_row = 1 if opponent.history[-2] == D else 0
1244+
if self.history[-2] == D:
1245+
history_row += 2
1246+
self.move_history[history_row, history_col] += 1
1247+
1248+
# Try to detect random opponent
1249+
if turn % 15 == 0 and turn > 15:
1250+
if self.detect_random(turn):
1251+
self.mode = "Defect"
1252+
return self.try_return(D, lower_flags=False) # Lower_flags not used here.
12521253

12531254
# If generous 2 turns ago and opponent defected last turn
12541255
if self.last_generous_n_turns_ago == 2 and opponent.history[-1] == D:

0 commit comments

Comments
 (0)