ID:50004
Title:Fifth
Author:Edoardo Valori
Date:2008-11-06 11:58:44
Score:27440.5568
Result:25919.22 (cyc: 14, node: 940)
CPU Time:144.4002
Status:Passed
Comments:
Based on:none
Code:
function [dy, dx, action, mark] = solver(main, food, ants, opAnt, scent, opScent, myDeath, opDeath)
atth = 1; % attack threshold: 0 = peace, 1 = war
attack = - (rand<atth);
foodth = .94;
firstmark = 68;
danger = 1;
vierh = 600;
i35=3:5;
i24=2:4;
i02=0:2;
nhbr8 = [7,8,9,12,14,17,18,19];
%7 12 17   1 4 6 
%8    18   2   7
%9 14 19   3 5 8

% find reachable regions
BAD=isnan(main);
m(7,7)=0;
L1=[1 5 21 25];
I1=[7 9 17 19];
L2=[2 4 6 10 16 20 22 24];
I2=[7 9 7 9 17 19 17 19];
J2=[8 8 12 14 12 14 18 18];
L3=[3 11 15 23];
I3=[7 7 19 19];
J3=[8 12 14 18];
K3=[9 17 9 17];
BAD(L1)=BAD(L1) | BAD(I1);
BAD(L2)=BAD(L2) | (BAD(I2) & BAD(J2));
BAD(L3)=BAD(L3) | (BAD(I3) & BAD(J3) & BAD(K3));
main(BAD)=-100;
food(BAD)=0;
scent(BAD)=0;
ants(BAD)=0;
opAnt(BAD)=0;
opScent(BAD)=0;
myDeath(BAD)=0;
opDeath(BAD)=0;
BAD(13)=1;
% setup game stats
scents=scent(scent>0);
maxscent=max(scents(:));
% mark this field
if (max(main(:))>0)
    mark=vierh-scent(13);
    scent(13)=vierh;
    init=0;
    hills=find(main>0);
    scent(hills)=1e7;
    food(hills)=0;
    if isempty(scents);maxscent=mark;end
elseif (maxscent==min(scents(:))) % init phase
    mark=maxscent-scent(13);
    scent(13)=maxscent;
    init=1;
elseif isempty(scents) % nothing is marked
    mark= firstmark;
    scent(13)=scent(13)+firstmark;
    maxscent=mark;
    init=1;
else
    mark=maxscent-1-scent(13);
    scent(13)=maxscent-1;
    init=0;
end
% look around for food
if max(food(:))<1 |init % nothing found or no way home, search
    target=((main<0)).*(maxscent-scent);target(BAD)=-100;
    [dy,dx]=weg(target,m);
    action=food(13)>0;
    if ~action
        action = attack;
    end
    return;
end
if (food(13)>1)&rand<foodth % sit on a lot of food, go home
    target=scent;target(BAD)=-100;
    [dy,dx]=weg(target,m);
    action=1;
    return;
end;
if (food(13) >= 1) % sit on one food, build track
    target=scent;target(BAD)=-100;
    [yhome,xhome]=find(target==max(target(:)));
    lowfood=food.*(scent<scent(13));
    action=1;
    dhere=max(max(abs(yhome-3),abs(xhome-3)));
    emph=ones(5);
    emph(i24,i24)=1e8;
    runs=numel(find(lowfood(:)>0));
    target=lowfood.*emph;target(BAD)=-100;
    while runs % fetch food behind
        [dy,dx,y,x]=weg(target,m);
        dthere=min(max(abs(yhome-y),abs(xhome-x)));
        if (dthere>dhere)
            action = attack;
            return;
        end
        target(y,x)=0;runs=runs-1;
    end
    if (action>0) % go home        
        target=scent;target(BAD)=-100;
        [dy,dx]=weg(target,m);
    end
    return;
end;
target=max((food-ants-opAnt)./(1+danger*myDeath),0);target(BAD)=-100;
action = attack;
[dy,dx]=weg(target,m);
return;

function [dy,dx,y,x]=weg(target,m1)
i35=3:5;
i24=2:4;
i02=0:2;
[y,x]=find(target==max(target(:)));
if (numel(y)>1)
    r=ceil(rand*numel(y));
    y=y(r);
    x=x(r);
end
if (y>1&y<5&x>1&x<5)|((y==1|y==5)&(x==1|x==5)) % direct way
    dy=y-3;
    dx=x-3;
    return;
end;
m1(y+i02,x+i02)=1;
m=m1(i35,i35);
t=target(i24,i24).*m+m;
[dy,dx]=find(t==max(t(:)));
r=ceil(rand*numel(dy));
dy=dy(r)-2;
dx=dx(r)-2;