stuff
This commit is contained in:
56
examples/monty-hall-problem/README.md
Normal file
56
examples/monty-hall-problem/README.md
Normal file
@ -0,0 +1,56 @@
|
||||
50/50 for ever door all the time!
|
||||
|
||||
The Monty Hall Problem
|
||||
======================
|
||||
|
||||
Loosely based on the 1960s game show "Let's Make a Deal", the Monty Hall problem is a paradox in probability.
|
||||
|
||||
A player is presented with three doors from which to choose. Behind one of the doors is a brand new car. Behind the other two are goats. The winning door is completely random.
|
||||
|
||||
Obviously the player wants to win the new car, and must pick a door to open, hoping to reveal it.
|
||||
|
||||
Before it is revealed however, the host will open one of the doors for the player to reveal one of the goats. The player can choose to stick with their original choice, or change their choice and pick the one other possible door.
|
||||
|
||||
First Iteration
|
||||
---------------
|
||||
|
||||
Using the Model-View-Controller pattern, let's first build out a terminal application for users to play the game.
|
||||
|
||||
###The View
|
||||
|
||||
Let's keep all messages and user prompts inside the views file. If you would like to create a class to hold the views, or just use the file as a module is up to you.
|
||||
|
||||
Just don't hold any state in the views. Every variable, likely just user choices, should be passed back to the controller.
|
||||
|
||||
###The Models
|
||||
|
||||
We'll be using the models for two things:
|
||||
|
||||
1. To handle any "business logic" the program needs. An example would be generating what's behind each door.
|
||||
|
||||
2. To commit to the database. Let's save user names, whether they chose to switch their choice or not when prompted, and whether they won the car.
|
||||
|
||||
A model in general should not communicate with other models, and should definitely not communicate directly with views. All communication should be handled through the controller.
|
||||
|
||||
###The Controller
|
||||
|
||||
No spaghetti code! All user choices should be passed from the views back to the controller. Any business logic should be passed from the models to the controller. Any data that needs persistence should be sent from the controller to the model that writes to the db.
|
||||
|
||||
Before you move on, make sure you can play your game without error, that it tells you whether you won or not, and that it commits the correct data to the db.
|
||||
|
||||
Second Iteration
|
||||
----------------
|
||||
|
||||
Now for the fun part.
|
||||
|
||||
Copy your code into a different folder because we're going to hack it up. You shouldn't even need your views anymore.
|
||||
|
||||
Change your code so that it runs automated - the computer is the one choosing the door, and choosing whether or not to change their choice after the host's reveal. This should all be at random to ensure an equitable range of choices.
|
||||
|
||||
You should write the same data to the db as previously.
|
||||
|
||||
Run your program 100,000 times.
|
||||
|
||||
Write another program to query the database and return to you the percentage of times the choice was changed after the reveal and the car was won, and the percentage of times the choice was not changed after the reveal and the car was won.
|
||||
|
||||
Write your findings in this file. Read through the [wikipedia](http://en.wikipedia.org/wiki/Monty_Hall_problem)
|
Binary file not shown.
BIN
examples/monty-hall-problem/__pycache__/models.cpython-34.pyc
Normal file
BIN
examples/monty-hall-problem/__pycache__/models.cpython-34.pyc
Normal file
Binary file not shown.
BIN
examples/monty-hall-problem/__pycache__/views.cpython-34.pyc
Normal file
BIN
examples/monty-hall-problem/__pycache__/views.cpython-34.pyc
Normal file
Binary file not shown.
57
examples/monty-hall-problem/controller.py
Normal file
57
examples/monty-hall-problem/controller.py
Normal file
@ -0,0 +1,57 @@
|
||||
from models import Doors
|
||||
from views import View
|
||||
|
||||
class Controller:
|
||||
def __init__(self):
|
||||
self.view = View()
|
||||
self.name = self.view.get_name()
|
||||
self.start()
|
||||
|
||||
def start(self):
|
||||
|
||||
selected = False
|
||||
while selected not in ['1', '2', '3']:
|
||||
selected = self.view.display_doors()
|
||||
|
||||
self.doors = Doors(int(selected), self.name)
|
||||
|
||||
self.host_twist()
|
||||
|
||||
return True
|
||||
|
||||
def host_twist(self):
|
||||
|
||||
message =""
|
||||
|
||||
for i in range(1, 4):
|
||||
|
||||
if i == self.doors.selected:
|
||||
message += '[ X ] '
|
||||
elif i == self.doors.open_door:
|
||||
message += '[ Goat ] '
|
||||
else:
|
||||
message += '[ ' + str(i) + ' ] '
|
||||
other_selection = i
|
||||
selection = False
|
||||
while selection not in ['n', 'y', '']:
|
||||
selection = self.view.host_twist(message).lower()
|
||||
|
||||
if selection == 'y':
|
||||
self.doors.selected = other_selection
|
||||
self.doors.switch = True
|
||||
return self.out_come()
|
||||
|
||||
def out_come(self):
|
||||
message = 'LOSE'
|
||||
if self.doors.out_come():
|
||||
message = 'WIN'
|
||||
|
||||
replay = self.view.out_come(message)
|
||||
|
||||
if replay in ['', 'y']:
|
||||
return self.start()
|
||||
|
||||
return exit()
|
||||
|
||||
if __name__ == '__main__':
|
||||
Controller()
|
BIN
examples/monty-hall-problem/default.db
Normal file
BIN
examples/monty-hall-problem/default.db
Normal file
Binary file not shown.
42
examples/monty-hall-problem/models.py
Normal file
42
examples/monty-hall-problem/models.py
Normal file
@ -0,0 +1,42 @@
|
||||
from peewee import *
|
||||
import random
|
||||
|
||||
db = SqliteDatabase('default.db')
|
||||
db.connect()
|
||||
|
||||
class Game(Model):
|
||||
name = CharField()
|
||||
switch = BooleanField()
|
||||
outcome = BooleanField()
|
||||
|
||||
class Meta:
|
||||
database = db
|
||||
|
||||
|
||||
class Doors:
|
||||
def __init__(self, selected, name):
|
||||
self.name = name
|
||||
self.switch = False
|
||||
self.selected = selected
|
||||
self.car = random.randint(1,3)
|
||||
self.open_door = self.host_twist()
|
||||
|
||||
def host_twist(self):
|
||||
open_door = random.randint(1,3)
|
||||
if open_door == self.car or open_door == self.selected:
|
||||
return self.host_twist()
|
||||
return open_door
|
||||
|
||||
def out_come(self):
|
||||
|
||||
win = self.selected == self.car
|
||||
Game.create(name=self.name, switch=self.switch, outcome=win)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
seed = input('Seed the database[y/n]: ').lower()
|
||||
if seed in ['y', 'yes']:
|
||||
print('seeding')
|
||||
db.create_tables([Game])
|
||||
|
||||
exit()
|
6
examples/monty-hall-problem/schema.sql
Normal file
6
examples/monty-hall-problem/schema.sql
Normal file
@ -0,0 +1,6 @@
|
||||
CREATE TABLE games (
|
||||
id INTEGER DEFAULT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT DEFAULT NULL,
|
||||
switch TEXT DEFAULT NULL,
|
||||
outcome TEXT DEFAULT NULL
|
||||
);
|
25
examples/monty-hall-problem/views.py
Normal file
25
examples/monty-hall-problem/views.py
Normal file
@ -0,0 +1,25 @@
|
||||
class View:
|
||||
def get_name(self):
|
||||
print('\033c')
|
||||
print('Welcome to the Monty Hall Problem!')
|
||||
return input('Please enter your name: ')
|
||||
|
||||
def display_doors(self):
|
||||
print('\033c')
|
||||
print('Doors: [ 1 ] [ 2 ] [ 3 ]')
|
||||
return input('Select door: ')
|
||||
|
||||
def host_twist(self, message):
|
||||
print('The host will revel where one goat is...')
|
||||
print(message)
|
||||
print('Would you like to change your selection? [n/y]')
|
||||
return input('[n]: ')
|
||||
|
||||
def out_come(self, message):
|
||||
print('You {}!!!'.format(message))
|
||||
return input('Play again?[y]')
|
||||
|
||||
if __name__ == '__main__':
|
||||
print('Please opne controller.py')
|
||||
exit()
|
||||
|
Reference in New Issue
Block a user