ID:50425
Title:Finite 2
Author:Daniel Armyr
Date:2008-11-08 09:06:23
Score:32098.9201
Result:29557.11 (cyc: 7, node: 423)
CPU Time:152.1457
Status:Passed
Comments:
Based on:none
Code:
function [dRow,dCol,action,mark] = solver(mainMap,foodMap,myAntMap,opAntMap, ...
    myScentMap,opScentMap,myDeathMap,opDeathMap)

% A few-state machine that first look for food and then tries to return home.
% The second state has two sub-states, one where the nest is seen and one
% where it isn't.

%pause(0.01);
dRow = 0;
dCol = 0;
action = -1;
mark = 0;


%Find which state we are in.
if ( sum(opAntMap(:)) > 0 )
    dRow = 0;
    dCol = 0;
    action = -1;
    mark = 0;
elseif ( foodMap(3,3) > 0 && mainMap(3,3) ~= 1 )
    % We are carrying food and are not at our own base.
    if ( sum( mainMap(:) == 1 ) > 0 )
        %We see the nest. Run for it. Mark heavilly.
        [dRow,dCol] = getMoveDirection( double(mainMap == 1) );
        action = 1;
        mark = 100;
    else
        %We do not see the nest. Follow the scent.
        [dRow,dCol] = getMoveDirection( myScentMap );
        action = 1;
        mark = 1;
    end
else
    carriableFoodMap = foodMap.*(mainMap~=1);
    unhandledFoodMap = carriableFoodMap - myAntMap;
    if ( sum(unhandledFoodMap(:)) > 0 )
        %We see food that none of our guys are handling. Go for it.
        [dRow,dCol] = getMoveDirection( unhandledFoodMap );
        action = -1;
        mark = 20;
    else 
        %We are not carrying food and have no idea where we are going.
        %Attack?
        if ( opAntMap(3,3) > 0 )
            dRow = 0;
            dCol = 0;
            action = -1;
            mark = 0;
        else
            %We wander randomly
            dRow = round(rand)*2-1;
            dCol = round(rand)*2-1;
            action = -1;
            mark = 10;
        end
    end
end

%Finish by checking that we are not walking into a wall.
[dRow,dCol] = verifySaneMovement( dRow,dCol,mainMap);

end

function [dRow,dCol] = getMoveDirection( attr )
%Find the equation of the attractor plane
[xMesh,yMesh] = meshgrid ( -2:2, -2:2 );
A = [xMesh(:) yMesh(:) ones(5*5,1)];
b = attr(:);
AtA = A'*A;
Atb = A'*b;
coefs = AtA\Atb;
coefs = coefs(1:2);
coefs = coefs / norm ( coefs );
dCol = round(coefs(1));
dRow = round(coefs(2));
if ( (dCol < 0.1 && dRow < 0.1) || any( isnan( [dRow dCol] ) ) )
    dCol = round(rand*2)-1;
    dRow = round(rand*2)-1;
end
end

function [dRow,dCol] = verifySaneMovement( dRow, dCol, mainMap )
    if (isnan(mainMap(3+dRow,3+dCol)))
        if ( ~isnan(mainMap(3+dRow,3)) )
            dCol = 0;
        elseif ( ~isnan(mainMap(3,3+dCol)) )
            dRow = 0;
        end            
    end
end