Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
037ce198e3 | |||
26469c9ae3 | |||
1ee37b4023 |
@ -1,3 +1,5 @@
|
|||||||
|
50/50 for ever door all the time!
|
||||||
|
|
||||||
The Monty Hall Problem
|
The Monty Hall Problem
|
||||||
======================
|
======================
|
||||||
|
|
||||||
|
Binary file not shown.
BIN
exercises/1-monty-hall-problem/__pycache__/models.cpython-34.pyc
Normal file
BIN
exercises/1-monty-hall-problem/__pycache__/models.cpython-34.pyc
Normal file
Binary file not shown.
BIN
exercises/1-monty-hall-problem/__pycache__/views.cpython-34.pyc
Normal file
BIN
exercises/1-monty-hall-problem/__pycache__/views.cpython-34.pyc
Normal file
Binary file not shown.
@ -1,6 +1,57 @@
|
|||||||
#from models import
|
from models import Doors
|
||||||
from views import View
|
from views import View
|
||||||
|
|
||||||
class Controller:
|
class Controller:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
pass
|
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()
|
||||||
|
print('name:', __name__)
|
||||||
|
if __name__ == '__main__':
|
||||||
|
Controller()
|
||||||
|
BIN
exercises/1-monty-hall-problem/default.db
Normal file
BIN
exercises/1-monty-hall-problem/default.db
Normal file
Binary file not shown.
@ -0,0 +1,58 @@
|
|||||||
|
import sqlite3
|
||||||
|
import random
|
||||||
|
|
||||||
|
conn = sqlite3.connect('default.db')
|
||||||
|
c = conn.cursor()
|
||||||
|
|
||||||
|
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):
|
||||||
|
|
||||||
|
if self.selected == self.car:
|
||||||
|
logDB(self.name, self.switch, True)
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
logDB(self.name, self.switch, False)
|
||||||
|
return False
|
||||||
|
|
||||||
|
def logDB(name, switch, outcome):
|
||||||
|
|
||||||
|
|
||||||
|
c.execute("INSERT INTO games (name, switch, outcome) values (?,?,?) ", (name, switch, outcome))
|
||||||
|
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def seedDB():
|
||||||
|
c.execute("""
|
||||||
|
CREATE TABLE games (
|
||||||
|
id INTEGER DEFAULT NULL PRIMARY KEY AUTOINCREMENT,
|
||||||
|
name TEXT DEFAULT NULL,
|
||||||
|
switch TEXT DEFAULT NULL,
|
||||||
|
outcome TEXT DEFAULT NULL
|
||||||
|
);
|
||||||
|
""")
|
||||||
|
|
||||||
|
conn.commit()
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
seed = input('Seed the database[y/n]: ').lower()
|
||||||
|
if seed in ['y', 'yes']:
|
||||||
|
print('seeding')
|
||||||
|
seedDB()
|
||||||
|
|
||||||
|
exit()
|
6
exercises/1-monty-hall-problem/schema.sql
Normal file
6
exercises/1-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
|
||||||
|
);
|
@ -1,3 +1,25 @@
|
|||||||
class View:
|
class View:
|
||||||
def __init__(self):
|
def get_name(self):
|
||||||
pass
|
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()
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user