from random import random from otree.api import * from otree.models import subsession doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'monty_hall_transparent' players_per_group = None num_rounds = 5 num_doors = 100 num_rows = 10 class Subsession(BaseSubsession): answer = models.IntegerField() another_door = models.IntegerField() class Group(BaseGroup): game_over = models.BooleanField(initial=False) stage = models.IntegerField(initial=1) door_keep = models.IntegerField(initial=101) class Player(BasePlayer): win = models.BooleanField(initial=False) first_choice = models.IntegerField(initial=101) old_choice = models.IntegerField(initial=101) final_choice = models.IntegerField(initial=101) switch = models.BooleanField() def creating_session(subsession: Subsession): import random subsession.answer = random.randint(1, Constants.num_doors) subsession.another_door = subsession.answer while subsession.another_door == subsession.answer: subsession.another_door = random.randint(1, Constants.num_doors) # PAGES class First_Stage(Page): @staticmethod def js_vars(player: Player): subsession = player.subsession return dict(num_doors = Constants.num_doors, num_rows = Constants.num_rows, another_door = subsession.another_door, answer = subsession.answer) @staticmethod def live_method(player: Player, data): import random group = player.group subsession = player.subsession if (data and data in range(1,101)): if (group.stage == 1): player.old_choice = player.first_choice player.first_choice = data else: player.old_choice = player.final_choice player.final_choice = data if (data and data == 'next' and group.stage == 2): group.stage = 3 # temporarily set final_choice as first_choice elif (data and data == 'next' and player.first_choice != 101 and group.stage == 1): group.stage = 2 player.final_choice = player.first_choice if player.first_choice == subsession.answer: group.door_keep = player.first_choice while group.door_keep == player.first_choice: group.door_keep = random.randint(1, Constants.num_doors) else: group.door_keep = subsession.answer elif (data and data == 'next' and group.stage == 3): group.game_over = True # debug # print(player.first_choice) # print(group.door_keep) return { 0: dict( game_over=group.game_over, first_choice = player.first_choice, final_choice = player.final_choice, old_choice = player.old_choice, stage = group.stage, door_keep = group.door_keep ) } class ResultsWaitPage(WaitPage): @staticmethod def after_all_players_arrive(group: Group): player = group.get_player_by_id(1) subsession = group.subsession if player.final_choice == subsession.answer: player.win = True class Results(Page): pass page_sequence = [First_Stage, ResultsWaitPage, Results]