from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import random doc = """ This is a one-shot "Prisoner's Dilemma". Two players are asked separately whether they want to cooperate or defect. Their choices directly determine the payoffs. """ class Constants(BaseConstants): name_in_url = 'pt1l' players_per_group = 4 num_rounds = 12 instructions_template = 'prisoner_training_notamt/instructions.html' # payoff if 1 player defects and the other cooperates""", betray_payoff = c(0) betrayed_payoff = c(10) # payoff if both players cooperate or both defect both_cooperate_payoff = c(2) both_defect_payoff = c(6) class Subsession(BaseSubsession): def creating_session(self): computer_choice = ['Cooperate', 'Defect'] self.session.vars['computer_choice'] = computer_choice #self.session.vars['start_time'] = time.time() class Group(BaseGroup): pass class Player(BasePlayer): decision = models.StringField( choices=['Cooperate', 'Defect'], doc="""This player's decision""", widget=widgets.RadioSelect ) skill_answer1 = models.StringField( choices=['Player 1', 'Player 2'], doc="""This player's decision""", widget=widgets.RadioSelect ) skill_answer2 = models.StringField( choices=['£0.10', '£2.00', '£0.06', '£0.01'], doc="""This player's decision""", widget=widgets.RadioSelect ) skill_answer3 = models.StringField( choices=['Confess', 'Deny'], doc="""This player's decision""", widget=widgets.RadioSelect ) #models.StringField( #choices=['Cooperate', 'Defect'], #doc="""Computer's choice""", #) computer_decision = models.StringField(initial='None') def other_player(self): computer_choice_made = random.choice(self.session.vars['computer_choice']) return computer_choice_made #return self.get_others_in_group()[0] #options = ['Cooperate', 'Defect'] #self.session.vars['options'] = options def set_payoff(self): self.computer_decision = self.other_player() payoff_matrix = { 'Cooperate': { 'Cooperate': Constants.both_cooperate_payoff, 'Defect': Constants.betrayed_payoff }, 'Defect': { 'Cooperate': Constants.betray_payoff, 'Defect': Constants.both_defect_payoff } } self.payoff = payoff_matrix[self.decision][self.computer_decision]