Code covered by the BSD License  

Highlights from
Logic Game

image thumbnail

Logic Game

by

 

05 Dec 2010 (Updated )

A very interesting game to play ....Requires a lot of logic.....Play with your friend or computer

game
function game
%A very interesting game based on logic
%Created by Mohit Gupta on 4th december
%Modified on 12.12.10,15.12.10,16.12.10,17.12.10

close all;
hfig=figure('MenuBar','none','Name','Logic Game','Numbertitle','off');
hwindow=axes('position',[.05 .05 .650 .650],'color',[0 1 0],'box','on',...
    'YLim',[0 5],'ButtonDownFcn',@click,'XTick',0,'YTick',0,'ZTick',0);

%draw lines
x=linspace(0,5,100);y=ones(1,100);
for ln=1:6
    hlnver(ln)=line((ln-1)*y,x);
    hlnhor(ln)=line(x,(ln-1)*y);
end
set(hlnver,'parent',hwindow);
set(hlnhor,'parent',hwindow);
%lines drawn

%uicontrols
hstatus=uicontrol('style','text',...
    'position',[20 300 100 20],'string','',...
    'BackGroundColor',get(hfig,'Color'),'FontSize',9) ;
hcaution=uicontrol('style','text',...
    'position',[140 300 100 40],'BackGroundColor',get(hfig,'Color'));
uicontrol('Style','text','FontName','Comic Sans MS',...
    'FontSize',10,'String','by Mohit Gupta','FontWeight','Bold',...
    'Position',[450 400 100 20],'BackGroundColor',get(hfig,'Color'));
hround=uicontrol('style','text','FontSize',12,...
    'position',[140 370 100 40],'BackGroundColor',get(hfig,'Color'));
hstart=uicontrol('style','pushbutton','position',[20 370 100 40],...
    'FontSize',12,'string','start','CallBack',@start);
hscore=uicontrol('style','text','FontSize',22,...
    'position',[440 30 90 100],'BackGroundColor',get(hfig,'Color'));
uicontrol('style','pushbutton','position',[20 330 100 30],...
    'FontSize',12,'string','close','Callback','close');
hplayers=uicontrol('Style','Text','Position',[400 280 150 20],...
    'BackGroundColor',get(hfig,'Color'),'FontSize',12,'String','Currently Playing:');
hplayer1=uicontrol('Position',[400 220 150 50],'Style','PushButton',...
    'String','Human','BackGroundColor','b','FontSize',12,'CallBack',@funcplayer1);
hplayer2=uicontrol('Position',[400 140 150 50],'Style','PushButton',...
    'String','Human','BackGroundColor','r','FontSize',12,'CallBack',@funcplayer2);
hvs=uicontrol('Style','Text','Position',[450 200 50 15],...
    'BackGroundColor',get(hfig,'Color'),'FontSize',10,'String','VS.');
hfast=uicontrol('Style','Pushbutton','Position',[460 370 80 25],...
    'Fontsize',12,'String','Fast','Callback',@fast);
hfast=uicontrol('Style','Pushbutton','Position',[460 340 80 25],...
    'Fontsize',12,'String','Slow','Callback',@slow);
hstop=uicontrol('Style','PushButton','Position',[460 310 80 25],...
    'Fontsize',12,'String','Pause','Callback',@func_stop);

%initialize Active player tokens and other variables(global to all
%functions in this file)
turn=zeros(5,5,2);
chance=1;
htok=[];round=0;testwin=0;
speed=1;                %speed variable
procesing=0;            %check if anime
player1=0;player2=0;    %humans
stop=0;                 %not paused

%change player1
    function funcplayer1(~,~)
        if player1==0
            player1=1;
            set(hplayer1,'String','Computer(easy)');
        elseif player1==1
            player1=2;
            set(hplayer1,'String','Computer(normal)');
        elseif player1==2
            player1=3;
            set(hplayer1,'String','Computer(hard)');
        else
            player1=0;
            set(hplayer1,'String','Human');
        end
        
        if (player1~=0)&&(chance==1)&&(round>0)
            computer;
        end
    end


%change player2
    function funcplayer2(~,~)
        if player2==0
            player2=1;
            set(hplayer2,'String','Computer(easy)');
        elseif player2==1
            player2=2;
            set(hplayer2,'String','Computer(normal)');
        elseif player2==2
            player2=3;
            set(hplayer2,'String','Computer(hard)');
        else
            player2=0;
            set(hplayer2,'String','Human');
        end
        if (player2~=0)&&(chance==2)&&(round>0)
            computer;
        end
    end

