from otree.api import * doc = """ This is a one-shot "Prisoner's Dilemma". In this game, the two players are two prisoners, who are asked separately whether they want to confess or not a crime. Their choices directly determine the years to be spent in prison. """ class C(BaseConstants): NAME_IN_URL = 'prisoner' PLAYERS_PER_GROUP = 2 NUM_ROUNDS = 1 INSTRUCTIONS_TEMPLATE = 'PrisonerDilemma/instructions.html' PAYOFF_A = 7 PAYOFF_B = 6 PAYOFF_C = 1 PAYOFF_D = 0 class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): Confess = models.BooleanField( choices=[[True, 'Confess'], [False, 'Not confess']], doc="""This player's decision""", widget=widgets.RadioSelect, ) # FUNCTIONS def set_payoffs(group: Group): for p in group.get_players(): set_payoff(p) def other_player(player: Player): return player.get_others_in_group()[0] def set_payoff(player: Player): payoff_matrix = { (False, True): C.PAYOFF_A, (True, True): C.PAYOFF_B, (False, False): C.PAYOFF_C, (True, False): C.PAYOFF_D, } other = other_player(player) player.payoff = payoff_matrix[(player.Confess, other.Confess)] # PAGES class Introduction(Page): timeout_seconds = 300 class Decision(Page): form_model = 'player' form_fields = ['Confess'] class ResultsWaitPage(WaitPage): after_all_players_arrive = set_payoffs class Results(Page): @staticmethod def vars_for_template(player: Player): opponent = other_player(player) return dict( opponent=opponent, same_choice=player.Confess == opponent.Confess, my_decision=player.field_display('Confess'), opponent_decision=opponent.field_display('Confess'), my_payoff=int(player.payoff) ) page_sequence = [Introduction, Decision, ResultsWaitPage, Results]