-
Notifications
You must be signed in to change notification settings - Fork 3
/
strategies.py
125 lines (109 loc) · 3.52 KB
/
strategies.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/python
import random
def tit4tat(iteration_count, player, oppo, player_moves, oppo_moves):
"""C first, then plays opponent's last move"""
if iteration_count == 0: return 'C'
opp_last_move = oppo_moves[-1]
if opp_last_move == 'C': return 'C'
elif opp_last_move == 'D': return 'D'
def alwaysC(iteration_count, player, oppo, player_moves, oppo_moves):
return 'C'
def alwaysD(iteration_count, player, oppo, player_moves, oppo_moves):
return 'D'
def spiteful(iteration_count, player, oppo, player_moves, oppo_moves):
"""plays C until opponent plays D, then always D"""
if iteration_count == 0: return 'C'
if oppo_moves.count('D') > 0: return 'D'
else: return 'C'
def soft_majo(iteration_count, player, oppo, player_moves, oppo_moves):
"""return opponent's majority move, but plays C if equal"""
num_C = oppo_moves.count('C'); num_D = oppo_moves.count('D')
if num_D > num_C: return 'D'
else: return 'C'
def hard_majo(iteration_count, player, oppo, player_moves, oppo_moves):
"""plays opponent's majority move, if equal then plays 'D'"""
num_C = oppo_moves.count('C'); num_D = oppo_moves.count('D')
if num_C > num_D: return 'C'
else: return 'D'
def per_ddc(iteration_count, player, oppo, player_moves, oppo_moves):
"""rotate D, D, C"""
if len(oppo_moves) % 3 == 0: return 'C'
else: return 'D'
def per_ccd(iteration_count, player, oppo, player_moves, oppo_moves):
"""rotate C, C, D"""
if len(oppo_moves) % 3 == 0: return 'D'
else: return 'C'
def mistrust(iteration_count, player, oppo, player_moves, oppo_moves):
"""defects first, then plays opponent's last move"""
if iteration_count == 0: return 'D'
opp_last_move = oppo_moves[-1]
if opp_last_move == 'C': return 'C'
elif opp_last_move == 'D': return 'D'
def per_cd(iteration_count, player, oppo, player_moves, oppo_moves):
"""rotate C, D"""
if iteration_count % 2 == 0: return 'C'
else: return 'D'
def pavlov(iteration_count, player, oppo, player_moves, oppo_moves):
"""C if both opponents had same move on last move"""
if iteration_count == 0: return 'C'
player_last_move = player_moves[-1]; opp_last_move = oppo_moves[-1]
if player_last_move == opp_last_move: return 'C'
else: return 'D'
def tit4tat2(iteration_count, player, oppo, player_moves, oppo_moves):
"""C except if opponent plays D twice consecutively"""
if iteration_count < 2: return 'C'
opp_last_2moves = oppo_moves[-2:]
if opp_last_2moves == ['D','D']: return 'D'
else: return 'C'
def hard_t4t(iteration_count, player, oppo, player_moves, oppo_moves):
"""C except if opponent played D at least once in last 2 moves"""
if iteration_count == 0: return 'C'
if iteration_count == 1 and oppo_moves[-1] == 'D': return 'D'
opp_last2moves_Dcount = oppo_moves[-2:].count('D')
if opp_last2moves_Dcount > 0: return 'D'
else: return 'C'
def soft_t4t(iteration_count, player, oppo, player_moves, oppo_moves):
"""round 0 and 1 will be C, then if opponent has same move 2 consec turns, follows"""
if iteration_count < 2: return 'C'
opp_last_2moves = oppo_moves[-2:]
if opp_last_2moves == ['D','D']: return 'D'
else: return 'C'
def rando(iteration_count, player, oppo, player_moves, oppo_moves):
"""50/50 chance for C/D"""
n = random.randint(1,2)
if n == 1: return 'C'
else: return 'D'
ALL = [
tit4tat,
tit4tat2,
soft_t4t,
hard_t4t,
mistrust,
alwaysC,
alwaysD,
spiteful,
soft_majo,
hard_majo,
per_ddc,
per_ccd,
per_cd,
pavlov,
rando,
]
SELECTED = [
tit4tat,
tit4tat2,
soft_t4t,
hard_t4t,
mistrust,
alwaysC,
alwaysD,
spiteful,
soft_majo,
hard_majo,
per_ddc,
per_ccd,
per_cd,
pavlov,
rando,
]