-
Notifications
You must be signed in to change notification settings - Fork 12
/
example4.py
49 lines (43 loc) · 1.96 KB
/
example4.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
####
# Each team's file must define four tokens:
# team_name: a string
# strategy_name: a string
# strategy_description: a string
# move: A function that returns 'c' or 'b'
####
team_name = 'E4'
strategy_name = 'Use early history'
strategy_description = '''\
Collude first round. Compare all rounds to the previous round and
assume opponent will behave the same as the first time the previous
round's result occurred. If the previous round's result never has
happened, collude except after being severly punished.'''
def move(my_history, their_history, my_score, their_score):
'''Make my move based on the history with this player.
history: a string with one letter (c or b) per round that has been played with this opponent.
their_history: a string of the same length as history, possibly empty.
The first round between these two players is my_history[0] and their_history[0]
The most recent round is my_history[-1] and their_history[-1]
Returns 'c' or 'b' for collude or betray.
'''
if len(my_history)==0: # It's the first round: collude
return 'c'
else:
# If there was a previous round just like the last one,
# do whatever they did in the round that followed it
# Reference last round
recent_round_them = their_history[-1]
recent_round_me = my_history[-1]
# Look at rounds before that one
for round in range(len(my_history)-1):
prior_round_them = their_history[round]
prior_round_me = my_history[round]
# If one matches
if (prior_round_me == recent_round_me) and \
(prior_round_them == recent_round_them):
return their_history[round]
# No match found
if my_history[-1]=='c' and their_history[-1]=='b':
return 'b' # Betray if they were severely punished last time
else:
return 'c' # Otherwise collude.