Skip to content

Commit 36aa63f

Browse files
langnerdrvinceknight
authored andcommitted
Spruce up game and its tests
1 parent 8022e55 commit 36aa63f

File tree

2 files changed

+69
-42
lines changed

2 files changed

+69
-42
lines changed

axelrod/game.py

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,61 @@
11
from typing import Tuple, Union
22

3-
from .action import Action
3+
from axelrod import Action
44

55
C, D = Action.C, Action.D
66

77
Score = Union[int, float]
88

99

1010
class Game(object):
11-
"""A class to hold the game matrix and to score a game accordingly."""
11+
"""Container for the game matrix and scoring logic.
12+
13+
Attributes
14+
----------
15+
scores: dict
16+
The numerical score attribute to all combinations of action pairs.
17+
"""
1218

1319
def __init__(self, r: Score = 3, s: Score = 0, t: Score = 5, p: Score = 1) -> None:
14-
self.scores = {(C, C): (r, r), (D, D): (p, p), (C, D): (s, t), (D, C): (t, s)}
20+
"""Create a new game object.
21+
22+
Parameters
23+
----------
24+
r: int or float
25+
Score obtained by both players for mutual cooperation.
26+
s: int or float
27+
Score obtained by a player for cooperating against a defector.
28+
t: int or float
29+
Score obtained by a player for defecting against a cooperator.
30+
p: int or float
31+
Score obtained by both player for mutual defection.
32+
"""
33+
self.scores = {
34+
(C, C): (r, r),
35+
(D, D): (p, p),
36+
(C, D): (s, t),
37+
(D, C): (t, s)}
1538

1639
def RPST(self) -> Tuple[Score, Score, Score, Score]:
17-
"""Return the values in the game matrix in the Press and Dyson
18-
notation."""
40+
"""Returns game matrix values in Press and Dyson notation."""
1941
R = self.scores[(C, C)][0]
2042
P = self.scores[(D, D)][0]
2143
S = self.scores[(C, D)][0]
2244
T = self.scores[(D, C)][0]
2345
return (R, P, S, T)
2446

