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". Bot is taking over the strategy method of the players and play the game by itself. """ class Constants(BaseConstants): name_in_url = 'RPD_bot' players_per_group = None num_rounds = 2 instructions_template = 'RPD_bot/Instructions.html' # payoff if one player cooperate and the other one defects 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): pass class Group(BaseGroup): pass class Player(BasePlayer): bot_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_bot_decision(self): if self.round_number == 1: self.bot_decision = self.participant.vars['strategy0'] elif self.in_round(self.round_number - 1).bot_decision == "Cooperate" and self.in_round( self.round_number - 1).other_player().bot_decision == "Cooperate": self.bot_decision = self.participant.vars['strategy1'] elif self.in_round(self.round_number - 1).bot_decision == "Cooperate" and self.in_round( self.round_number - 1).other_player().bot_decision == "Defect": self.bot_decision = self.participant.vars['strategy2'] elif self.in_round(self.round_number - 1).bot_decision == "Defect" and self.in_round( self.round_number - 1).other_player().bot_decision == "Cooperate": self.bot_decision = self.participant.vars['strategy3'] elif self.in_round(self.round_number - 1).bot_decision == "Defect" and self.in_round( self.round_number - 1).other_player().bot_decision == "Defect": self.bot_decision = self.participant.vars['strategy4'] 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.bot_decision][self.other_player().bot_decision]