forked from Axelrod-Python/Axelrod
-
Notifications
You must be signed in to change notification settings - Fork 20
/
oncebitten.py
107 lines (90 loc) · 2.69 KB
/
oncebitten.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import random
from axelrod import Player
class OnceBitten(Player):
"""
Co-operates once when the opponent defects, but if they defect twice in a row defaults to forgetful grudger for 10 turns defecting
"""
name = 'Once Bitten'
def __init__(self):
"""
Initialised the player
"""
super(OnceBitten, self).__init__()
self.history = []
self.score = 0
self.mem_length = 10
self.grudged = False
self.grudge_memory = 0
self.stochastic = False
def strategy(self, opponent):
"""
Begins by playing C, then plays D for mem_length rounds if the opponent ever plays D twice in a row
"""
if self.grudge_memory >= self.mem_length:
self.grudge_memory = 0
self.grudged = False
if len(opponent.history) < 2:
return 'C'
if self.grudged:
self.grudge_memory += 1
return 'D'
elif not ('C' in opponent.history[-2:]):
self.grudged = True
return 'D'
return 'C'
def reset(self):
"""
Resets scores and history
"""
self.history = []
self.grudged = False
self.grudge_memory = 0
class FoolMeOnce(Player):
"""
Forgives one D then retaliates forever on a second D.
"""
name = 'Fool Me Once'
def __init__(self):
"""
Initialised the player
"""
super(FoolMeOnce, self).__init__()
self.D_count = 0
self._initial = 'C'
self.stochastic = False
def strategy(self, opponent):
if not opponent.history:
return self._initial
if opponent.history[-1] == 'D':
self.D_count += 1
if self.D_count > 1:
return 'D'
return 'C'
def reset(self):
self.D_count = 0
self.history = []
class ForgetfulFoolMeOnce(Player):
"""
Forgives one D then retaliates forever on a second D. Sometimes randomly forgets the defection count.
"""
name = 'Forgetful Fool Me Once'
def __init__(self, forget_probability=0.1):
super(ForgetfulFoolMeOnce, self).__init__()
self.D_count = 0
self._initial = 'C'
self.forget_probability = forget_probability
self.stochastic = True
def strategy(self, opponent):
r = random.random()
if not opponent.history:
return self._initial
if opponent.history[-1] == 'D':
self.D_count += 1
if r < self.forget_probability:
self.D_count = 0
if self.D_count > 1:
return 'D'
return 'C'
def reset(self):
self.D_count = 0
self.history = []