@@ -22,30 +22,37 @@ class TestPunisher(TestPlayer):
22
22
23
23
def test_init (self ):
24
24
"""Tests for the __init__ method."""
25
- P1 = axelrod .Punisher ()
26
- self .assertEqual (P1 .history , [])
27
- self .assertEqual (P1 .mem_length , 1 )
28
- self .assertEqual ( P1 .grudged , False )
29
- self .assertEqual (P1 .grudge_memory , 1 )
25
+ player = axelrod .Punisher ()
26
+ self .assertEqual (player .history , [])
27
+ self .assertEqual (player .mem_length , 1 )
28
+ self .assertFalse ( player .grudged )
29
+ self .assertEqual (player .grudge_memory , 1 )
30
30
31
31
def test_strategy (self ):
32
- self .responses_test ([C ], [], [], attrs = {"grudged" : False })
33
- self .responses_test ([C ], [C ], [C ], attrs = {"grudged" : False })
34
- self .responses_test ([D ], [C ], [D ], attrs = {"grudged" : True })
35
- for i in range (10 ):
36
- self .responses_test ([D ], [C , C ] + [D ] * i , [C , D ] + [C ] * i ,
37
- attrs = {"grudged" : True , "grudge_memory" : i ,
38
- "mem_length" : 10 })
32
+
33
+ opponent = axelrod .Alternator ()
34
+ actions = [(C , C ), (C , D ), (D , C )]
35
+ self .versus_test (opponent = opponent , expected_actions = actions ,
36
+ attrs = {"grudged" : True , "grudge_memory" : 0 })
37
+
38
+ opponent = axelrod .MockPlayer ([C , D ] + [C ] * 10 )
39
+ actions = [(C , C ), (C , D )] + [(D , C )] * 11
40
+ self .versus_test (opponent = opponent , expected_actions = actions ,
41
+ attrs = {"grudged" : True , "grudge_memory" : 10 })
42
+
39
43
# Eventually the grudge is dropped
40
- i = 11
41
- self .responses_test ([C ], [C , C ] + [D ] * i , [C , D ] + [C ] * i ,
42
- attrs = {"grudged" : False , "grudge_memory" : 0 ,
43
- "mem_length" : 10 })
44
+ opponent = axelrod .MockPlayer ([C , D ] + [C ] * 10 )
45
+ actions = [(C , C ), (C , D )] + [(D , C )] * 11 + [(C , D )]
46
+ self .versus_test (opponent = opponent , expected_actions = actions ,
47
+ attrs = {"grudged" : False , "grudge_memory" : 0 ,
48
+ "mem_length" : 10 })
44
49
45
50
# Grudged again on opponent's D
46
- self .responses_test ([D ], [C , C ] + [D ] * i + [C ], [C , D ] + [C ] * i + [D ],
47
- attrs = {"grudged" : True , "grudge_memory" : 0 ,
48
- "mem_length" : 2 })
51
+ opponent = axelrod .MockPlayer ([C , D ] + [C ] * 11 )
52
+ actions = [(C , C ), (C , D )] + [(D , C )] * 11 + [(C , C ), (C , D ), (D , C )]
53
+ self .versus_test (opponent = opponent , expected_actions = actions ,
54
+ attrs = {"grudged" : True , "grudge_memory" : 0 ,
55
+ "mem_length" : 2 })
49
56
50
57
51
58
class TestInversePunisher (TestPlayer ):
@@ -64,34 +71,37 @@ class TestInversePunisher(TestPlayer):
64
71
65
72
def test_init (self ):
66
73
"""Tests for the __init__ method."""
67
- P1 = axelrod .InversePunisher ()
68
- self .assertEqual (P1 .history , [])
69
- self .assertEqual (P1 .mem_length , 1 )
70
- self .assertEqual ( P1 .grudged , False )
71
- self .assertEqual (P1 .grudge_memory , 1 )
74
+ player = axelrod .InversePunisher ()
75
+ self .assertEqual (player .history , [])
76
+ self .assertEqual (player .mem_length , 1 )
77
+ self .assertFalse ( player .grudged )
78
+ self .assertEqual (player .grudge_memory , 1 )
72
79
73
80
def test_strategy (self ):
74
- self .responses_test ([C ], [], [], attrs = {"grudged" : False })
75
- self .responses_test ([C ], [C ], [C ], attrs = {"grudged" : False })
76
- self .responses_test ([D ], [C ], [D ], attrs = {"grudged" : True })
77
- for i in range (10 ):
78
- self .responses_test ([D ], [C , C ] + [D ] * i , [C , D ] + [C ] * i ,
79
- attrs = {"grudged" : True , "grudge_memory" : i ,
80
- "mem_length" : 10 })
81
+ opponent = axelrod .Alternator ()
82
+ actions = [(C , C ), (C , D ), (D , C )]
83
+ self .versus_test (opponent = opponent , expected_actions = actions ,
84
+ attrs = {"grudged" : True , "grudge_memory" : 0 })
85
+
86
+ opponent = axelrod .MockPlayer ([C , D ] + [C ] * 10 )
87
+ actions = [(C , C ), (C , D )] + [(D , C )] * 11
88
+ self .versus_test (opponent = opponent , expected_actions = actions ,
89
+ attrs = {"grudged" : True , "grudge_memory" : 10 })
90
+
81
91
# Eventually the grudge is dropped
82
- i = 11
83
- self .responses_test ([C ], [C , C ] + [D ] * i , [C , D ] + [C ] * i ,
84
- attrs = {"grudged" : False , "grudge_memory" : 0 ,
85
- "mem_length" : 10 })
92
+ opponent = axelrod .MockPlayer ([C , D ] + [C ] * 10 )
93
+ actions = [(C , C ), (C , D )] + [(D , C )] * 11 + [(C , D )]
94
+ self .versus_test (opponent = opponent , expected_actions = actions ,
95
+ attrs = {"grudged" : False , "grudge_memory" : 0 ,
96
+ "mem_length" : 10 })
97
+
86
98
# Grudged again on opponent's D
87
- self .responses_test ([D ], [C , C ] + [D ] * i + [C ], [C , D ] + [C ] * i + [D ],
88
- attrs = {"grudged" : True , "grudge_memory" : 0 ,
89
- "mem_length" : 17 })
99
+ opponent = axelrod .MockPlayer ([C , D ] + [C ] * 11 )
100
+ actions = [(C , C ), (C , D )] + [(D , C )] * 11 + [(C , C ), (C , D ), (D , C )]
101
+ self .versus_test (opponent = opponent , expected_actions = actions ,
102
+ attrs = {"grudged" : True , "grudge_memory" : 0 ,
103
+ "mem_length" : 17 })
90
104
91
- # Test a different grudge length period
92
- self .responses_test ([D ], [C ] * 5 , [C ] * 4 + [D ],
93
- attrs = {"grudged" : True , "grudge_memory" : 0 ,
94
- "mem_length" : 16 })
95
105
96
106
class TestLevelPunisher (TestPlayer ):
97
107
@@ -111,11 +121,18 @@ def test_strategy(self):
111
121
# Starts by Cooperating
112
122
self .first_play_test (C )
113
123
114
- # Defects if the turns played are less than 10.
115
- self .responses_test ([C ], [C ], [C ])
116
- self .responses_test ([C ], [C ] * 4 , [C , D , C , D ])
117
-
118
- # Check for the number of rounds greater than 10.
119
- self .responses_test ([C ], [C ] * 10 , [C , C , C , C , D , C , C , C , C , D ])
120
- #Check if number of defections by opponent is greater than 20%
121
- self .responses_test ([D ], [C ] * 10 , [D , D , D , D , D , C , D , D , D , D ])
124
+ # Cooperates if the turns played are less than 10.
125
+ actions = [(C , C )] * 9
126
+ self .versus_test (opponent = axelrod .Cooperator (),
127
+ expected_actions = actions )
128
+
129
+ # After 10 rounds
130
+ # Check if number of defections by opponent is greater than 20%
131
+ opponent = axelrod .MockPlayer ([C ] * 4 + [D ] * 2 + [C ] * 3 + [D ])
132
+ actions = [(C , C )] * 4 + [(C , D )] * 2 + [(C , C )] * 3 + [(C , D ), (D , C )]
133
+ self .versus_test (opponent = opponent , expected_actions = actions )
134
+
135
+ # Check if number of defections by opponent is less than 20%
136
+ opponent = axelrod .MockPlayer ([C ] * 4 + [D ] + [C ] * 4 + [D ])
137
+ actions = [(C , C )] * 4 + [(C , D )] + [(C , C )] * 4 + [(C , D ), (C , C )]
138
+ self .versus_test (opponent = opponent , expected_actions = actions )
0 commit comments