#The game is played like this:
#1. The game show set has three doors. A prize such as a car or vacation is behind one door, and the other two doors
#hide a valueless prize called a Zonk; in most discussions of the problem, the Zonk is a goat.
#2. The contestant chooses one door. We’ll assume the contestant has no inside knowledge of which door holds the
#prize, so the contestant will just make a random choice.
#3. The smiling host Monty Hall opens one of the other doors, always choosing one that shows a goat, and always
#offers the contestant a chance to switch their choice to the remaining unopened door.
#4. The contestant either chooses to switch doors, or opts to stick with the first choice.
#5. Monty calls for the remaining two doors to open, and the contestant wins whatever is behind their chosen door.

""" simulate the monty hall problem

"""

import argparse,random

def simulate(num_doors,switch,verbose):
    """(int, bool): bool

        Carry out the game for one contestant. If 'switch' is True,
        the contestant will switch their chosen door when offered the chance.
        Returns a Boolean value telling whether the simulated contestant won.
       """

    # Doors are numbered from 0 upto doors-1 (inclusive)

    # Randomly choose number hiding the prize
    winning_door = random.randint(0, num_doors - 1)

    if verbose:
        print('Prize is behind door {}'.format(winning_door+1))

# The contestant chooses random door

    choice=random.randint(0,num_doors-1)

    if verbose:
        print('Contestant has chosen door {}'.format(choice+1))



  #Host opens all doors but two

    closed_doors=list(range(num_doors))

    while len(closed_doors)>2:
        door_to_remove=random.choice(closed_doors)

        if door_to_remove== winning_door or door_to_remove== choice:
            continue


        closed_doors.remove(door_to_remove)

        if verbose:
            print('Host opens door {}'.format(door_to_remove+1))

    # There are always two doors opening

    assert len(closed_doors)==2

    # Does the contestant want to switch the door

    if switch:
        if verbose:
            print('Contestant switches door from {0} '.format(choice+1),end='' )

        # There are two closed doors left . The contestant will never choose the same door,
        # so, will remove that door as a choice.

        available_doors=list(closed_doors) #make copy of the list
        available_doors.remove(choice)


        #Change choice to the only door available
        choice=available_doors.pop()

        if verbose:
            print('to {}'.format(choice+1))

    # Did the contestant win

    won=(choice==winning_door)

    if verbose:
        if won:
            print('contestant won !!!',end='\n\n')
        else:
            print('contestant lost !!!', end='\n\n')

    return won

def main():
    # Get command line arguments
    parser=argparse.ArgumentParser(description='Simulate the Monty Hall Problem')
    parser.add_argument('--doors',default=3,type=int,metavar='int',help='number of doors offered to the contestant')
    parser.add_argument('--trials', default=10, type=int, metavar='int', help='number of trials to perform')
    parser.add_argument('--verbose', default=True, action='store_true', help='display the results of each trial')
    args=parser.parse_args()

    print('Simulating {} trials...'.format(args.trials))

    # Carry out the trials
    winning_non_switchers=0
    winning_switchers=0

    for i in range(args.trials):

        # First ,do a trial where the contestant never switches
        won=simulate(args.doors,switch=False,verbose=args.verbose)

        if won:
            winning_non_switchers+=1



        won=simulate(args.doors,switch=True,verbose=args.verbose)

        if won:
            winning_switchers += 1

    print('Switching won {0:5} times out of {1} ({2}% of the time)'.format(winning_switchers,
        args.trials,(winning_switchers / args.trials * 100 ) ))

    print('Not switching won {0:5} times out of {1} ({2}% of the time)'.format(winning_non_switchers, args.trials,
        (winning_non_switchers / args.trials * 100 ) ))

if __name__ == '__main__':

    main()