%speed functions
    function fast(~,~)
        speed=1.5*speed;
    end
    function slow(~,~)
        speed=0.5*speed;
    end
    function func_stop(~,~)
        if stop==0
            stop=1;
            set(hstop,'String','Resume');
            while stop==1
                pause(0.1);
            end
        else
            set(hstop,'String','Pause');
            stop=0;
        end
    end

%start button
    function start(~,~)
        if round~=0                         %click on new game
            close;
            game;
            return
        end
        
        set(hstatus,'string','First player','BackGroundColor',[1 1 1]);
        set(hround,'string','Round:1');
        set(hstart,'string','New Game');
        set(hcaution,'string',' ','BackGroundColor',get(hfig,'Color'));
        set(hscore,'string','Score0:0','BackGroundColor',[1 1 1]);
        round=1;
        
        if player1~=0
            computer;
        end
        
    end

%click on screen
    function click(~,~)
        if procesing==0
            pt=get(hwindow,'currentpoint');
            pt=ceil(pt(1,1:2));                %top-right point of cell
            set(hcaution,'string',' ','BackGroundColor',get(hfig,'Color'));
            
            %check if valid click
            if (chance==1)&&(turn(pt(1),pt(2),2)==0)
                anime(pt);
                %next turn
                chance=2;
                set(hstatus,'string','Second player');
                
                if player2~=0
                    computer;
                end
                
            elseif (chance==2)&&(turn(pt(1),pt(2),1)==0)
                anime(pt);
                %next turn
                chance=1;
                set(hstatus,'string','First player');
                round=round+1;
                if player1~=0
                    computer;
                end
            else                                 %invalid choice
                set(hcaution,'string',...
                    'cannot enter in cell occupied by opponent'...
                    ,'BackGroundColor','r');
            end
        end
    end

%anime
    function anime(pt)
        %part of project: game
        %draw token
        %animate and cause chain reactions
        procesing=1;
        ptold=pt  ;                     %save point vectors
        for j=1:size(ptold,1)
            pt=ptold(j,:);
            
            %score and win condition
            ballsum=[sum(sum(turn(:,:,1))) sum(sum(turn(:,:,2)))];
            set(hscore,'string',strcat('Score',num2str(ballsum(1))...%display score
                ,':',num2str(ballsum(2))));
            if chance==1
                temp=turn(pt(1),pt(2),2);
                turn(pt(1),pt(2),2)=0;
                if (ballsum(2)==0)&&(testwin==0)&&(round>1)           %winning condition
                    hwin=uicontrol('style','text','position',[250 350 200 70],...
                        'fontsize',20,'Fontweight','Bold','string','First player wins!');
                    for z=0:9
                        set(hfig,'Color','b');
                        pause(0.1);
                        set(hfig,'Color',[0 0.1 0.5])
                        pause(0.1);
                    end
                    procesing=2;
                    testwin=1;
                end
            else
                temp=turn(pt(1),pt(2),1);
                turn(pt(1),pt(2),1)=0;
                if (ballsum(1)==0)&&(testwin==0)&&(round>1)           %winning condition
                    hwin=uicontrol('style','text','position',[250 350 200 70],...
                        'fontsize',20,'Fontweight','Bold','string','Second player wins!');
                    for z=0:9
                        set(hfig,'Color','r');
                        pause(0.1);
                        set(hfig,'Color',[0.5 0.1 0])
                        pause(0.1);
                    end
                    procesing=2;
                    testwin=1;
                end
            end
            num=temp+turn(pt(1),pt(2),chance)+1;      %number of tokens in cell
            %generate xdata for token
            if num/2==fix(num/2)                %even check
                xdata=pt(1)-0.25;
            else
                xdata=pt(1)-0.75;
            end
            %generate ydata for token
            if num<3
                ydata=pt(2)-0.75;
            else
                ydata=pt(2)-0.25;
            end
            %draw token and get handle
            if num<5
                htok(num,pt(1),pt(2))=line(xdata,ydata,'marker','o',...
                    'markersize',15);
                turn(pt(1),pt(2),chance)=num;     %update no. of tokens
                %marker color
                if chance==1
                    set(htok(1:num,pt(1),pt(2)),'markerfacecolor','b');
                else
                    set(htok(1:num,pt(1),pt(2)),'markerfacecolor','r');
                end
            end
        end
        
        %anime
        ptnew=zeros(200,2);       %initialize to increase speed
        dim_pt=1;                %dimension of ptnew
        an=0;                    %test if animation has been done
        ptold=unique(ptold,'rows');
        for k=1:size(ptold,1)
            pt=ptold(k,:);
            
            if ((pt(1)==1)&&(pt(2)==1)&&(turn(1,1,chance)==2))||...
                    ((pt(1)==1)&&(turn(1,pt(2),chance)==3))||...
                    ((pt(1)==1)&&(pt(2)==5)&&(turn(1,5,chance)==2))||...
                    ((pt(2)==1)&&(turn(pt(1),1,chance)==3))||...
                    ((pt(2)==5)&&(turn(pt(1),5,chance)==3))||...
                    ((pt(1)==5)&&(turn(5,pt(2),chance)==3))||...
                    ((pt(1)==5)&&(pt(2)==1)&&(turn(5,1,chance)==2))||...
                    ((pt(1)==5)&&(pt(2)==5)&&(turn(5,5,chance)==2))||...
                    turn(pt(1),pt(2),chance)==4
                
                if an==0
                    i=0;
                    while i<=1           %calculate how fast animation id done
                        for j=1:size(ptold,1)
                            pt=ptold(j,:);
                            
                            %corner 1,1
                            if (pt(1)==1)&&(pt(2)==1)&&(turn(1,1,chance)>=2)
                                set(htok(2,1,1),'xdata',0.25+i);
                                set(htok(1,1,1),'ydata',0.25+i);
                                %corner 5,1
                            elseif (pt(1)==5)&&(pt(2)==1)&&(turn(5,1,chance)>=2)
                                set(htok(2,5,1),'ydata',0.25+i);
                                set(htok(1,5,1),'xdata',4.25-i);
                                %corner 5,5
                            elseif (pt(1)==5)&&(pt(2)==5)&&(turn(5,5,chance)>=2)
                                set(htok(2,5,5),'ydata',4.25-i);
                                set(htok(1,5,5),'xdata',4.25-i);
                                %corner 1,5
                            elseif (pt(1)==1)&&(pt(2)==5)&&(turn(1,5,chance)>=2)
                                
                                set(htok(2,1,5),'xdata',0.75+i);
                                set(htok(1,1,5),'ydata',4.25-i);
                                %side x=1
                            elseif (pt(1)==1)&&(turn(1,pt(2),chance)>=3)
                                
                                set(htok(2,1,pt(2)),'xdata',0.25+i);
                                set(htok(3,1,pt(2)),'ydata',pt(2)-0.25+i);
                                set(htok(1,1,pt(2)),'ydata',pt(2)-0.75-i);
                                
                                %side y=1
                            elseif (pt(2)==1)&&(turn(pt(1),1,chance)>=3)
                                set(htok(2,pt(1),1),'xdata',pt(1)-0.25+i);
                                set(htok(3,pt(1),1),'ydata',0.75+i);
                                set(htok(1,pt(1),1),'xdata',pt(1)-0.75-i);
                                %side y=5
                            elseif (pt(2)==5)&&(turn(pt(1),5,chance)>=3)
                                set(htok(2,pt(1),5),'xdata',pt(1)-0.25+i);
                                set(htok(3,pt(1),5),'xdata',pt(1)-0.75-i);
                                set(htok(1,pt(1),5),'ydata',4.25-i);
                                
                                %side x=5
                            elseif (pt(1)==5)&&(turn(5,pt(2),chance)>=3)
                                set(htok(2,5,pt(2)),'ydata',pt(2)-0.75-i);
                                set(htok(3,5,pt(2)),'ydata',pt(2)-0.25+i);
                                set(htok(1,5,pt(2)),'xdata',4.25-i);
                                
                                %middle block
                            elseif turn(pt(1),pt(2),chance)>=4
                                set(htok(1,pt(1),pt(2)),'xdata',pt(1)-0.75-i);
                                set(htok(2,pt(1),pt(2)),'ydata',pt(2)-0.75-i);
                                set(htok(3,pt(1),pt(2)),'ydata',pt(2)-0.25+i);
                                set(htok(4,pt(1),pt(2)),'xdata',pt(1)-0.25+i);
                                
                            else %unnecessary
                            end
                        end
                        pause(.001);
                        i=i+0.03*speed;
                    end
                    an=1;                           %animated;
                end
                pt=ptold(k,:);
                %corner 1,1
                if (pt(1)==1)&&(pt(2)==1)&&(turn(1,1,chance)>=2)
                    
                    delete(htok(1:turn(pt(1),pt(2),chance),1,1));
                    turn(1,1,chance)=0;
                    ptnew(dim_pt:dim_pt+1,:)=[[1,2];[2,1]];
                    dim_pt=dim_pt+2;
                    %corner 1,5
                elseif (pt(1)==1)&&(pt(2)==5)&&(turn(1,5,chance)>=2)
                    
                    delete(htok(1:turn(pt(1),pt(2),chance),1,5));
                    turn(1,5,chance)=0;
                    ptnew(dim_pt:dim_pt+1,:)=[[1,4];[2,5]];
                    dim_pt=dim_pt+2;
                    %corner 5,1
                elseif (pt(1)==5)&&(pt(2)==1)&&(turn(5,1,chance)>=2)
                    
                    delete(htok(1:turn(pt(1),pt(2),chance),5,1));
                    turn(5,1,chance)=0;
                    ptnew(dim_pt:dim_pt+1,:)=[[4 1];[5 2]];
                    dim_pt=dim_pt+2;
                    %corner 5,5
                elseif (pt(1)==5)&&(pt(2)==5)&&(turn(5,5,chance)>=2)
                    
                    delete(htok(1:turn(pt(1),pt(2),chance),5,5));
                    turn(5,5,chance)=0;
                    ptnew(dim_pt:dim_pt+1,:)=[[4,5];[5,4]];
                    dim_pt=dim_pt+2;
                    %side x=1
                elseif (pt(1)==1)&&(turn(1,pt(2),chance)>=3)
                    
                    delete(htok(1:turn(pt(1),pt(2),chance),1,pt(2)));
                    turn(1,pt(2),chance)=0;
                    ptnew(dim_pt:dim_pt+2,:)=[[1,pt(2)+1];[1,pt(2)-1];[2,pt(2)]];
                    dim_pt=dim_pt+3;
                    
                    %side y=1
                elseif (pt(2)==1)&&(turn(pt(1),1,chance)>=3)
                    
                    delete(htok(1:turn(pt(1),pt(2),chance),pt(1),1));
                    turn(pt(1),1,chance)=0;
                    ptnew(dim_pt:dim_pt+2,:)=[[pt(1)+1,1];[pt(1)-1,1];[pt(1),2]];
                    dim_pt=dim_pt+3;
                    %side y=5
                elseif (pt(2)==5)&&(turn(pt(1),5,chance)>=3)
                    
                    delete(htok(1:turn(pt(1),pt(2),chance),pt(1),5));
                    turn(pt(1),5,chance)=0;
                    ptnew(dim_pt:dim_pt+2,:)=[[pt(1)+1,5];[pt(1)-1,5];[pt(1),4]];
                    dim_pt=dim_pt+3;
                    %side x=5
                elseif (pt(1)==5)&&(turn(5,pt(2),chance)>=3)
                    
                    delete(htok(1:turn(pt(1),pt(2),chance),5,pt(2)));
                    turn(5,pt(2),chance)=0;
                    ptnew(dim_pt:dim_pt+2,:)=[[5,pt(2)+1];[5,pt(2)-1];[4,pt(2)]];
                    dim_pt=dim_pt+3;
                    %middle block
                elseif turn(pt(1),pt(2),chance)>=4
                    
                    delete(htok(1:turn(pt(1),pt(2),chance),pt(1),pt(2)));
                    turn(pt(1),pt(2),chance)=0;
                    ptnew(dim_pt:dim_pt+3,:)=[[pt(1)-1,pt(2) ];[pt(1),pt(2)-1 ];...
                        [pt(1),pt(2)+1 ];[pt(1)+1,pt(2) ]];
                    dim_pt=dim_pt+4;
                end
            end
        end
        
        ptnew(dim_pt:200,:)=[];              %diminish ptnew
        if dim_pt>1
            anime(ptnew);
        end
        %display round number
        set(hround,'string',strcat('Round : ',num2str(round)));
        procesing=0;
        
    end


%play with computer
    function computer
        if testwin==1
            procesing=2;
            return
        elseif procesing==0;
            procesing=1;
            lvl=eval(strcat('player',num2str(chance)));     %gets level of computer play
            if lvl==1
                pt=lvleasy;
            elseif lvl==2
                pt=lvlnormal;
            elseif lvl==3
                pt=lvlhard;
            end
            show(pt);
            anime(pt);
            if chance==1
                chance=2;
                set(hstatus,'string','Second player');
            else
                chance=1;
                set(hstatus,'string','First player');
                round=round+1;
            end
            
            if eval(strcat('player',num2str(chance)))~=0
                computer;
            else
                procesing=0;
                return
            end
        end
        
        %         functions in computer
        %         easy level
        function pt=lvleasy
            pt=[fix(5*rand(1)+1) fix(5*rand(1)+1)];
            if (chance==1)&&(turn(pt(1),pt(2),2)==0)
                return
            elseif (chance==2)&&(turn(pt(1),pt(2),1)==0)
                return
            else
                pt=lvleasy;
            end
        end
        
        %normal level
        function pt=lvlnormal
            %continuosly expand approach
            
            %check for ready to explode cell in middle block
            for i=2:4
                for j=2:4
                    if turn(i,j,chance)==3
                        pt=[i,j];
                        return
                    end
                end
            end
            %else check for ready to explode cell in horizontal sides
            for i=2:4
                for j=1:4:5
                    if turn(i,j,chance)==2
                        pt=[i,j];
                        return
                    end
                end
            end
            %else check for vertical sides
            for i=1:4:5
                for j=2:4
                    if turn(i,j,chance)==2
                        pt=[i,j];
                        return
                    end
                end
            end
            %check for an already occupied corner
            for i=1:4:5
                for j=1:4:5
                    if turn(i,j,chance)==1
                        pt=[i j];
                        return
                    end
                end
            end
            %else fill a corner if possible
            for i=1:4:5
                for j=1:4:5
                    if (chance==1)&&(turn(i,j,2)==0)
                        pt=[i,j];
                        return
                        
                    elseif (chance==2)&&(turn(i,j,1)==0)
                        pt=[i,j];
                        return
                    end
                end
            end
            %else fill a horizontal side
            for i=2:4
                for j=1:4:5
                    if (chance==1)&&(turn(i,j,2)==0)
                        pt=[i,j];
                        return
                    elseif (chance==2)&&(turn(i,j,1)==0)
                        pt=[i,j];
                        return
                    end
                end
            end
            %else fill a vertical side
            for i=1:4:5
                for j=2:4
                    if (chance==1)&&(turn(i,j,2)==0)
                        pt=[i,j];
                        return
                    elseif (chance==1)&&(turn(i,j,1)==0)
                        pt=[i,j];
                        return
                    end
                end
            end
            %else fill the middle block
            for i=2:4
                for j=2:4
                    if (chance==1)&&(turn(i,j,2)==0)
                        pt=[i,j];
                        return
                    elseif (chance==1)&&(turn(i,j,1)==0)
                        pt=[i,j];
                        return
                    end
                end
            end
            
        end
        
        %Hard level
        function point=lvlhard
            
            temp_turn=turn;                         %save turn variable as it gets modified in process
            sums=ones(5);
            for m=1:5
                for n=1:5
                    if (chance==1)&&(turn(m,n,2)~=0)
                        sums(m,n)=0;
                    elseif (chance==2)&&(turn(m,n,1)~=0)
                        sums(m,n)=0;
                    else
                        call2process=0;
                        sums(m,n)=process([m n]);       %sums will save ballsums for all points
                        turn=temp_turn;                 %return turn to original state
                    end
                end
            end
            [temp row]=max(sums);                   %row is row index vector for max ballsum in each column
            [maxval,col]=max(temp);                 %col is column index for max ballsum
            dim_i=1;
            index=zeros(25,2);                      %initialize index matrix for speed
            for m=1:5
                for n=1:5
                    if sums(m,n)==maxval
                        index(dim_i,:)=[m n];
                        dim_i=dim_i+1;
                    end
                end
            end
            index(dim_i:25,:)=[];                   %diminish index
            dim_i=dim_i-1;
            point=lvlnormal;
            for m=1:dim_i
                if point==index(m,:);
                    return                     %returns if suitable point is obtained
                end
            end                                %reached if lvlnormal couldn't return a value
            row=row(col);                      %now row is the row index of maximum ballsum
            point=[row col];
            return
            %returns ballsum for this point
            function ballsum=process(pt)
                call2process=call2process+1;
                ptold=pt  ;                     %save point vectors
                for j=1:size(ptold,1)
                    pt=ptold(j,:);
                    if chance==1
                        temp=turn(pt(1),pt(2),2);
                        turn(pt(1),pt(2),2)=0;
                    else
                        temp=turn(pt(1),pt(2),1);
                        turn(pt(1),pt(2),1)=0;
                    end
                    num=temp+turn(pt(1),pt(2),chance)+1;      %number of tokens in cell
                    if num<5
                        turn(pt(1),pt(2),chance)=num;     %update no. of tokens
                    end
                end
                
                ptnew=zeros(200,2);       %initialize to increase speed
                dim_pt=1;                %dimension of ptnew
                ptold=unique(ptold,'rows');
                for k=1:size(ptold,1)
                    pt=ptold(k,:);
                    %corner 1,1
                    if (pt(1)==1)&&(pt(2)==1)&&(turn(1,1,chance)>=2)
                        turn(1,1,chance)=0;
                        ptnew(dim_pt:dim_pt+1,:)=[[1,2];[2,1]];
                        dim_pt=dim_pt+2;
                        %corner 1,5
                    elseif (pt(1)==1)&&(pt(2)==5)&&(turn(1,5,chance)>=2)
                        turn(1,5,chance)=0;
                        ptnew(dim_pt:dim_pt+1,:)=[[1,4];[2,5]];
                        dim_pt=dim_pt+2;
                        %corner 5,1
                    elseif (pt(1)==5)&&(pt(2)==1)&&(turn(5,1,chance)>=2)
                        turn(5,1,chance)=0;
                        ptnew(dim_pt:dim_pt+1,:)=[[4 1];[5 2]];
                        dim_pt=dim_pt+2;
                        %corner 5,5
                    elseif (pt(1)==5)&&(pt(2)==5)&&(turn(5,5,chance)>=2)
                        turn(5,5,chance)=0;
                        ptnew(dim_pt:dim_pt+1,:)=[[4,5];[5,4]];
                        dim_pt=dim_pt+2;
                        %side x=1
                    elseif (pt(1)==1)&&(turn(1,pt(2),chance)>=3)
                        turn(1,pt(2),chance)=0;
                        ptnew(dim_pt:dim_pt+2,:)=[[1,pt(2)+1];[1,pt(2)-1];[2,pt(2)]];
                        dim_pt=dim_pt+3;
                        
                        %side y=1
                    elseif (pt(2)==1)&&(turn(pt(1),1,chance)>=3)
                        turn(pt(1),1,chance)=0;
                        ptnew(dim_pt:dim_pt+2,:)=[[pt(1)+1,1];[pt(1)-1,1];[pt(1),2]];
                        dim_pt=dim_pt+3;
                        %side y=5
                    elseif (pt(2)==5)&&(turn(pt(1),5,chance)>=3)
                        turn(pt(1),5,chance)=0;
                        ptnew(dim_pt:dim_pt+2,:)=[[pt(1)+1,5];[pt(1)-1,5];[pt(1),4]];
                        dim_pt=dim_pt+3;
                        %side x=5
                    elseif (pt(1)==5)&&(turn(5,pt(2),chance)>=3)
                        turn(5,pt(2),chance)=0;
                        ptnew(dim_pt:dim_pt+2,:)=[[5,pt(2)+1];[5,pt(2)-1];[4,pt(2)]];
                        dim_pt=dim_pt+3;
                        %middle block
                    elseif turn(pt(1),pt(2),chance)>=4
                        turn(pt(1),pt(2),chance)=0;
                        ptnew(dim_pt:dim_pt+3,:)=[[pt(1)-1,pt(2) ];[pt(1),pt(2)-1 ];...
                            [pt(1),pt(2)+1 ];[pt(1)+1,pt(2) ]];
                        dim_pt=dim_pt+4;
                    end
                end
                ptnew(dim_pt:200,:)=[];              %diminish ptnew
                
                if dim_pt==1
                    ballsum=sum(sum(turn(:,:,chance)));
                    return
                elseif call2process<450
                    ballsum=process(ptnew);
                    return
                else
                    ballsum=sum(sum(turn(:,:,chance)));
                end
            end
        end
        
        %show computer's move for 2 seconds
        function show(pt)
            num=turn(pt(1),pt(2),chance)+1;
            %generate xdata for token
            if num/2==fix(num/2)                %even check
                xdata=pt(1)-0.25;
            else
                xdata=pt(1)-0.75;
            end
            %generate ydata for token
            if num<3
                ydata=pt(2)-0.75;
            else
                ydata=pt(2)-0.25;
            end
            temph=line(xdata,ydata,'marker','o',...
                'markersize',15,'markerfacecolor','g');
            for z=0:4
                set(temph,'markerfacecolor','g');
                pause(0.05);
                if chance==1
                    set(temph,'markerfacecolor','b');
                else
                    set(temph,'markerfacecolor','r');
                end
                pause(0.05);
            end
            delete(temph);
        end
        
    end

end

Contact us