% A simulation of the Monty Hall Paradox
% Tolga Tasdizen
% Department of Electrical and Computer Engineering
% University of Utah

% Set interactive = 1 for interactive play
% Set interactive = 0 for automatic play

interactive = 1;
Million = imread('Million.png');
Ten = imread('Ten.png');
Door = imread('Door.png');
Player = imread('PlayerChoice.png');
M = zeros(2);
cnt = 0;
while cnt<500
    a = rand();
    if a<1/3
        c=1;
    elseif a<2/3
        c=2;
    else
        c=3;
    end;
    
    %         UNCOMMENT THE FOLLOWING FOR AN OPEN GAME
    %         figure(1);clf;
    %         subplot(3,1,1);image(Ten);axis off;
    %         subplot(3,1,2);image(Ten);axis off;
    %         subplot(3,1,3);image(Ten);axis off;
    %         subplot(3,1,c);image(Million);axis off;
    
    figure(2);clf;
    subplot(3,1,1);image(Door);axis off;
    subplot(3,1,2);image(Door);axis off;
    subplot(3,1,3);image(Door);axis off;
    subplot(3,1,1);title('CHOOSE A DOOR','FontSize',24);
    
    if interactive==1
        p=0;
        while p<1 | p>3
            p = input('Player choice?');
        end;
    else
        a = rand();
        if a<1/3
            p=1;
        elseif a<2/3
            p=2;
        else
            p=3;
        end;
    end;
    figure(2);subplot(3,1,p);image(Player);axis off;
    subplot(3,1,1);title('WAIT...','FontSize',24);
    if interactive==1
        pause(1);
    end;
    if p == c
        x = rand()<0.5;
        if c == 1 && x == 0
            h = 2;
        end;
        if c == 1 && x == 1
            h = 3;
        end;
        %
        if c == 2 && x == 0
            h = 1;
        end;
        if c == 2 && x == 1
            h = 3;
        end;
        %
        if c == 3 && x == 0
            h = 1;
        end;
        if c == 3 && x == 1
            h = 2;
        end;
    else
        if p == 1
            if c == 2
                h = 3;
            else
                h = 2;
            end;
        end;
        %
        if p == 2
            if c == 1
                h = 3;
            else
                h = 1;
            end;
        end;
        %
        if p == 3
            if c == 1
                h = 2;
            else
                h = 1;
            end;
        end;
    end;
    figure(2);subplot(3,1,h);image(Ten);axis off;
    subplot(3,1,1);title('DO YOU WANT TO SWITCH ?','FontSize',24);
    s=-1;
    if interactive==1
        while s~=0 & s~=1
            s = input('Switch?');
        end;
    else
        s = rand()<0.5;
    end;
    if s==1
        if h == 1
            if p == 2
                pn = 3;
            else
                pn = 2;
            end;
        elseif h == 2
            if p == 1
                pn = 3;
            else
                pn = 1;
            end;
        else
            if p == 1
                pn = 2;
            else
                pn = 1;
            end;
        end;
        subplot(3,1,p);image(Door);axis off;
        subplot(3,1,pn);image(Player);axis off;
        p = pn;
    end;
    subplot(3,1,1);title('.....','FontSize',24);
    if interactive==1
        pause(1);
    end;
    if p == c
        subplot(3,1,p);image(Million);axis off;
        subplot(3,1,1);title('CONGRATULATIONS!!!','FontSize',24);
        M(s+1,1) = M(s+1,1) + 1;
    else
        subplot(3,1,p);image(Ten);axis off;
        subplot(3,1,1);title('SORRY','FontSize',24);
        M(s+1,2) = M(s+1,2) + 1;
    end;
    if interactive==1
        M
        figure(3);clf
        fh=subplot(2,1,1);bar(M(2,:));title('SWITCHED','FontSize',24);
        set(fh,'XTick',[]);
        xlabel('        WON                                            LOST','FontSize',16)
        fh=subplot(2,1,2);bar(M(1,:));title('STAYED','FontSize',24);
        set(fh,'XTick',[])
        xlabel('        WON                                            LOST','FontSize',16)
        pause(1);
    end;
    cnt = cnt + 1;
end;
if interactive==0
    figure(3);clf
    fh=subplot(2,1,1);bar(M(2,:));title('SWITCHED','FontSize',24);
    set(fh,'XTick',[]);
    xlabel('        WON                                            LOST','FontSize',16)
    fh=subplot(2,1,2);bar(M(1,:));title('STAYED','FontSize',24);
    set(fh,'XTick',[])
    xlabel('        WON                                            LOST','FontSize',16)
end;