Skip to content

Commit 43ee514

Browse files
rnjdmarcharper
authored andcommitted
Added strategies Desperate, Willing, Hopeless, Grim
Adding tests Desperate, Hopeless, Willing and Grim in mutual.py
1 parent efcaa2e commit 43ee514

File tree

3 files changed

+151
-5
lines changed

3 files changed

+151
-5
lines changed

axelrod/strategies/_strategies.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
ZDGen2, ZDGTFT2, ZDSet2, WinStayLoseShift, WinShiftLoseStay)
4444
from .mindcontrol import MindController, MindWarper, MindBender
4545
from .mindreader import MindReader, ProtectedMindReader, MirrorMindReader
46+
from .mutual import Desperate, Hopeless, Willing
4647
from .oncebitten import OnceBitten, FoolMeOnce, ForgetfulFoolMeOnce, FoolMeForever
4748
from .prober import (Prober, Prober2, Prober3, HardProber,
4849
NaiveProber, RemorsefulProber)
@@ -92,6 +93,7 @@
9293
Davis,
9394
Defector,
9495
DefectorHunter,
96+
Desperate,
9597
DoubleCrosser,
9698
Eatherley,
9799
EventualCycleHunter,
@@ -115,21 +117,22 @@
115117
GoByMajority20,
116118
GoByMajority40,
117119
GoByMajority5,
120+
Golden,
121+
Gradual,
122+
Grofman,
123+
Grudger,
124+
Grumpy,
118125
Handshake,
119126
HardGoByMajority,
120127
HardGoByMajority10,
121128
HardGoByMajority20,
122129
HardGoByMajority40,
123130
HardGoByMajority5,
124-
Golden,
125-
Gradual,
126-
Grofman,
127-
Grudger,
128-
Grumpy,
129131
HardProber,
130132
HardTitFor2Tats,
131133
HardTitForTat,
132134
HesitantQLearner,
135+
Hopeless,
133136
Inverse,
134137
InversePunisher,
135138
Joss,
@@ -184,6 +187,7 @@
184187
TrickyDefector,
185188
Tullock,
186189
TwoTitsForTat,
190+
Willing,
187191
WinShiftLoseStay,
188192
WinStayLoseShift,
189193
ZDExtort2,

axelrod/strategies/mutual.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from axelrod import Actions, Player, init_args
2+
3+
C, D = Actions.C, Actions.D
4+
5+
class Desperate(Player):
6+
"""A player that only cooperates after mutual defection"""
7+
name = "Desperate"
8+
classifier = {
9+
'memory_depth': 1,
10+
'stochastic': False,
11+
'makes_use_of': set(),
12+
'inspects_source': False,
13+
'manipulates_source': False,
14+
'manipulates_state': False
15+
}
16+
17+
def strategy(self, opponent):
18+
if not opponent.history:
19+
return D
20+
if self.history[-1] == D and opponent.history[-1] == D:
21+
return C
22+
return D
23+
24+
class Hopeless(Player):
25+
"""A player that only defects after mutual cooperation"""
26+
name = "Hopeless"
27+
classifier = {
28+
'memory_depth': 1,
29+
'stochastic': False,
30+
'makes_use_of': set(),
31+
'inspects_source': False,
32+
'manipulates_source': False,
33+
'manipulates_state': False
34+
}
35+
36+
def strategy(self, opponent):
37+
if not opponent.history:
38+
return C
39+
if self.history[-1] == C and opponent.history[-1] == C:
40+
return D
41+
return C
42+
43+
class Willing(Player):
44+
"""A player that only defects after mutual defection"""
45+
name = "Willing"
46+
classifier = {
47+
'memory_depth': 1,
48+
'stochastic': False,
49+
'makes_use_of': set(),
50+
'inspects_source': False,
51+
'manipulates_source': False,
52+
'manipulates_state': False
53+
}
54+
55+
def strategy(self, opponent):
56+
if not opponent.history:
57+
return C
58+
if self.history[-1] == D and opponent.history[-1] == D:
59+
return D
60+
return C

axelrod/tests/unit/test_mutual.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
"""Tests for strategies Desperate, Hopeless, Willing, and Grim"""
2+
import axelrod
3+
4+
from .test_player import TestPlayer
5+
6+
C, D = axelrod.Actions.C, axelrod.Actions.D
7+
8+
class TestDesperate(TestPlayer):
9+
10+
name = "Desperate"
11+
player = axelrod.Desperate
12+
expected_classifier = {
13+
'memory_depth': 1,
14+
'stochastic': False,
15+
'makes_use_of': set(),
16+
'inspects_source': False,
17+
'manipulates_source': False,
18+
'manipulates_state': False
19+
}
20+
21+
def test_strategy(self):
22+
"""
23+
Test that initial strategy defects.
24+
"""
25+
self.first_play_test(D)
26+
27+
def test_responses(self):
28+
29+
self.responses_test([C]* 4, [D] * 4, [D])
30+
self.responses_test([D, D, C], [C, C, D], [D])
31+
self.responses_test([D, D, D], [C, C, D], [C])
32+
33+
34+
class TestHopeless(TestPlayer):
35+
36+
name = "Hopeless"
37+
player = axelrod.Hopeless
38+
expected_classifier = {
39+
'memory_depth': 1,
40+
'stochastic': False,
41+
'makes_use_of': set(),
42+
'inspects_source': False,
43+
'manipulates_source': False,
44+
'manipulates_state': False
45+
}
46+
47+
def test_strategy(self):
48+
"""
49+
Test that initial strategy cooperates.
50+
"""
51+
self.first_play_test(C)
52+
53+
def test_responses(self):
54+
55+
self.responses_test([C] * 4, [D] * 4, [C])
56+
self.responses_test([D] * 5, [C] * 5, [C])
57+
self.responses_test([C, D, C], [C, C, C], [D])
58+
59+
class TestWilling(TestPlayer):
60+
61+
name = "Willing"
62+
player = axelrod.Willing
63+
expected_classifier = {
64+
'memory_depth': 1,
65+
'stochastic': False,
66+
'makes_use_of': set(),
67+
'inspects_source': False,
68+
'manipulates_source': False,
69+
'manipulates_state': False
70+
}
71+
72+
def test_strategy(self):
73+
"""
74+
Test that initial strategy cooperates.
75+
"""
76+
self.first_play_test(C)
77+
78+
def test_responses(self):
79+
80+
self.responses_test([C] * 4, [D] * 4, [C])
81+
self.responses_test([D] * 5, [C] * 5, [C])
82+
self.responses_test([C, C, D], [C, C, D], [D])

0 commit comments

Comments
 (0)