Skip to content

Instantly share code, notes, and snippets.

@JasYoung315
Created December 11, 2014 12:04
Show Gist options
  • Save JasYoung315/d7081ae0e069e1709f6a to your computer and use it in GitHub Desktop.
Save JasYoung315/d7081ae0e069e1709f6a to your computer and use it in GitHub Desktop.
import random
class Prisoner():
def __init__(self, choice, years, strategy):
self.choice = choice
self.years = 0
self.strategy = []
self.switch = False
def optimist(self, Player, Oponent):
if self.switch == True:
return True
for c in Oponent.strategy:
if c == 'b':
self.switch == True
Player.choice == True
return Player.choice == False
def tit_for_tat(self, Player, Oponent):
if Opponent.strategy == []:
return True
if Oponent.strategy[-1] == 'b':
return True
return Player.choice == False
def random_choice(self):
return random.choice([True, False])
def karma(self, Player, Oponent):
if Player.random_choice():
Player.strategy.append('b')
else:
Player.strategy.append('c')
return Oponent.strategy == Player.strategy[-1]
r = random.choice([True, False])
p1 = Prisoner(r, 0, 0)
p2 = Prisoner(r, 0, 0)
def gamevrandom(Player, Oponent):
Oponentchoice = Oponent.random_choice()
Playerchoice = Player.random_choice()
if Playerchoice and Oponentchoice:
Player.strategy.append('b')
Oponent.strategy.append('b')
Player.years += 3
Oponent.years += 3
if Playerchoice == False and Oponentchoice == False:
Player.strategy.append('c')
Oponent.strategy.append('c')
Player.years += 2
Oponent.years += 2
if Playerchoice and Oponentchoice == False:
Player.strategy.append('b')
Oponent.strategy.append('c')
Oponent.years += 4
elif Playerchoice == False and Oponentchoice:
Player.strategy.append('c')
Oponent.strategy.append('b')
Player.years += 4
return Player.years, Oponent.years, Player.strategy, Oponent.strategy
def gamevoptimist(Player, Oponent):
Oponentchoice = Oponent.random_choice()
Playerchoice = Player.optimist(Player, Oponent)
if Player.optimist() and Oponentchoice:
Player.strategy.append('b')
Oponent.strategy.append('b')
Player.years += 3
Oponent.years += 3
if Player.optimist() == False and Oponentchoice == False:
Player.strategy.append('c')
Oponent.strategy.append('c')
Player.years += 2
Oponent.years += 2
if Player.optimist() and Oponentchoice == False:
Player.strategy.append('b')
Oponent.strategy.append('c')
Oponent.years += 4
elif Player.optimist() == False and Oponentchoice:
Player.strategy.append('c')
Oponent.strategy.append('b')
Player.years += 4
return Player.years, Oponent.years, Player.strategy, Oponent.strategy
def karmagame(Player, Oponent):
Oponentchoice = Oponent.random_choice()
if Player.karma(Player, Oponent) and Oponentchoice:
Player.strategy.append('b')
Oponent.strategy.append('b')
Player.years += 3
Oponent.years += 3
if Player.karma(Player, Oponent) == False and Oponentchoice == False:
Player.strategy.append('c')
Oponent.strategy.append('c')
Player.years += 2
Oponent.years += 2
if Player.karma(Player, Oponent) and Oponentchoice == False:
Player.strategy.append('b')
Oponent.strategy.append('c')
Oponent.years += 4
elif Player.karma(Player, Oponent) == False and Oponentchoice:
Player.strategy.append('c')
Oponent.strategy.append('b')
Player.years += 4
return Player.years, Oponent.years, Player.strategy, Oponent.strategy
def titfortatgame(Player, Oponent) :
Oponentchoice = Oponent.random_choice()
Oponent.strategy.append.random.choice(['b', 'c'])
if Player.tit_for_tat(Player, Oponent) and Oponentchoice:
Player.strategy.append('b')
Oponent.strategy.append('b')
Player.years += 3
Oponent.years += 3
if Player.tit_for_tat(Player, Oponent) == False and Oponentchoice == False:
Player.strategy.append('c')
Oponent.strategy.append('c')
Player.years += 2
Oponent.years += 2
if Player.tit_for_tat(Player, Oponent) and Oponentchoice == False:
Player.strategy.append('b')
Oponent.strategy.append('c')
Oponent.years += 4
elif Player.tit_for_tat(Player, Oponent) == False and Oponentchoice:
Player.strategy.append('c')
Oponent.strategy.append('b')
Player.years += 4
return Player.years, Oponent.years, Player.strategy, Oponent.strategy
def iterate(n, Player, Oponent):
for i in range(n):
gamevoptimist(Player, Oponent)
return Player.years, Oponent.years, Player.strategy, Oponent.strategy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment