@@ -22,30 +22,36 @@ 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 .mem_length , 1 )
27
+ self .assertFalse (player .grudged )
28
+ self .assertEqual (player .grudge_memory , 1 )
30
29
31
30
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 })
31
+
32
+ opponent = axelrod .Alternator ()
33
+ actions = [(C , C ), (C , D ), (D , C )]
34
+ self .versus_test (opponent = opponent , expected_actions = actions ,
35
+ attrs = {"grudged" : True , "grudge_memory" : 0 })
36
+
37
+ opponent = axelrod .MockPlayer ([C , D ] + [C ] * 10 )
38
+ actions = [(C , C ), (C , D )] + [(D , C )] * 11
39
+ self .versus_test (opponent = opponent , expected_actions = actions ,
40
+ attrs = {"grudged" : True , "grudge_memory" : 10 })
41
+
39
42
# 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 })
43
+ opponent = axelrod .MockPlayer ([C , D ] + [C ] * 10 )
44
+ actions = [(C , C ), (C , D )] + [(D , C )] * 11 + [(C , D )]
45
+ self .versus_test (opponent = opponent , expected_actions = actions ,
46
+ attrs = {"grudged" : False , "grudge_memory" : 0 ,
47
+ "mem_length" : 10 })
44
48
45
49
# 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 })
50
+ opponent = axelrod .MockPlayer ([C , D ] + [C ] * 11 )
51
+ actions = [(C , C ), (C , D )] + [(D , C )] * 11 + [(C , C ), (C , D ), (D , C )]
52
+ self .versus_test (opponent = opponent , expected_actions = actions ,
53
+ attrs = {"grudged" : True , "grudge_memory" : 0 ,
54
+ "mem_length" : 2 })
49
55
50
56
51
57
class TestInversePunisher (TestPlayer ):
@@ -64,34 +70,36 @@ class TestInversePunisher(TestPlayer):
64
70
65
71
def test_init (self ):
66
72
"""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 )
73
+ player = axelrod .InversePunisher ()
74
+ self .assertEqual (player .mem_length , 1 )
75
+ self .assertFalse (player .grudged )
76
+ self .assertEqual (player .grudge_memory , 1 )
72
77
73
78
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 })
79
+ opponent = axelrod .Alternator ()
80
+ actions = [(C , C ), (C , D ), (D , C )]
81
+ self .versus_test (opponent = opponent , expected_actions = actions ,
82
+ attrs = {"grudged" : True , "grudge_memory" : 0 })
83
+
84
+ opponent = axelrod .MockPlayer ([C , D ] + [C ] * 10 )
85
+ actions = [(C , C ), (C , D )] + [(D , C )] * 11
86
+ self .versus_test (opponent = opponent , expected_actions = actions ,
87
+ attrs = {"grudged" : True , "grudge_memory" : 10 })
88
+
81
89
# 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 })
90
+ opponent = axelrod .MockPlayer ([C , D ] + [C ] * 10 )
91
+ actions = [(C , C ), (C , D )] + [(D , C )] * 11 + [(C , D )]
92
+ self .versus_test (opponent = opponent , expected_actions = actions ,
93
+ attrs = {"grudged" : False , "grudge_memory" : 0 ,
94
+ "mem_length" : 10 })
95
+
86
96
# 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 })
97
+ opponent = axelrod .MockPlayer ([C , D ] + [C ] * 11 )
98
+ actions = [(C , C ), (C , D )] + [(D , C )] * 11 + [(C , C ), (C , D ), (D , C )]
99
+ self .versus_test (opponent = opponent , expected_actions = actions ,
100
+ attrs = {"grudged" : True , "grudge_memory" : 0 ,
101
+ "mem_length" : 17 })
90
102
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
103
96
104
class TestLevelPunisher (TestPlayer ):
97
105
@@ -111,11 +119,18 @@ def test_strategy(self):
111
119
# Starts by Cooperating
112
120
self .first_play_test (C )
113
121
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 ])
122
+ # Cooperates if the turns played are less than 10.
123
+ actions = [(C , C )] * 9
124
+ self .versus_test (opponent = axelrod .Cooperator (),
125
+ expected_actions = actions )
126
+
127
+ # After 10 rounds
128
+ # Check if number of defections by opponent is greater than 20%
129
+ opponent = axelrod .MockPlayer ([C ] * 4 + [D ] * 2 + [C ] * 3 + [D ])
130
+ actions = [(C , C )] * 4 + [(C , D )] * 2 + [(C , C )] * 3 + [(C , D ), (D , C )]
131
+ self .versus_test (opponent = opponent , expected_actions = actions )
132
+
133
+ # Check if number of defections by opponent is less than 20%
134
+ opponent = axelrod .MockPlayer ([C ] * 4 + [D ] + [C ] * 4 + [D ])
135
+ actions = [(C , C )] * 4 + [(C , D )] + [(C , C )] * 4 + [(C , D ), (C , C )]
136
+ self .versus_test (opponent = opponent , expected_actions = actions )
0 commit comments