Created
December 8, 2014 13:57
-
-
Save drvinceknight/ff06d8b936cc35820e04 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
def utility(s1, s2): | |
if s1 == s2 == 'B': | |
return 4,4 | |
if s1 == s2 == 'C': | |
return 2,2 | |
if s1 == 'C' and s2 == 'B': | |
return 5,0 | |
return 0,5 | |
p1strategies = [] | |
p2strategies = [] | |
utilities = [(0,0)] | |
# Two players playing randomly | |
while len(p1strategies) < 5: | |
p1strategies.append(random.choice(['B', 'C'])) | |
p2strategies.append(random.choice(['B', 'C'])) | |
thisround = utility(p1strategies[-1], p2strategies[-1]) | |
utilities.append((utilities[-1][0] + thisround[0], utilities[-1][1] + thisround[1])) | |
print '--------' | |
print p1strategies | |
print p2strategies | |
print utilities | |
print 50 * "=" | |
# One player playing tit for tat | |
def tit_for_tat(s1): | |
if s1 == 'B': | |
return 'B' | |
return 'C' | |
p1strategies = [] | |
p2strategies = [] | |
utilities = [(0,0)] | |
while len(p1strategies) < 5: | |
p1strategies.append(random.choice(['B', 'C'])) | |
if len(p2strategies) == 0: | |
p2strategies.append('C') | |
else: | |
p2strategies.append(tit_for_tat(p1strategies[-2])) | |
thisround = utility(p1strategies[-1], p2strategies[-1]) | |
utilities.append((utilities[-1][0] + thisround[0], utilities[-1][1] + thisround[1])) | |
print '--------' | |
print p1strategies | |
print p2strategies | |
print utilities |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment