This commit is contained in:
William Mantly 2016-02-23 17:46:32 -05:00
parent 1ee37b4023
commit 26469c9ae3
6 changed files with 81 additions and 53 deletions

View File

@ -2,51 +2,56 @@ from models import Doors
from views import View from views import View
class Controller: class Controller:
def __init__( self ): def __init__(self):
self.view = View() self.view = View()
self.name = self.view.get_name() self.name = self.view.get_name()
self.start() self.start()
def start( self ): def start(self):
selected = int( self.view.display_doors() ) selected = False
self.doors = Doors( selected, self.name ) while selected not in ['1', '2', '3']:
selected = self.view.display_doors()
self.doors = Doors(int(selected), self.name)
self.host_twist() self.host_twist()
return True return True
def host_twist( self ): def host_twist(self):
message ="" message =""
for i in range( 1, 4 ): for i in range(1, 4):
if i == self.doors.selected: if i == self.doors.selected:
message += "[ X ] " message += '[ X ] '
elif i == self.doors.open_door: elif i == self.doors.open_door:
message += "[ Goat ] " message += '[ Goat ] '
else: else:
message += "[ " + str(i) + " ] " message += '[ ' + str(i) + ' ] '
other_selection = 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 == 'y':
if selection:
self.doors.selected = other_selection self.doors.selected = other_selection
self.doors.switch = True self.doors.switch = True
return self.out_come() return self.out_come()
def out_come( self ): def out_come(self):
message = 'LOSE'
if self.doors.out_come(): if self.doors.out_come():
do = self.view.winner() message = 'WIN'
else:
do = self.view.loser()
if do == 'n': replay = self.view.out_come(message)
exit()
self.start() if replay in ['', 'y']:
return True return self.start()
Controller() return exit()
if __name__ == '__main__':
Controller()

View File

@ -1,37 +1,59 @@
import sqlite3
import random import random
conn = sqlite3.connect('default.db')
c = conn.cursor()
class Doors: class Doors:
def __init__( self, selected, name ): def __init__(self, selected, name):
self.name = name self.name = name
self.switch = False self.switch = False
self.selected = selected self.selected = selected
self.car = random.randint( 1,3 ) self.car = random.randint(1,3)
self.open_door = self.host_twist() self.open_door = self.host_twist()
def host_twist( self ): def host_twist(self):
open_door = random.randint( 1,3 ) open_door = random.randint(1,3)
if open_door == self.car or open_door == self.selected: if open_door == self.car or open_door == self.selected:
return self.host_twist() return self.host_twist()
return open_door return open_door
def out_come( self ): def out_come(self):
if self.selected == self.car: if self.selected == self.car:
logDB( self.name, self.switch, True) logDB(self.name, self.switch, True)
return True return True
else: else:
logDB( self.name, self.switch, False) logDB(self.name, self.switch, False)
return False return False
def logDB( name, switch, outcome ): 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 ) ) c.execute("INSERT INTO games (name, switch, outcome) values (?,?,?) ", (name, switch, outcome))
conn.commit() conn.commit()
conn.close() conn.close()
return True 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()

View File

@ -1,24 +1,25 @@
class View: class View:
def __init__( self ): def get_name(self):
print( "Welcome to the Monty Hall Problem!") print('\033c')
print('Welcome to the Monty Hall Problem!')
return input('Please enter your name: ')
def get_name( self ): def display_doors(self):
return input( "Please enter your name: " ) print('\033c')
print('Doors: [ 1 ] [ 2 ] [ 3 ]')
return input('Select door: ')
def display_doors( self ): def host_twist(self, message):
print( 'Doors: [ 1 ] [ 2 ] [ 3 ]' ) print('The host will revel where one goat is...')
return input( 'Select door: ' ) print(message)
print('Would you like to change your selection? [n/y]')
return input('[n]: ')
def host_twist( self, message ): def out_come(self, message):
print( "The host will revel where one goat is..." ) print('You {}!!!'.format(message))
print( message ) return input('Play again?[y]')
print( "Would you like to change your selection? [n/y]" )
return input( "[no]: ")
def winner( self ): if __name__ == '__main__':
print( "You WIN!!!" ) print('Please opne controller.py')
return input("Play again?[y]") exit()
def loser( self ):
print( "You Lose!!!" )
return input("Play again?[y]")