-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathgame.py
More file actions
74 lines (58 loc) · 2 KB
/
game.py
File metadata and controls
74 lines (58 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from typing import Tuple, Union
from axelrod import Action
C, D = Action.C, Action.D
Score = Union[int, float]
class Game(object):
"""Container for the game matrix and scoring logic.
Attributes
----------
scores: dict
The numerical score attribute to all combinations of action pairs.
"""
def __init__(
self, r: Score = 3, s: Score = 0, t: Score = 5, p: Score = 1
) -> None:
"""Create a new game object.
Parameters
----------
r: int or float
Score obtained by both players for mutual cooperation.
s: int or float
Score obtained by a player for cooperating against a defector.
t: int or float
Score obtained by a player for defecting against a cooperator.
p: int or float
Score obtained by both player for mutual defection.
"""
self.scores = {
(C, C): (r, r),
(D, D): (p, p),
(C, D): (s, t),
(D, C): (t, s),
}
def RPST(self) -> Tuple[Score, Score, Score, Score]:
"""Returns game matrix values in Press and Dyson notation."""
R = self.scores[(C, C)][0]
P = self.scores[(D, D)][0]
S = self.scores[(C, D)][0]
T = self.scores[(D, C)][0]
return R, P, S, T
def score(self, pair: Tuple[Action, Action]) -> Tuple[Score, Score]:
"""Returns the appropriate score for a decision pair.
Parameters
----------
pair: tuple(Action, Action)
A pair actions for two players, for example (C, C).
Returns
-------
tuple of int or float
Scores for two player resulting from their actions.
"""
return self.scores[pair]
def __repr__(self) -> str:
return "Axelrod game: (R,P,S,T) = {}".format(self.RPST())
def __eq__(self, other):
if not isinstance(other, Game):
return False
return self.RPST() == other.RPST()
DefaultGame = Game()