Skip to content

Commit 7a968cd

Browse files
committed
Merge pull request #289 from Axelrod-Python/286
Creates tournament attributes dictionary for players
2 parents 1cee08e + 30b0503 commit 7a968cd

File tree

7 files changed

+22
-11
lines changed

7 files changed

+22
-11
lines changed

axelrod/player.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def __init__(self):
3333
"""Initiates an empty history and 0 score for a player."""
3434
self.history = []
3535
self.stochastic = "random" in inspect.getsource(self.__class__)
36-
self.tournament_length = -1
36+
self.tournament_attributes = {'length': -1, 'game': None}
3737
if self.name == "Player":
3838
self.stochastic = False
3939
self.cooperations = 0

axelrod/round_robin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def _pair_of_players(self, player1_index, player2_index):
8787
player2 = copy.deepcopy(player1)
8888
else:
8989
player2 = class1()
90-
player2.tournament_length = self.turns
90+
player2.tournament_attributes = player1.tournament_attributes
9191
class2 = class1
9292
else:
9393
player2 = self.players[player2_index]

axelrod/strategies/axelrod_tournaments.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class Champion(Player):
161161

162162
def strategy(self, opponent):
163163
current_round = len(self.history)
164-
expected_length = self.tournament_length
164+
expected_length = self.tournament_attributes['length']
165165
# Cooperate for the first 1/20-th of the game
166166
if current_round == 0:
167167
return 'C'

axelrod/strategies/backstabber.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class BackStabber(Player):
1414
def strategy(self, opponent):
1515
if not opponent.history:
1616
return 'C'
17-
if len(opponent.history) > (self.tournament_length - 3):
17+
if len(opponent.history) > (self.tournament_attributes['length'] - 3):
1818
return 'D'
1919
if opponent.defections > 3:
2020
return 'D'
@@ -38,7 +38,7 @@ def strategy(self, opponent):
3838

3939
if not opponent.history:
4040
return 'C'
41-
if len(opponent.history) > (self.tournament_length - 3):
41+
if len(opponent.history) > (self.tournament_attributes['length'] - 3):
4242
return 'D'
4343
if len(opponent.history) < 180:
4444
if len(opponent.history) > cutoff:

axelrod/tests/unit/test_player.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ def test_initialisation(self):
9898
"""Test that the player initiates correctly."""
9999
self.assertEqual(self.player().history, [])
100100
self.assertEqual(self.player().stochastic, self.stochastic)
101-
self.assertEqual(self.player().tournament_length, -1)
101+
self.assertEqual(self.player().tournament_attributes,
102+
{'length': -1, 'game': None})
102103
self.assertEqual(self.player().cooperations, 0)
103104
self.assertEqual(self.player().defections, 0)
104105

@@ -149,9 +150,9 @@ def responses_test(self, history_1, history_2, responses,
149150
subsequent moves by player one to test.
150151
"""
151152
P1 = self.player()
152-
P1.tournament_length = tournament_length
153+
P1.tournament_attributes['length'] = tournament_length
153154
P2 = Player()
154-
P2.tournament_length = tournament_length
155+
P2.tournament_attributes['length'] = tournament_length
155156
test_responses(
156157
self, P1, P2, history_1, history_2, responses,
157158
random_seed=random_seed)

axelrod/tests/unit/test_tournament.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def setUpClass(cls):
2626
axelrod.GoByMajority()]
2727
cls.test_name = 'test'
2828
cls.test_repetitions = 5
29+
cls.test_turns = 100
2930

3031
cls.expected_payoff = [
3132
[600, 600, 0, 600, 600],
@@ -46,12 +47,19 @@ def test_init(self):
4647
name=self.test_name,
4748
players=self.players,
4849
game=self.game,
50+
turns=self.test_turns,
4951
processes=4,
5052
noise=0.2)
5153
self.assertEqual(len(tournament.players), len(self.players))
52-
self.assertEqual(tournament.players[0].tournament_length, 200)
54+
self.assertEqual(
55+
tournament.players[0].tournament_attributes['length'],
56+
self.test_turns
57+
)
58+
self.assertIsInstance(
59+
tournament.players[0].tournament_attributes['game'], axelrod.Game
60+
)
5361
self.assertEqual(tournament.game.score(('C', 'C')), (3, 3))
54-
self.assertEqual(tournament.turns, 200)
62+
self.assertEqual(tournament.turns, self.test_turns)
5563
self.assertEqual(tournament.repetitions, 10)
5664
self.assertEqual(tournament.name, 'test')
5765
self.assertEqual(tournament._processes, 4)

axelrod/tournament.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ def players(self):
3838
def players(self, players):
3939
newplayers = []
4040
for player in players:
41-
player.tournament_length = self.turns
41+
player.tournament_attributes = {
42+
'length': self.turns,
43+
'game': self.game}
4244
newplayers.append(player)
4345
self._players = newplayers
4446

0 commit comments

Comments
 (0)