# -*- coding: utf-8 -*- # from __future__ import division from otree.db import models from otree.constants import BaseConstants from otree.models import BaseSubsession, BaseGroup, BasePlayer from otree import widgets from otree.common import 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. """ source_code = "https://github.com/oTree-org/oTree/tree/master/prisoner" bibliography = () links = { "Wikipedia": { "Prisoner's Dilemma": "https://en.wikipedia.org/wiki/Prisoner%27s_dilemma" } } keywords = ("Prisoner's Dilemma",) class Constants(BaseConstants): name_in_url = 'prisoner_2' players_per_group = 2 num_rounds = 2 # Points made if player defects and the other cooperates""", down_up_amount = c(75) # Points made if both players cooperate up_amount = c(125) up_down_amount = c(275) down_amount = c(225) base_points = c(0) training_1_choices = [ 'Alice gets 75 points, Bob gets 275 points', 'Alice gets 225 points, Bob gets 225 points', 'Alice gets 275 points, Bob gets 75 points', 'Alice gets 125 points, Bob gets 125 points' ] training_1_correct = training_1_choices[0] class Subsession(BaseSubsession): def before_session_starts(self): if self.round_number == 1: paying_round = random.randint(1, Constants.num_rounds) self.session.vars['paying_round'] = paying_round class Group(BaseGroup): pass class Player(BasePlayer): training_question_1 = models.CharField( choices=Constants.training_1_choices, widget=widgets.RadioSelect(), #timeout_default=Constants.training_1_choices[1] ) def is_training_question_1_correct(self): return self.training_question_1 == Constants.training_1_correct decision = models.CharField( choices=['up', 'down'], doc="""This player's decision""", widget=widgets.RadioSelect() ) def other_player(self): return self.get_others_in_group()[0] def set_payoff(self): points_matrix = {'up': {'up': Constants.up_amount, 'down': Constants.up_down_amount}, 'down': {'up': Constants.down_up_amount, 'down': Constants.down_amount}} if (self.subsession.round_number == self.session.vars['paying_round']): self.payoff = (points_matrix[self.decision] [self.other_player().decision]) else self.payoff = c(0)