import random class Personality(): def __init__(self, name = "Unnamed"): self.name = name self.score = 0 def getName(self): return self.name def setName(self, name): self.name = name def getScore(self): return self.score def setScore(self, newScore): self.score = newScore def addToScore(self, points): self.score += points class Human(Personality): def cheat(self, opponentCheated): print self.getName() + ", it is your turn." while True: response = raw_input("Do you cheat? Type Y or N: ") if response == "y" or response == "Y": return True elif response == "n" or response == "N": return False else: print "Invalid" class TitForTat(Personality): def cheat(self, opponentCheated): return opponentCheated class Random(Personality): def cheat(self, opponentCheated): return random.choice([True, False]) class AlwaysCheat(Personality): def cheat(self, opponentCheated): return True class NeverCheat(Personality): def cheat(self, opponentCheated): return False class GrimTrigger(Personality): def __init__(self): self.trigger = False def setTrigger(self): #toggles trigger self.trigger = not self.trigger def getTrigger(self): return self.trigger def cheat(self, opponentCheated): if opponentCheated and not self.getTrigger(): self.setTrigger() return self.getTrigger() def play(player1, player2, turnsToPlay): turn = 0 player1.setScore(0) player2.setScore(0) # Neither player has cheated yet. In general, the AI scripts will begin by assuming good faith. # Source: page 161 "You begin by cooperating on the first round." for tit-for-tat strategy player1Cheated = False player2Cheated = False while turn < turnsToPlay: turn += 1 print print "Turn " + str(turn) print player1.getName() + ": $" + str(player1.getScore()) print player2.getName() + ": $" + str(player2.getScore()) player1WillCheat = player1.cheat(player2Cheated) player2WillCheat = player2.cheat(player1Cheated) if player1WillCheat and player2WillCheat: print "%s cheats. %s cheats." % (player1.getName(), player2.getName()) print "%s and %s are competing and make medicore profits." % (player1.getName(), player2.getName()) player1.addToScore(2) player2.addToScore(2) elif player1WillCheat and not player2WillCheat: print "%s cheats. %s cooperates." % (player1.getName(), player2.getName()) print "%s made high profits at %s's expense." % (player1.getName(), player2.getName()) player1.addToScore(4) player2.addToScore(1) elif not player1WillCheat and player2WillCheat: print "%s cooperates. %s cheats." % (player1.getName(), player2.getName()) print "%s made high profits at %s's expense." % (player2.getName(), player1.getName()) player1.addToScore(1) player2.addToScore(4) elif not player1WillCheat and not player2WillCheat: print "%s cooperates. %s cooperates." % (player1.getName(), player2.getName()) print "%s and %s control the market and make moderate profits." % (player1.getName(), player2.getName()) player1.addToScore(3) player2.addToScore(3) player1Cheated = player1WillCheat player2Cheated = player2WillCheat print "\nFinal outcome:" print player1.getName() + ": $" + str(player1.getScore()) print player2.getName() + ": $" + str(player2.getScore()) return (player1.getScore(), player2.getScore()) def about(): print "This is a simple duopoly simulation. On each turn, players will have an opportunity to cheat on their agreement." print "If both players cheat, they will be in competition and make medicore profits. (2;2)" print "If only one player cheats, the cheater will make high profits and the other player will make low profits. (4;1) or (1;4)" print "If neither cheat, both players will control the market and make moderate profits. (3;3)" def createPlayer(): while True: personalities = {1: Human(), 2: AlwaysCheat(), 3: NeverCheat(), 4: Random(), 5: TitForTat(), 6: GrimTrigger()} print "Choose a personality type:" print "1: Human (you will play)" print "2: Always cheat" print "3: Never cheat" print "4: Randomly cheat" print "5: Tit-for-Tat strategy" print "6: Grim trigger strategy" try: return personalities[int(raw_input("Enter a number from 1-6: "))] except: print "Invalid" def initialize(): about() print "\nPLAYER 1:\n" player1 = createPlayer() player1.setName(raw_input("Enter player 1's name: ")) print "\nPLAYER 2:\n" player2 = createPlayer() player2.setName(raw_input("Enter player 2's name: ")) while True: try: numberOfTurns = int(raw_input("Enter turns to play: ")) break except: "Please enter a valid integer" while True: try: numberOfTrials = int(raw_input("Enter number of games: ")) break except: "Please enter a valid integer" trial = 0 player1Outcomes = [] player2Outcomes = [] while trial < numberOfTrials: outcome = play(player1, player2, numberOfTurns) player1Outcomes.append(outcome[0]) player2Outcomes.append(outcome[1]) trial += 1 player1Average = sum(player1Outcomes)/len(player1Outcomes) player2Average = sum(player2Outcomes)/len(player2Outcomes) print "\nFINAL AVERAGES" print player1.getName() + ": $" + str(player1Average) print player2.getName() + ": $" + str(player2Average) initialize()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question