diff --git a/exercises/1-monty-hall-problem/__pycache__/models.cpython-34.pyc b/exercises/1-monty-hall-problem/__pycache__/models.cpython-34.pyc index 4c59939..137fdb7 100644 Binary files a/exercises/1-monty-hall-problem/__pycache__/models.cpython-34.pyc 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 index e1233a1..8cb7e61 100644 Binary files a/exercises/1-monty-hall-problem/__pycache__/views.cpython-34.pyc 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 345a34a..165a37e 100644 --- a/exercises/1-monty-hall-problem/controller.py +++ b/exercises/1-monty-hall-problem/controller.py @@ -2,51 +2,56 @@ from models import Doors from views import View class Controller: - def __init__( self ): + def __init__(self): self.view = View() self.name = self.view.get_name() self.start() - def start( self ): + def start(self): - selected = int( self.view.display_doors() ) - self.doors = Doors( selected, self.name ) + 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 ): + def host_twist(self): message ="" - for i in range( 1, 4 ): + for i in range(1, 4): if i == self.doors.selected: - message += "[ X ] " + message += '[ X ] ' elif i == self.doors.open_door: - message += "[ Goat ] " + message += '[ Goat ] ' else: - message += "[ " + str(i) + " ] " + message += '[ ' + str(i) + ' ] ' other_selection = i + selection = False + while selection not in ['n', 'y', '']: + selection = self.view.host_twist(message).lower() - selection = self.view.host_twist( message ) - - if selection: + if selection == 'y': self.doors.selected = other_selection self.doors.switch = True return self.out_come() - def out_come( self ): + def out_come(self): + message = 'LOSE' if self.doors.out_come(): - do = self.view.winner() - else: - do = self.view.loser() + message = 'WIN' - if do == 'n': - exit() + replay = self.view.out_come(message) - self.start() - return True + if replay in ['', 'y']: + return self.start() + + return exit() -Controller() +if __name__ == '__main__': + Controller() diff --git a/exercises/1-monty-hall-problem/default.db b/exercises/1-monty-hall-problem/default.db index cf5b656..4158e39 100644 Binary files a/exercises/1-monty-hall-problem/default.db 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 e23763b..bdc1531 100644 --- a/exercises/1-monty-hall-problem/models.py +++ b/exercises/1-monty-hall-problem/models.py @@ -1,37 +1,59 @@ +import sqlite3 import random +conn = sqlite3.connect('default.db') +c = conn.cursor() + class Doors: - def __init__( self, selected, name ): + def __init__(self, selected, name): self.name = name self.switch = False self.selected = selected - self.car = random.randint( 1,3 ) + self.car = random.randint(1,3) self.open_door = self.host_twist() - def host_twist( self ): - open_door = random.randint( 1,3 ) + 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 ): + def out_come(self): if self.selected == self.car: - logDB( self.name, self.switch, True) + logDB(self.name, self.switch, True) return True else: - logDB( self.name, self.switch, False) + logDB(self.name, self.switch, False) return False -def logDB( name, switch, outcome ): - import sqlite3 +def logDB(name, switch, outcome): - conn = sqlite3.connect('default.db') - c = conn.cursor() - c.execute( "INSERT INTO games (name, switch, outcome) values (?,?,?) ", ( name, switch, outcome ) ) + c.execute("INSERT INTO games (name, switch, outcome) values (?,?,?) ", (name, switch, outcome)) conn.commit() conn.close() - return True \ No newline at end of file + 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() \ No newline at end of file diff --git a/exercises/1-monty-hall-problem/views.py b/exercises/1-monty-hall-problem/views.py index 301aed5..03134c6 100644 --- a/exercises/1-monty-hall-problem/views.py +++ b/exercises/1-monty-hall-problem/views.py @@ -1,24 +1,25 @@ class View: - def __init__( self ): - print( "Welcome to the Monty Hall Problem!") + def get_name(self): + print('\033c') + print('Welcome to the Monty Hall Problem!') + return input('Please enter your name: ') - def get_name( self ): - return input( "Please enter your name: " ) + def display_doors(self): + print('\033c') + print('Doors: [ 1 ] [ 2 ] [ 3 ]') + return input('Select door: ') - 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('[n]: ') - 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 out_come(self, message): + print('You {}!!!'.format(message)) + return input('Play again?[y]') - 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 +if __name__ == '__main__': + print('Please opne controller.py') + exit() + \ No newline at end of file