function [x] = rps_main(steps)
% written by Dent A Earl @ UCLA
% 2007-06-01
%
x = round(.5+rand(13,25)*3); % we add an extra row and column at the end to allow pcolor to plot it correctly
flag=0;
x(1,:)=0;
x(12,:)=0;
x(13,:)=0;
x(:,1)=0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% x(:,7)=0;     % these two lines make up the rows in the lecture hall.
% x(:,18)=0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x(:,24)=0;
x(:,25)=0;
x(13,24)=1;     % this line and the following two lines are essential for proper pcolor plots. When pops crash without them, they may switch colors.
x(13,23)=2;
x(13,22)=3;
figure
h=gcf;
theworld=pcolor(x);
set(theworld,'EdgeColor','none')
axis off
colormap([1 1 1;0 0 1;0 1 0;1 0 0]);
figure
g=gcf;
plot_rock=[];
plot_paper=[];
plot_sciss=[];
[r,c]=find(x(1:12,:)>1)
total=numel(r);
[r,c]=find(x(1:12,:)==1);
plot_rock=[plot_rock numel(r)/total];
pr=plot(plot_rock,'');
hold on
[r,c]=find(x(1:12,:)==2);
plot_paper=[plot_paper numel(r)/total];
pp=plot(plot_paper,'g');
[r,c]=find(x(1:12,:)==3);
plot_sciss=[plot_sciss numel(r)/total];
ps=plot(plot_sciss,'r');
axis([0 steps+1 0 1]);
set(pr,'EraseMode','xor','LineWidth',3);
set(pp,'EraseMode','xor','LineWidth',3);
set(ps,'EraseMode','xor','LineWidth',3);
set(gca,'DrawMode','fast')


for repititions=1:steps
    drawnow;
    y=zeros(13,25);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%     y(2:11,2:6)=1;        % the next two lines are for a lecture hall
%     with asiles running down it.
%     y(2:11,8:17)=1;
%     y(2:11,19:23)=1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    y(2:11,2:23)=1;   % one sinlge large lecture hall
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    tracker=0;
    while find(y(:,1:23)>0)
        tracker=tracker+1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%       SEMILOCAL GAME
%         pause
        [r,c]=find(y(:,1:23)>0);
        selected1=round(.5+rand*length(r));
        y(r(selected1),c(selected1))=0;
        location=[r(selected1),c(selected1)];
%         pause(0.01)
        [r,c]=find(y==1);
        hotspots=[];
        hotspots_r=find(r==location(1)-1 | r==location(1) | r==location(1)+1);
        hotspots_c=find(c==location(2)-1 | c==location(2) | c==location(2)+1);
        hotspots=intersect(hotspots_r, hotspots_c);
%         disp(num2str(hotspots));

        if isempty(hotspots)==0
            selected2=(hotspots(round(.5+rand*length(hotspots))));
%             disp(['(',num2str(location(1)),',',num2str(location(2)),')']);
%             disp(['   goes to... --->']);
%             disp(['(',num2str(r(selected2)),',',num2str(c(selected2)),')']);
            y(r(selected2),c(selected2))=0;
            [x(location(1),location(2)),x(r(selected2),c(selected2))]=rps(x(location(1),location(2)),x(r(selected2),c(selected2)));
        else
            y(location(1),location(2))=1;
        end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%       FULLY RANDOM PLAYING!
%         [r,c]=find(y(:,1:23)>0);
%         selected1=round(.5+rand*length(r));
%         y(r(selected1),c(selected1))=0;
%         location=[r(selected1),c(selected1)];
% 
%         [r,c]=find(y(:,1:23)>0);
%         selected2=round(.5+rand*length(r));
%         y(r(selected2),c(selected2))=0;
%         [x(location(1),location(2)),x(r(selected2),c(selected2))]=rps(x(location(1),location(2)),x(r(selected2),c(selected2)));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        if tracker > 210
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%             RAISE YOUR HAND
            [r,c]=find(y(:,1:23)>0);
            selected1=round(.5+rand*length(r));
            y(r(selected1),c(selected1))=0;
            location=[r(selected1),c(selected1)];
            [r,c]=find(y(:,1:23)>0);
            selected2=round(.5+rand*length(r));
            y(r(selected2),c(selected2))=0;
            [x(location(1),location(2)),x(r(selected2),c(selected2))]=rps(x(location(1),location(2)),x(r(selected2),c(selected2)));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %        LOCALS ONLY, BRO!
%             [r,c]=find(y(:,1:23)>0);
%             for i=1:numel(r)
%                 y(r(i),c(i))=0;
%             end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        end
    end
%     pause(.1)
    figure(h)
    set(theworld,'CData',x);
    name=[num2str(repititions),['a'],'.jpg'];
    saveas(gcf,name,'jpg');
    figure(g)
    [r,c]=find(x(1:12,:)==1);
    plot_rock=[plot_rock numel(r)/total];
    if (numel(r)/total)==1
        flag=1;
    end
    [r,c]=find(x(1:12,:)==2);
    plot_paper=[plot_paper numel(r)/total];
    if (numel(r)/total)==1
        flag=1;
    end
    [r,c]=find(x(1:12,:)==3);
    if (numel(r)/total)==1
        flag=1;
    end
    plot_sciss=[plot_sciss numel(r)/total];
    set(pr,'Ydata',plot_rock);
    set(pp,'Ydata',plot_paper);
    set(ps,'Ydata',plot_sciss);
    name=[num2str(repititions),['b'],'.jpg'];
    saveas(gcf,name,'jpg');
       
    
    img1name=[num2str(repititions),'a','.jpg'];
    img2name=[num2str(repititions),'b','.jpg'];
    img1 = imread(img1name,'jpg');
    img2 = imread(img2name,'jpg');
    imgcat=cat(2,img1,img2);
    imgcatname=['imgcat',num2str(repititions),'.jpg'];
    imwrite(imgcat,imgcatname,'jpg');
%     M(i)=im2frame(imgcat);
    delete(img1name);
    delete(img2name);
    
    if flag==1
        break
    end
end


function [a,b] = rps(a,b)
%
% rock paper scissors player!
% 1 = rock
% 2 = paper
% 3 = scissors
%
% 1 beats 2, 2 beats 3, 3 beats 1

if a == 1
    if b == 2
        b = 1;
    elseif b == 3
        a = 3;
    end
elseif a == 2
    if b == 3
        b = 2;
    elseif b == 1;
        a = 1;
    end
elseif a == 3
    if b == 1
        b = 3;
    elseif b == 2
        a = 2;
    end
end
