function t()
clc;
global board;
board=zeros(3);
turn=round(rand());
win=0;
while(win==0)
    disp(board);
    if(turn==0)
        disp('Thinking..');
        ind=0;
        if(sum(sum(board))==2)
            if(board(1,1)==2||board(1,3)==2||board(3,1)==2||board(3,3)==2)
                ind=5;
            elseif(board(2,2)==2)
                ind=[1 3 7 9];
            end
        elseif((sum(diag(board))==5||sum(diag(flipud(board)))==5)&&sum(sum(board))==5)
            if(board(2,2)==1)
                ind=find(board(2:2:8)==0).*2;
            else
                ind=[1 3 7 9];
                ind=ind(board(ind)==0);
            end
        elseif(sum(sum(board))==0)
            ind=[1 3 7 9];
        end
        if(ind==0)
            prob=zeros(3);
            %defensive
            for r=1:3
                s=sum(board(r,:)==2);
                prob(r,:)=prob(r,:)+(s^3).*(board(r,:)==0);
                s=sum(board(:,r)==2);
                prob(:,r)=prob(:,r)+(s^3).*(board(:,r)==0);
            end
            s=sum(board(1:4:9)==2);
            prob(1:4:9)=prob(1:4:9)+(s^3).*(board(1:4:9)==0);
            s=sum(board(3:2:7)==2);
            prob(3:2:7)=prob(3:2:7)+(s^3).*(board(3:2:7)==0);
            %offensive
            for r=1:3
                s=sum(board(r,:)==1);
                prob(r,:)=prob(r,:)+(s^3+max(s-1,0).*10).*(board(r,:)==0);
                s=sum(board(:,r)==1);
                prob(:,r)=prob(:,r)+(s^3+max(s-1,0).*10).*(board(:,r)==0);
            end
            s=sum(board(1:4:9)==1);
            prob(1:4:9)=prob(1:4:9)+(s^3+s^2+max(s-1,0).*10).*(board(1:4:9)==0);
            s=sum(board(3:2:7)==1);
            prob(3:2:7)=prob(3:2:7)+(s^3+s^2+max(s-1,0).*10).*(board(3:2:7)==0);
            prob(2,2)=prob(2,2).*2;
            ind=find(prob==max(max(prob)));
        end
        i=ind(round(rand()*(length(ind)-1)+1));
        while(~place(1,mod(i-1,3)+1,ceil(i/3)))
            prob(prob==max(max(prob)))=min(min(prob))-10;
            ind=find(prob==max(max(prob)));
            i=ind(round(rand()*(length(ind)-1)+1));
        end
    else
        p=false;
        while(~p)
            r=input('Row: ');
            c=input('Column: ');
            if(~isempty(r)&&~isempty(c))
                p=place(2,r,c);
            end
        end
        clc;
    end
    turn=mod(turn+1,2);
    win=checkWin();
    if(~win&&~any(any(board==0)))
        win=-1;
    end
end
disp(board);
if(win==-1)
    disp('Tie');
elseif(win==1)
    disp('I won!');
else
    disp('You won!');
end
end

function win=checkWin()
global board;
win=0;
if(all(diag(board)==1)||all(diag(flipud(board))==1))
    win=1;
end
if(all(diag(board)==2)||all(diag(flipud(board))==2))
    win=2;
end
if(win==0)
    for r=1:3
        if(all(board(r,:)==1)||all(board(:,r)==1))
            win=1;
        end
        if(all(board(r,:)==2)||all(board(:,r)==2))
            win=2;
        end
    end
end
end

function p=place(n,r,c)
global board;
if(r<0||r>3||c<0||c>3)
    p=false;
else
    p=true;
end
if(p)
    if(board(r,c)~=0)
        p=false;
    else
        board(r,c)=n;
    end
end
end