2547
def score(self, pair: Tuple[Action, Action]) -> Tuple[Score, Score]:
26-
"""Return the appropriate score for decision pair.
48+
"""Returns the appropriate score for a decision pair.
49+
50+
Parameters
51+
----------
52+
pair: tuple(Action, Action)
53+
A pair actions for two players, for example (C, C).
2754
28-
Returns the appropriate score (as a tuple) from the scores dictionary
29-
for a given pair of plays (passed in as a tuple).
30-
e.g. score((C, C)) returns (2, 2)
55+
Returns
56+
-------
57+
tuple of int or float
58+
Scores for two player resulting from their actions.
3159
"""
3260
return self.scores[pair]
3361

axelrod/tests/unit/test_game.py

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,81 @@
11
import unittest
22

3-
import axelrod
4-
from axelrod.tests.property import *
3+
from axelrod import Action, Game
4+
from axelrod.tests.property import games
55

66
from hypothesis import given, settings
7+
from hypothesis.strategies import integers
78

8-
C, D = axelrod.Action.C, axelrod.Action.D
9+
C, D = Action.C, Action.D
910

1011

1112
class TestGame(unittest.TestCase):
12-
@classmethod
13-
def setUpClass(cls):
14-
cls.game = axelrod.Game()
1513

16-
def test_init(self):
14+
def test_default_scores(self):
1715
expected_scores = {
1816
(C, D): (0, 5),
1917
(D, C): (5, 0),
2018
(D, D): (1, 1),
2119
(C, C): (3, 3),
2220
}
23-
self.assertEqual(self.game.scores, expected_scores)
21+
self.assertEqual(Game().scores, expected_scores)
2422

25-
def test_RPST(self):
23+
def test_default_RPST(self):
2624
expected_values = (3, 1, 0, 5)
27-
self.assertEqual(self.game.RPST(), expected_values)
25+
self.assertEqual(Game().RPST(), expected_values)
2826

29-
def test_score(self):
30-
self.assertEqual(self.game.score((C, C)), (3, 3))
31-
self.assertEqual(self.game.score((D, D)), (1, 1))
32-
self.assertEqual(self.game.score((C, D)), (0, 5))
33-
self.assertEqual(self.game.score((D, C)), (5, 0))
34-
35-
def test_equality(self):
36-
game_1 = Game(1, 2, 3, 4)
37-
game_2 = Game(1, 2, 3, 4)
38-
not_equal = Game()
39-
40-
self.assertEqual(game_1, game_2)
41-
self.assertNotEqual(not_equal, game_1)
27+
def test_default_score(self):
28+
game = Game()
29+
self.assertEqual(game.score((C, C)), (3, 3))
30+
self.assertEqual(game.score((D, D)), (1, 1))
31+
self.assertEqual(game.score((C, D)), (0, 5))
32+
self.assertEqual(game.score((D, C)), (5, 0))
4233

34+
def test_default_equality(self):
4335
self.assertEqual(Game(), Game())
4436

37+
def test_not_default_equality(self):
38+
self.assertEqual(Game(1, 2, 3, 4), Game(1, 2, 3, 4))
39+
self.assertNotEqual(Game(1, 2, 3, 4), Game(1, 2, 3, 5))
40+
self.assertNotEqual(Game(1, 2, 3, 4), Game())
41+
42+
def test_wrong_class_equality(self):
4543
self.assertNotEqual(Game(), "wrong class")
4644

4745
@given(r=integers(), p=integers(), s=integers(), t=integers())
4846
@settings(max_examples=5, max_iterations=20)
49-
def test_property_init(self, r, p, s, t):
50-
"""Use the hypothesis library to test init"""
47+
def test_random_init(self, r, p, s, t):
48+
"""Test init with random scores using the hypothesis library."""
5149
expected_scores = {
5250
(C, D): (s, t),
5351
(D, C): (t, s),
5452
(D, D): (p, p),
5553
(C, C): (r, r),
5654
}
57-
game = axelrod.Game(r, s, t, p)
55+
game = Game(r, s, t, p)
5856
self.assertEqual(game.scores, expected_scores)
5957

6058
@given(r=integers(), p=integers(), s=integers(), t=integers())
6159
@settings(max_examples=5, max_iterations=20)
62-
def test_property_RPST(self, r, p, s, t):
63-
"""Use the hypothesis library to test RPST"""
64-
game = axelrod.Game(r, s, t, p)
60+
def test_random_RPST(self, r, p, s, t):
61+
"""Test RPST method with random scores using the hypothesis library."""
62+
game = Game(r, s, t, p)
6563
self.assertEqual(game.RPST(), (r, p, s, t))
6664

6765
@given(r=integers(), p=integers(), s=integers(), t=integers())
6866
@settings(max_examples=5, max_iterations=20)
69-
def test_property_score(self, r, p, s, t):
70-
"""Use the hypothesis library to test score"""
71-
game = axelrod.Game(r, s, t, p)
67+
def test_random_score(self, r, p, s, t):
68+
"""Test score method with random scores using the hypothesis library."""
69+
game = Game(r, s, t, p)
7270
self.assertEqual(game.score((C, C)), (r, r))
7371
self.assertEqual(game.score((D, D)), (p, p))
7472
self.assertEqual(game.score((C, D)), (s, t))
7573
self.assertEqual(game.score((D, C)), (t, s))
7674

7775
@given(game=games())
7876
@settings(max_examples=5, max_iterations=20)
79-
def test_repr(self, game):
77+
def test_random_repr(self, game):
78+
"""Test repr with random scores using the hypothesis library."""
8079
expected_repr = "Axelrod game: (R,P,S,T) = {}".format(game.RPST())
8180
self.assertEqual(expected_repr, game.__repr__())
8281
self.assertEqual(expected_repr, str(game))

0 commit comments

Comments
 (0)