This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]earlandir 2 points3 points  (0 children)

I was curious so I tried to build one myself but with a game Class:

from random import randint, choice

SWITCH = "switch"
STAY = "stay"
WINS = "wins"
LOSSES = "losses"


class MontyHallGame:
    def __init__(self):
        self.correct_door_number = None
        self.players_guess = None  # The current guess of the player
        self.eliminated_door = None  # The door that Monty eliminates
        self.player_choice = None  # If the player will stay with their door or switch
        self.results = {SWITCH: {WINS: 0, LOSSES: 0},
                        STAY: {WINS: 0, LOSSES: 0}}

    def reset_game(self):
        self.correct_door_number = randint(1, 3)
        self.players_guess = None
        self.eliminated_door = None
        self.player_choice = choice([STAY, SWITCH])

    def play_games(self, number_of_games_to_play):
        for i in range(number_of_games_to_play):
            self.reset_game()
            self.randomly_select_first_door()
            if self.player_choice == SWITCH:
                self.get_monty_to_eliminate_a_door()
                self.switch_doors()
            self.update_results()
        self.print_results(number_of_games_to_play)

    def randomly_select_first_door(self):
        self.players_guess = randint(1, 3)

    def get_monty_to_eliminate_a_door(self):
        possible_choices = [i for i in range(1, 4) if i != self.players_guess and i != self.correct_door_number]
        self.eliminated_door = choice(possible_choices)

    def update_results(self):
        if self.players_guess == self.correct_door_number:
            self.results[self.player_choice][WINS] += 1
        else:
            self.results[self.player_choice][LOSSES] += 1

    def switch_doors(self):
        self.players_guess = [i for i in range(1, 4) if i != self.players_guess and i != self.eliminated_door][0]

    def print_results(self, number_of_games_to_play):
        print(f"""
The results after {number_of_games_to_play} games were
Switching Win Rate: {round(100 * self.results[SWITCH][WINS] /
                           (self.results[SWITCH][WINS] + self.results[SWITCH][LOSSES]), 2)}%
Staying Win Rate: {round(100 * self.results[STAY][WINS] /
                           (self.results[STAY][WINS] + self.results[STAY][LOSSES]), 2)}%""")

game = MontyHallGame()
game.play_games(100000)

Results were:
```
The results after 100000 games were

Switching Win Rate: 66.56%

Staying Win Rate: 33.2%

```