ID:50337
Title:incompetants
Author:nathan q
Date:2008-11-07 19:02:20
Score:24886.7166
Result:24838.77 (cyc: 54, node: 1578)
CPU Time:47.4651
Status:Passed
Comments:
Based on:none
Code:
function [dRow,dCol,Action,Mark]=solver(mainMap,foodMap,myAntMap,opAntMap, ...
                                          myScentMap,opScentMap,myDeathMap,opDeathMap );


i35=3:5;
i24=2:4;


% expected number of (random wanderer) enemies in this square at end of move:
nEnemy = sum(sum(opAntMap(i24,i24)))/9;
ratio = nEnemy/myAntMap(13);

if ratio>1 || rand<ratio
    dRow=0;
    dCol=0;
    Action=-1;
    Mark=0;
    return
end


food0=foodMap;    
i02=0:2;
dirx = [-1,2,2,2,-1; 2,-1,-1,-1,2; 2,0,0,0,2; 2,1,1,1,2; 1,2,2,2,1];
diry = [-1,-1,0,1,1; -1,-1,0,1,1; -1,-1,0,1,1; -1,-1,0,1,1; -1,-1,0,1,1];
% find reachable regions
BAD=isnan(mainMap);
if BAD(7)||BAD(9)||BAD(17)||BAD(19)
    if BAD(7)
        BAD(1)=1;
        BAD(2)=BAD(2)||BAD(8);
        BAD(3)=BAD(3)||(BAD(8)&&BAD(9));
        BAD(6)=BAD(6)||BAD(12);
        BAD(11)=BAD(11)||(BAD(12)&&BAD(17));
    end
    if BAD(9)
        BAD(4)=BAD(4)||BAD(8);
        BAD(5)=1;
        BAD(10)=BAD(10)||BAD(14);
    end
    if BAD(17)
        BAD(16)=BAD(16)||BAD(12);
        BAD(21)=1;
        BAD(22)=BAD(22)||BAD(18);
    end
    if BAD(19)
        BAD(15)=BAD(15)||(BAD(9)&&BAD(14));
        BAD(20)=BAD(20)||(BAD(14));
        BAD(23)=BAD(23)||(BAD(17)&&BAD(18));
        BAD(24)=BAD(24)||(BAD(18));
        BAD(25)=1;
    end
    % modify current board based on reachable regions
    mainMap(BAD)=-1;
    foodMap(BAD)=0;
    myScentMap(BAD)=0;
end
BAD(13)=1;
% setup game stats
scents=myScentMap(myScentMap>0);
maxscent=max(scents(:));   % the max non-zero scent
% Mark this field
if (max(mainMap(:)))   % there is a base
       % try to set scent to 400
    totalMark=400-myScentMap(13);
    myScentMap(13)=400;
    init=0;    % we've found the base - no longer in init mode
    hills=find(mainMap>0);
    myScentMap(hills)=1e7;
    foodMap(hills)=0;
    if isempty(scents);maxscent=totalMark;end
elseif (maxscent==min(scents(:))) %  the non-zero scents are all equal => we are in init mode
    totalMark=maxscent-myScentMap(13);  % set the scent to the uniform level to mark the spot as explored
    myScentMap(13)=maxscent;
    init=1;
elseif isempty(scents) % nothing is marked => we are in init mode
    totalMark=98;
    myScentMap(13)=myScentMap(13)+98;
    maxscent=totalMark;
    init=1;
else
    totalMark=maxscent-1-myScentMap(13);   % set the scent to one less than the max local scent
    myScentMap(13)=maxscent-1;
    init=0;
end

    intScent = floor(totalMark/myAntMap(13));
    fracScent = totalMark/myAntMap(13) - intScent;
    Mark = intScent;
    if fracScent>0 && rand<fracScent
        Mark = Mark+1;
    end

    % look around for food
if max(foodMap(:))<1 || init % nothing found or no way home, search
    target=maxscent-myScentMap;target(BAD)=-1;
    [y,x]=find(target==max(target(:)));
    if (numel(y)>1)
        r=ceil(rand*numel(y));
        y=y(r);
        x=x(r);
    end
    Action=foodMap(13)>0;
    dCol = dirx(x,y);
    if dCol<2
        dRow = diry(x,y);
        return;
    end;
    m(7,7)=0;
    m(y+i02,x+i02)=1;
    m=m(i35,i35);
    t=target(i24,i24).*m+m;
    [dRow,dCol]=find(t==max(t(:)));
    r=ceil(rand*numel(dRow));
    dRow=dRow(r)-2;
    dCol=dCol(r)-2;
    return;
end;

