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 = 'prisoner_RPD' players_per_group = 2 num_rounds = 20 stopping_probability = 0.25 instructions_template = 'prisoner_RPD/Instructions.html' # payoff if 1 player defects and the other cooperates""", betray_payoff = c(50) betrayed_payoff = c(12) # payoff if both players cooperate or both defect both_cooperate_payoff = c(48) both_defect_payoff = c(25) class Subsession(BaseSubsession): def creating_session(self): if self.round_number == 1: self.group_randomly() else: self.group_like_round(1) class Group(BaseGroup): is_last_round=models.BooleanField(choices=['yes', 'no']) class Player(BasePlayer): decision = models.StringField( choices=['Cooperate', 'Defect'], doc="""This player's decision""", widget=widgets.RadioSelect ) def other_player(self): return self.get_others_in_group()[0] def set_payoff(self): 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.other_player().decision]