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
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()
Controller()
return exit()
if __name__ == '__main__':
Controller()

View File

@ -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
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:
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]")
if __name__ == '__main__':
print('Please opne controller.py')
exit()
def loser( self ):
print( "You Lose!!!" )
return input("Play again?[y]")