Skip to content

Commit ba99a8f

Browse files
authored
Merge pull request #4 from Axelrod-Python/add-test-for-original-strategy
Add a test for original_strategy
2 parents 1867f76 + 9966040 commit ba99a8f

File tree

1 file changed

+49
-17
lines changed

1 file changed

+49
-17
lines changed

tests/test_player.py

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66

77
import itertools
88

9-
from hypothesis import given
10-
from hypothesis.strategies import integers
11-
129
C, D = Action.C, Action.D
1310

1411

@@ -26,22 +23,57 @@ def test_matches():
2623
for strategy in all_strategies:
2724
for opponent in (Alternator, Cooperator, Defector):
2825
players = (Player(strategy), opponent())
29-
match = Match(players, 200)
26+
match = Match(players, turns=200)
27+
assert all(
28+
action in (C, D) for interaction in match.play()
29+
for action in interaction)
30+
31+
def test_noisy_matches():
32+
for strategy in all_strategies:
33+
for opponent in (Alternator, Cooperator, Defector):
34+
players = (Player(strategy), opponent())
35+
match = Match(players, turns=200, noise=0.5)
36+
assert all(
37+
action in (C, D) for interaction in match.play()
38+
for action in interaction)
39+
40+
def test_probend_matches():
41+
for strategy in all_strategies:
42+
for opponent in (Alternator, Cooperator, Defector):
43+
players = (Player(strategy), opponent())
44+
match = Match(players, prob_end=0.5, noise=0.5)
3045
assert all(
3146
action in (C, D) for interaction in match.play()
3247
for action in interaction)
3348

49+
def test_original_strategy():
50+
"""
51+
Test original strategy against all possible first 5 moves of a Match
52+
"""
53+
actions_to_scores = {(0, 0): (3, 3), (0, 1): (0, 5),
54+
(1, 0): (5, 0), (1, 1): (1, 1)}
55+
for strategy in all_strategies:
56+
for opponent_sequence in itertools.product((0, 1), repeat=5):
57+
58+
player = Player(strategy)
59+
60+
# Initial set up for empty history
61+
my_score, their_score = 0, 0
62+
move_number = 1
63+
their_previous_action, my_action = 0, 0
64+
65+
for action in opponent_sequence:
66+
my_action = player.original_strategy(
67+
their_last_move=their_previous_action,
68+
move_number=move_number,
69+
my_score=my_score,
70+
their_score=their_score,
71+
noise=0,
72+
my_last_move=my_action)
73+
74+
assert my_action in [0, 1]
3475

35-
@given(
36-
move_number=integers(min_value=1, max_value=200),
37-
my_score=integers(min_value=0, max_value=200),
38-
their_score=integers(min_value=0, max_value=200),
39-
)
40-
def test_original_strategy(move_number, my_score, their_score):
41-
for their_last_move, my_last_move in itertools.product((0, 1), repeat=2):
42-
for strategy in all_strategies:
43-
with Player(strategy) as player:
44-
action = player.original_strategy(
45-
their_last_move, move_number, my_score, their_score, 0,
46-
my_last_move)
47-
assert action in (0, 1), print(f'{strategy} returned {action}')
76+
scores = actions_to_scores[my_action, action]
77+
their_previous_action = action
78+
my_score += scores[0]
79+
their_score += scores[1]

0 commit comments

Comments
 (0)