ID:49920
Title:Stupid Ant
Author:Benja&Litos
Date:2008-11-06 08:07:23
Score:13737.9773
Result:13531.90 (cyc: 55, node: 1567)
CPU Time:110.6204
Status:Passed
Comments:
Based on:none
Code:
function [dRow,dCol,action,mark] = solver( ...
    mainMap,foodMap,myAntMap,opAntMap, ...
    myScentMap,opScentMap,myDeathMap,opDeathMap)


%Smart Ant
% rand(1,5);
i35=3:5;
i24=2:4;
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) %BAD is the region unreachable in two turns
    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;
    food(BAD)=0;
    myScentMap(BAD)=0;
end
BAD(13)=1;
% setup game stats
myScents=myScentMap(myScentMap>0);
maxmyScent=max(myScents(:));
% mark this field
if (max(mainMap(:))) %there is any of my anthills in the reachable region 
    mark=(2000-myScentMap(13))/sum(myAntMap(:)); %the scent mark in the region near the anthill is limited to 400 units
    myScentMap(13)=2000;
    init=0;
    myHills=find(mainMap>0);
    myScentMap(myHills)=1e7;   %consider that my hill has the highest scent
    foodMap(myHills)=0;      %consider that my hill has no food
    if isempty(myScents);maxmyScent=mark;end
elseif (maxmyScent==min(myScents(:))) % init phase
    mark=maxmyScent-myScentMap(13);
    myScentMap(13)=maxmyScent;
    init=1;
elseif isempty(myScents) % nothing is marked
    mark=98;
    myScentMap(13)=myScentMap(13)+98;
    maxmyScent=mark;
    init=1;
else
    mark=(maxmyScent-2-myScentMap(13))/sum(myAntMap(:));
    myScentMap(13)=maxmyScent-1;
    init=0;
end
%kill'em
if ((max(myDeathMap(:))>80)&&(max(max(opAntMap(i24,i24))))&&(rand>0.5))||(opAntMap(13)>0)
    action=-1;
    dRow=0;
    dCol=0;
    return;
end
% look around for food

if max(foodMap(:))<1 ||init % nothing found or no way home, search
    if rand(1)<0.8 %drunk ant clause
        target=max(maxmyScent-myScentMap-myAntMap*10-opScentMap*0.5,0);
    else
        target=zeros(5,5);
    end
    target(BAD)=-1; %go to zones with low scent to explore new regions
    [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

ratio=foodMap(13)/myAntMap(13);

if ((ratio>1)&&rand<.95)||((foodMap(13)>0)&&(ratio<1)&&(rand<ratio)) % sit on a lot of food, go home
    target=myScentMap;target(BAD)=-1; %go to zones with high Scent to come back home with food
    [y,x]=find(target==max(target(:)));
    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;
if (foodMap(13) >= 1) % sit on one food, build track
    target=myScentMap;target(BAD)=-1; %the target is the anthill
    [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;
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;


end