% % the "Euclidean" bit
% if max(mainMap(:)==1) && foodMap(13)>1  % the base is in sight and we have food
%     localmain=mainMap(i24,i24); 
%     if max(localmain(:))<1 %%&& sum(food0(mainMap==1))>=497
%         if     (mainMap(1,2)==1 || mainMap(2,1)==1) && mainMap(2,2)==0
%             dRow=-1; dCol=-1; Action=1; return
%         elseif (mainMap(1,4)==1 || mainMap(2,5)==1) && mainMap(2,4)==0
%             dRow=-1; dCol=1; Action=1; return
%         elseif (mainMap(4,1)==1 || mainMap(5,2)==1) && mainMap(4,2)==0
%             dRow=1; dCol=-1; Action=1; return
%         elseif (mainMap(5,4)==1 || mainMap(4,5)==1) && mainMap(4,4)==0
%             dRow=1; dCol=1; Action=1; return
%         end
%     end
% end

if (foodMap(13)>1) %% nq change &&rand<.95 % sit on a lot of food, go home
    if foodMap(13)>myAntMap(13) || rand < (foodMap(13)-2)/myAntMap(13)  
        % to do: try to ensure we leave one piece of food there
        target=myScentMap;target(BAD)=-1;
        [y,x]=find(target==max(target(:))); % move towards higher scent
        if (numel(y)>1)
            y=y(1);
            x=x(1);
        end
        Action=1;
        dCol = dirx(x,y);
        if dCol<2
            dRow = diry(x,y);
            return;
        end;
        m(7,7)=0;
        m(y+i02,x+i02)=1;
        m=m(i35,i35);
        t=target(i24,i24).*m+m;
        [dRow,dCol]=find(t==max(t(:)));
        r=ceil(rand*numel(dRow));
        dRow=dRow(r)-2;
        dCol=dCol(r)-2;
        return;
    end
end;
if (foodMap(13) >= 1) % sit on one food, build track
    % ONLY ONE ANT should do this
    if myAntMap(13)==1 || rand<1/myAntMap(13)
        target=myScentMap;target(BAD)=-1;
        [yhome,xhome]=find(target==max(target(:)));
        lowfood=foodMap;lowfood(myScentMap>=myScentMap(13))=0;
        Action=1;
        dhere=max(max(abs(yhome-3),abs(xhome-3)));
        runs=numel(lowfood(lowfood(:)>0));
        lowfood(i24,i24)=lowfood(i24,i24)*1e8;
        target=lowfood;target(BAD)=-1;
        while runs % fetch food behind
            [y,x]=find(target==max(target(:)));
            if (numel(y)>1)
                r=ceil(rand*numel(y));
                y=y(r);
                x=x(r);
            end
            dCol = dirx(x,y);
            if dCol<2
                dRow = diry(x,y);
            else
                m(7,7)=0;
                m(y+i02,x+i02)=1;
                m=m(i35,i35);
                t=target(i24,i24).*m+m;
                [dRow,dCol]=find(t==max(t(:)));
                r=ceil(rand*numel(dRow));
                dRow=dRow(r)-2;
                dCol=dCol(r)-2;
            end
            if ( min(max(abs(yhome-y),abs(xhome-x))) > dhere )
                Action=0;
                return;
            end
            target(y,x)=0;runs=runs-1;
        end
        if (Action) % go home
            target=myScentMap;target(BAD)=-1;
            [y,x]=find(target==max(target(:)));
            if (numel(y)>1)
                r=ceil(rand*numel(y));
                y=y(r);
                x=x(r);
            end
            dCol = dirx(x,y);
            if dCol<2
                dRow = diry(x,y);
                return;
            end;
            m(7,7)=0;
            m(y+i02,x+i02)=1;
            m=m(i35,i35);
            t=target(i24,i24).*m+m;
            [dRow,dCol]=find(t==max(t(:)));
            r=ceil(rand*numel(dRow));
            dRow=dRow(r)-2;
            dCol=dCol(r)-2;
            return;
        end
        return;
    end
end;
target=max(foodMap-myAntMap,0);target(BAD)=-1000;
[y,x]=find(target==max(target(:)));
if (numel(y)>1)
    r=ceil(rand*numel(y));
    y=y(r);
    x=x(r);
end
Action=0;
dCol = dirx(x,y);
if dCol<2
    dRow = diry(x,y);
    return;
end;
m(7,7)=0;
m(y+i02,x+i02)=1;
m=m(i35,i35);
t=target(i24,i24).*m+m;
[dRow,dCol]=find(t==max(t(:)));
r=ceil(rand*numel(dRow));
dRow=dRow(r)-2;
dCol=dCol(r)-2;