diff --git a/exercises/1-monty-hall-problem/README.md b/exercises/1-monty-hall-problem/README.md index 292b08a..e4105c8 100644 --- a/exercises/1-monty-hall-problem/README.md +++ b/exercises/1-monty-hall-problem/README.md @@ -1,3 +1,5 @@ +50/50 for ever door all the time! + The Monty Hall Problem ====================== diff --git a/exercises/1-monty-hall-problem/__pycache__/models.cpython-34.pyc b/exercises/1-monty-hall-problem/__pycache__/models.cpython-34.pyc new file mode 100644 index 0000000..4c59939 Binary files /dev/null and b/exercises/1-monty-hall-problem/__pycache__/models.cpython-34.pyc differ diff --git a/exercises/1-monty-hall-problem/__pycache__/views.cpython-34.pyc b/exercises/1-monty-hall-problem/__pycache__/views.cpython-34.pyc new file mode 100644 index 0000000..e1233a1 Binary files /dev/null and b/exercises/1-monty-hall-problem/__pycache__/views.cpython-34.pyc differ diff --git a/exercises/1-monty-hall-problem/controller.py b/exercises/1-monty-hall-problem/controller.py index 2de7b42..345a34a 100644 --- a/exercises/1-monty-hall-problem/controller.py +++ b/exercises/1-monty-hall-problem/controller.py @@ -1,6 +1,52 @@ -#from models import +from models import Doors from views import View class Controller: - def __init__(self): - pass \ No newline at end of file + def __init__( self ): + self.view = View() + self.name = self.view.get_name() + self.start() + + def start( self ): + + selected = int( self.view.display_doors() ) + self.doors = Doors( 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 = self.view.host_twist( message ) + + if selection: + self.doors.selected = other_selection + self.doors.switch = True + return self.out_come() + + def out_come( self ): + if self.doors.out_come(): + do = self.view.winner() + else: + do = self.view.loser() + + if do == 'n': + exit() + + self.start() + return True + +Controller() diff --git a/exercises/1-monty-hall-problem/default.db b/exercises/1-monty-hall-problem/default.db new file mode 100644 index 0000000..cf5b656 Binary files /dev/null and b/exercises/1-monty-hall-problem/default.db differ diff --git a/exercises/1-monty-hall-problem/models.py b/exercises/1-monty-hall-problem/models.py index e69de29..e23763b 100644 --- a/exercises/1-monty-hall-problem/models.py +++ b/exercises/1-monty-hall-problem/models.py @@ -0,0 +1,37 @@ +import random + +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 ): + import sqlite3 + + conn = sqlite3.connect('default.db') + c = conn.cursor() + + c.execute( "INSERT INTO games (name, switch, outcome) values (?,?,?) ", ( name, switch, outcome ) ) + + conn.commit() + + conn.close() + return True \ No newline at end of file diff --git a/exercises/1-monty-hall-problem/schema.sql b/exercises/1-monty-hall-problem/schema.sql new file mode 100644 index 0000000..d9a373b --- /dev/null +++ b/exercises/1-monty-hall-problem/schema.sql @@ -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 +); diff --git a/exercises/1-monty-hall-problem/views.py b/exercises/1-monty-hall-problem/views.py index ee133c7..301aed5 100644 --- a/exercises/1-monty-hall-problem/views.py +++ b/exercises/1-monty-hall-problem/views.py @@ -1,3 +1,24 @@ class View: - def __init__(self): - pass \ No newline at end of file + def __init__( self ): + print( "Welcome to the Monty Hall Problem!") + + def get_name( self ): + return input( "Please enter your name: " ) + + def display_doors( self ): + 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( "[no]: ") + + def winner( self ): + print( "You WIN!!!" ) + return input("Play again?[y]") + + def loser( self ): + print( "You Lose!!!" ) + return input("Play again?[y]") \ No newline at end of file