%THREE: MATLAB Script file for playing the three-door game
choice1=input('Door 1, 2, or 3?');
rvar=rand;
if choice1==1       %user chooses 1
    if 3*rvar <= 1  %Prize behind door 1
        rvar2=rand;
        if rvar2 <= .5 
            doornot=2;
        else
            doornot=3;
        end
        disp('Prize is not behind door')
        disp(doornot)
        choice2=input('Switch? (y/n)','s');
        if choice2=='y'
            disp('Tough break.')
        else
            disp('We have a winner.')
        end
    elseif 3*rvar<=2    %Prize is behind Door 2
        disp('Prize is not behind Door')
        disp(3)
        choice2=input('Switch? (y/n)','s');
        if choice2=='y'
            disp('We have a winner.')
        else
            disp('Tough break.')
        end
    else            %Prize is behind Door 3
        disp('Prize is not behind Door')
        disp(2)
        choice2=input('Switch? (y/n)','s');
        if choice2=='y'
            disp('We have a winner.')
        else
            disp('Tough break.')
        end
    end
elseif choice1==2   %User chooses 2
    if 3*rvar <= 1  %Prize behind Door 1
        disp('Prize is not behind Door')
        disp(3)
        choice2=input('Switch? (y/n)','s');
        if choice2=='y'
            disp('We have a winner.')
        else
            disp('Tough break.')
        end   
    elseif 3*rvar <= 2  %Prize behind Door 2
        rvar2=rand;
        if rvar2 <= .5 
            doornot=1;
        else
            doornot=3;
        end
        disp('Prize is not behind door')
        disp(doornot)
        choice2=input('Switch? (y/n)','s');
        if choice2=='y'
            disp('Tough break.')
        else
            disp('We have a winner.')
        end    
    else    %Prize behind Door 3
        disp('Prize is not behind Door')
        disp(1)
        choice2=input('Switch? (y/n)','s');
        if choice2=='y'
            disp('We have a winner.')
        else
            disp('Tough break.')
        end
    end
elseif choice1==3    %Prize behind Door 3
    if 3*rvar<=1    %Prize behind Door 1
        disp('Prize is not behind Door')
        disp(2)
        choice2=input('Switch? (y/n)','s');
        if choice2=='y'
            disp('We have a winner.')
        else
            disp('Tough break.')
        end   
    elseif 3*rvar<=2    %Prize behind Door 2
        disp('Prize is not behind Door')
        disp(1)
        choice2=input('Switch? (y/n)','s');
        if choice2=='y'
            disp('We have a winner.')
        else
            disp('Tough break.')
        end   
    else 
        rvar2=rand;
        if rvar2 <= .5 
            doornot=2;
        else
            doornot=1;
        end
        disp('Prize is not behind door')
        disp(doornot)
        choice2=input('Switch? (y/n)','s');
        if choice2=='y'
            disp('Tough break.')
        else
            disp('We have a winner.')
        end       
    end
end

    